xref: /openbmc/qemu/block/curl.c (revision 44b424dc)
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  */
24452fcdbcSMarkus Armbruster 
2580c71a24SPeter Maydell #include "qemu/osdep.h"
26da34e65cSMarkus Armbruster #include "qapi/error.h"
27796a060bSRichard W.M. Jones #include "qemu/error-report.h"
280b8fa32fSMarkus Armbruster #include "qemu/module.h"
29922a01a0SMarkus Armbruster #include "qemu/option.h"
30e2c1c34fSMarkus Armbruster #include "block/block-io.h"
31737e150eSPaolo Bonzini #include "block/block_int.h"
32452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
33d49b6836SMarkus Armbruster #include "qapi/qmp/qstring.h"
341bff9606SDaniel P. Berrange #include "crypto/secret.h"
35769ce76dSAlexander Graf #include <curl/curl.h>
36f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
37ed2a66deSLaurent Vivier #include "trace.h"
38769ce76dSAlexander Graf 
39769ce76dSAlexander Graf // #define DEBUG_VERBOSE
40769ce76dSAlexander Graf 
41e7b8d9d0SAnton Johansson /* CURL 7.85.0 switches to a string based API for specifying
42e7b8d9d0SAnton Johansson  * the desired protocols.
43e7b8d9d0SAnton Johansson  */
44e7b8d9d0SAnton Johansson #if LIBCURL_VERSION_NUM >= 0x075500
45e7b8d9d0SAnton Johansson #define PROTOCOLS "HTTP,HTTPS,FTP,FTPS"
46e7b8d9d0SAnton Johansson #else
47fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
4823dce387SMax Reitz                    CURLPROTO_FTP | CURLPROTO_FTPS)
49e7b8d9d0SAnton Johansson #endif
50fb6d1bbdSStefan Hajnoczi 
51769ce76dSAlexander Graf #define CURL_NUM_STATES 8
52769ce76dSAlexander Graf #define CURL_NUM_ACB    8
53f76faedaSRichard W.M. Jones #define CURL_TIMEOUT_MAX 10000
54769ce76dSAlexander Graf 
55e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL       "url"
56e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead"
5797a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
58212aefaaSDaniel Henrique Barboza #define CURL_BLOCK_OPT_TIMEOUT "timeout"
59a94f83d9SRichard W.M. Jones #define CURL_BLOCK_OPT_COOKIE    "cookie"
60327c8ebdSPeter Krempa #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
611bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_USERNAME "username"
621bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
631bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
641bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
65e3542c67SMatthew Booth 
66712b64e8SMax Reitz #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024)
67712b64e8SMax Reitz #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true
68712b64e8SMax Reitz #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5
69712b64e8SMax Reitz 
70769ce76dSAlexander Graf struct BDRVCURLState;
7104878616SMax Reitz struct CURLState;
72769ce76dSAlexander Graf 
732d25964dSJeff Cody static bool libcurl_initialized;
742d25964dSJeff Cody 
75769ce76dSAlexander Graf typedef struct CURLAIOCB {
7628256d82SPaolo Bonzini     Coroutine *co;
77769ce76dSAlexander Graf     QEMUIOVector *qiov;
78363c3c85SNick Thomas 
792125e5eaSPaolo Bonzini     uint64_t offset;
802125e5eaSPaolo Bonzini     uint64_t bytes;
8128256d82SPaolo Bonzini     int ret;
82363c3c85SNick Thomas 
83769ce76dSAlexander Graf     size_t start;
84769ce76dSAlexander Graf     size_t end;
85769ce76dSAlexander Graf } CURLAIOCB;
86769ce76dSAlexander Graf 
87ff5ca166SMax Reitz typedef struct CURLSocket {
88ff5ca166SMax Reitz     int fd;
893663dca4SMax Reitz     struct BDRVCURLState *s;
90ff5ca166SMax Reitz } CURLSocket;
91ff5ca166SMax Reitz 
92769ce76dSAlexander Graf typedef struct CURLState
93769ce76dSAlexander Graf {
94769ce76dSAlexander Graf     struct BDRVCURLState *s;
95769ce76dSAlexander Graf     CURLAIOCB *acb[CURL_NUM_ACB];
96769ce76dSAlexander Graf     CURL *curl;
97769ce76dSAlexander Graf     char *orig_buf;
982125e5eaSPaolo Bonzini     uint64_t buf_start;
99769ce76dSAlexander Graf     size_t buf_off;
100769ce76dSAlexander Graf     size_t buf_len;
101769ce76dSAlexander Graf     char range[128];
102769ce76dSAlexander Graf     char errmsg[CURL_ERROR_SIZE];
103769ce76dSAlexander Graf     char in_use;
104769ce76dSAlexander Graf } CURLState;
105769ce76dSAlexander Graf 
106769ce76dSAlexander Graf typedef struct BDRVCURLState {
107769ce76dSAlexander Graf     CURLM *multi;
108031fd1beSPeter Maydell     QEMUTimer timer;
1092125e5eaSPaolo Bonzini     uint64_t len;
110769ce76dSAlexander Graf     CURLState states[CURL_NUM_STATES];
1110f418a20SMax Reitz     GHashTable *sockets; /* GINT_TO_POINTER(fd) -> socket */
112769ce76dSAlexander Graf     char *url;
113c76f4952SNolan     size_t readahead_size;
11497a3ea57SMatthew Booth     bool sslverify;
115f76faedaSRichard W.M. Jones     uint64_t timeout;
116a94f83d9SRichard W.M. Jones     char *cookie;
1173494d650SFam Zheng     bool accept_range;
11863f0f45fSStefan Hajnoczi     AioContext *aio_context;
119ba3186c4SPaolo Bonzini     QemuMutex mutex;
120709f2132SPaolo Bonzini     CoQueue free_state_waitq;
1211bff9606SDaniel P. Berrange     char *username;
1221bff9606SDaniel P. Berrange     char *password;
1231bff9606SDaniel P. Berrange     char *proxyusername;
1241bff9606SDaniel P. Berrange     char *proxypassword;
125769ce76dSAlexander Graf } BDRVCURLState;
126769ce76dSAlexander Graf 
127769ce76dSAlexander Graf static void curl_clean_state(CURLState *s);
128769ce76dSAlexander Graf static void curl_multi_do(void *arg);
129769ce76dSAlexander Graf 
1300f418a20SMax Reitz static gboolean curl_drop_socket(void *key, void *value, void *opaque)
1310f418a20SMax Reitz {
1320f418a20SMax Reitz     CURLSocket *socket = value;
1330f418a20SMax Reitz     BDRVCURLState *s = socket->s;
1340f418a20SMax Reitz 
13560f782b6SStefan Hajnoczi     aio_set_fd_handler(s->aio_context, socket->fd,
136826cc324SStefan Hajnoczi                        NULL, NULL, NULL, NULL, NULL);
1370f418a20SMax Reitz     return true;
1380f418a20SMax Reitz }
1390f418a20SMax Reitz 
1400f418a20SMax Reitz static void curl_drop_all_sockets(GHashTable *sockets)
1410f418a20SMax Reitz {
1420f418a20SMax Reitz     g_hash_table_foreach_remove(sockets, curl_drop_socket, NULL);
1430f418a20SMax Reitz }
1440f418a20SMax Reitz 
14534db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held.  */
146031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
147031fd1beSPeter Maydell {
148031fd1beSPeter Maydell     BDRVCURLState *s = opaque;
149031fd1beSPeter Maydell 
150ed2a66deSLaurent Vivier     trace_curl_timer_cb(timeout_ms);
151031fd1beSPeter Maydell     if (timeout_ms == -1) {
152031fd1beSPeter Maydell         timer_del(&s->timer);
153031fd1beSPeter Maydell     } else {
154031fd1beSPeter Maydell         int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
155031fd1beSPeter Maydell         timer_mod(&s->timer,
156031fd1beSPeter Maydell                   qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
157031fd1beSPeter Maydell     }
158031fd1beSPeter Maydell     return 0;
159031fd1beSPeter Maydell }
160031fd1beSPeter Maydell 
16134db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held.  */
162769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
16363f0f45fSStefan Hajnoczi                         void *userp, void *sp)
164769ce76dSAlexander Graf {
16563f0f45fSStefan Hajnoczi     BDRVCURLState *s;
166838ef602SMatthew Booth     CURLState *state = NULL;
167ff5ca166SMax Reitz     CURLSocket *socket;
168ff5ca166SMax Reitz 
169838ef602SMatthew Booth     curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
17063f0f45fSStefan Hajnoczi     s = state->s;
171838ef602SMatthew Booth 
1720f418a20SMax Reitz     socket = g_hash_table_lookup(s->sockets, GINT_TO_POINTER(fd));
173ff5ca166SMax Reitz     if (!socket) {
174ff5ca166SMax Reitz         socket = g_new0(CURLSocket, 1);
175ff5ca166SMax Reitz         socket->fd = fd;
1763663dca4SMax Reitz         socket->s = s;
1770f418a20SMax Reitz         g_hash_table_insert(s->sockets, GINT_TO_POINTER(fd), socket);
178ff5ca166SMax Reitz     }
179ff5ca166SMax Reitz 
180ed2a66deSLaurent Vivier     trace_curl_sock_cb(action, (int)fd);
181769ce76dSAlexander Graf     switch (action) {
182769ce76dSAlexander Graf         case CURL_POLL_IN:
18360f782b6SStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd,
184826cc324SStefan Hajnoczi                                curl_multi_do, NULL, NULL, NULL, socket);
185769ce76dSAlexander Graf             break;
186769ce76dSAlexander Graf         case CURL_POLL_OUT:
18760f782b6SStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd,
188826cc324SStefan Hajnoczi                                NULL, curl_multi_do, NULL, NULL, socket);
189769ce76dSAlexander Graf             break;
190769ce76dSAlexander Graf         case CURL_POLL_INOUT:
19160f782b6SStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd,
192826cc324SStefan Hajnoczi                                curl_multi_do, curl_multi_do,
193826cc324SStefan Hajnoczi                                NULL, NULL, socket);
194769ce76dSAlexander Graf             break;
195769ce76dSAlexander Graf         case CURL_POLL_REMOVE:
19660f782b6SStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd,
197826cc324SStefan Hajnoczi                                NULL, NULL, NULL, NULL, NULL);
198769ce76dSAlexander Graf             break;
199769ce76dSAlexander Graf     }
200769ce76dSAlexander Graf 
201007f339bSMax Reitz     if (action == CURL_POLL_REMOVE) {
2020f418a20SMax Reitz         g_hash_table_remove(s->sockets, GINT_TO_POINTER(fd));
203007f339bSMax Reitz     }
204007f339bSMax Reitz 
205769ce76dSAlexander Graf     return 0;
206769ce76dSAlexander Graf }
207769ce76dSAlexander Graf 
20834db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held.  */
2093494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
210769ce76dSAlexander Graf {
2113494d650SFam Zheng     BDRVCURLState *s = opaque;
212769ce76dSAlexander Graf     size_t realsize = size * nmemb;
2137788a319SDavid Edmondson     const char *header = (char *)ptr;
2147788a319SDavid Edmondson     const char *end = header + realsize;
21569032253SDavid Edmondson     const char *accept_ranges = "accept-ranges:";
2167788a319SDavid Edmondson     const char *bytes = "bytes";
217769ce76dSAlexander Graf 
2187788a319SDavid Edmondson     if (realsize >= strlen(accept_ranges)
21969032253SDavid Edmondson         && g_ascii_strncasecmp(header, accept_ranges,
22069032253SDavid Edmondson                                strlen(accept_ranges)) == 0) {
2217788a319SDavid Edmondson 
2227788a319SDavid Edmondson         char *p = strchr(header, ':') + 1;
2237788a319SDavid Edmondson 
2247788a319SDavid Edmondson         /* Skip whitespace between the header name and value. */
2257788a319SDavid Edmondson         while (p < end && *p && g_ascii_isspace(*p)) {
2267788a319SDavid Edmondson             p++;
2277788a319SDavid Edmondson         }
2287788a319SDavid Edmondson 
2297788a319SDavid Edmondson         if (end - p >= strlen(bytes)
2307788a319SDavid Edmondson             && strncmp(p, bytes, strlen(bytes)) == 0) {
2317788a319SDavid Edmondson 
2327788a319SDavid Edmondson             /* Check that there is nothing but whitespace after the value. */
2337788a319SDavid Edmondson             p += strlen(bytes);
2347788a319SDavid Edmondson             while (p < end && *p && g_ascii_isspace(*p)) {
2357788a319SDavid Edmondson                 p++;
2367788a319SDavid Edmondson             }
2377788a319SDavid Edmondson 
2387788a319SDavid Edmondson             if (p == end || !*p) {
2393494d650SFam Zheng                 s->accept_range = true;
2400bfcd599SBlue Swirl             }
2417788a319SDavid Edmondson         }
2427788a319SDavid Edmondson     }
243769ce76dSAlexander Graf 
244769ce76dSAlexander Graf     return realsize;
245769ce76dSAlexander Graf }
246769ce76dSAlexander Graf 
24734db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held.  */
248769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
249769ce76dSAlexander Graf {
250769ce76dSAlexander Graf     CURLState *s = ((CURLState*)opaque);
251769ce76dSAlexander Graf     size_t realsize = size * nmemb;
252769ce76dSAlexander Graf 
253ed2a66deSLaurent Vivier     trace_curl_read_cb(realsize);
254769ce76dSAlexander Graf 
2554e767657SMax Reitz     if (!s || !s->orig_buf) {
2564e767657SMax Reitz         goto read_end;
2574e767657SMax Reitz     }
258769ce76dSAlexander Graf 
2596d4b9e55SFam Zheng     if (s->buf_off >= s->buf_len) {
2606d4b9e55SFam Zheng         /* buffer full, read nothing */
2614e767657SMax Reitz         goto read_end;
2626d4b9e55SFam Zheng     }
2636d4b9e55SFam Zheng     realsize = MIN(realsize, s->buf_len - s->buf_off);
264769ce76dSAlexander Graf     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
265769ce76dSAlexander Graf     s->buf_off += realsize;
266769ce76dSAlexander Graf 
2674e767657SMax Reitz read_end:
2684e767657SMax Reitz     /* curl will error out if we do not return this value */
2694e767657SMax Reitz     return size * nmemb;
270769ce76dSAlexander Graf }
271769ce76dSAlexander Graf 
272456af346SPaolo Bonzini /* Called with s->mutex held.  */
27328256d82SPaolo Bonzini static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
274769ce76dSAlexander Graf                           CURLAIOCB *acb)
275769ce76dSAlexander Graf {
276769ce76dSAlexander Graf     int i;
2772125e5eaSPaolo Bonzini     uint64_t end = start + len;
2782125e5eaSPaolo Bonzini     uint64_t clamped_end = MIN(end, s->len);
2792125e5eaSPaolo Bonzini     uint64_t clamped_len = clamped_end - start;
280769ce76dSAlexander Graf 
281769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
282769ce76dSAlexander Graf         CURLState *state = &s->states[i];
2832125e5eaSPaolo Bonzini         uint64_t buf_end = (state->buf_start + state->buf_off);
2842125e5eaSPaolo Bonzini         uint64_t buf_fend = (state->buf_start + state->buf_len);
285769ce76dSAlexander Graf 
286769ce76dSAlexander Graf         if (!state->orig_buf)
287769ce76dSAlexander Graf             continue;
288769ce76dSAlexander Graf         if (!state->buf_off)
289769ce76dSAlexander Graf             continue;
290769ce76dSAlexander Graf 
291769ce76dSAlexander Graf         // Does the existing buffer cover our section?
292769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
293769ce76dSAlexander Graf             (start <= buf_end) &&
2944e504535SMax Reitz             (clamped_end >= state->buf_start) &&
2954e504535SMax Reitz             (clamped_end <= buf_end))
296769ce76dSAlexander Graf         {
297769ce76dSAlexander Graf             char *buf = state->orig_buf + (start - state->buf_start);
298769ce76dSAlexander Graf 
2994e504535SMax Reitz             qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len);
3004e504535SMax Reitz             if (clamped_len < len) {
3014e504535SMax Reitz                 qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len);
3024e504535SMax Reitz             }
30328256d82SPaolo Bonzini             acb->ret = 0;
30428256d82SPaolo Bonzini             return true;
305769ce76dSAlexander Graf         }
306769ce76dSAlexander Graf 
307769ce76dSAlexander Graf         // Wait for unfinished chunks
308b7079df4SMatthew Booth         if (state->in_use &&
309b7079df4SMatthew Booth             (start >= state->buf_start) &&
310769ce76dSAlexander Graf             (start <= buf_fend) &&
3114e504535SMax Reitz             (clamped_end >= state->buf_start) &&
3124e504535SMax Reitz             (clamped_end <= buf_fend))
313769ce76dSAlexander Graf         {
314769ce76dSAlexander Graf             int j;
315769ce76dSAlexander Graf 
316769ce76dSAlexander Graf             acb->start = start - state->buf_start;
3174e504535SMax Reitz             acb->end = acb->start + clamped_len;
318769ce76dSAlexander Graf 
319769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++) {
320769ce76dSAlexander Graf                 if (!state->acb[j]) {
321769ce76dSAlexander Graf                     state->acb[j] = acb;
32228256d82SPaolo Bonzini                     return true;
323769ce76dSAlexander Graf                 }
324769ce76dSAlexander Graf             }
325769ce76dSAlexander Graf         }
326769ce76dSAlexander Graf     }
327769ce76dSAlexander Graf 
32828256d82SPaolo Bonzini     return false;
329769ce76dSAlexander Graf }
330769ce76dSAlexander Graf 
331ba3186c4SPaolo Bonzini /* Called with s->mutex held.  */
332838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s)
333769ce76dSAlexander Graf {
334769ce76dSAlexander Graf     int msgs_in_queue;
335769ce76dSAlexander Graf 
336769ce76dSAlexander Graf     /* Try to find done transfers, so we can free the easy
337769ce76dSAlexander Graf      * handle again. */
3381f2cead3SMatthew Booth     for (;;) {
339769ce76dSAlexander Graf         CURLMsg *msg;
340769ce76dSAlexander Graf         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
341769ce76dSAlexander Graf 
3421f2cead3SMatthew Booth         /* Quit when there are no more completions */
343769ce76dSAlexander Graf         if (!msg)
344769ce76dSAlexander Graf             break;
345769ce76dSAlexander Graf 
3461f2cead3SMatthew Booth         if (msg->msg == CURLMSG_DONE) {
347bfb23b48SMax Reitz             int i;
348769ce76dSAlexander Graf             CURLState *state = NULL;
349bfb23b48SMax Reitz             bool error = msg->data.result != CURLE_OK;
350bfb23b48SMax Reitz 
351f6246509SMatthew Booth             curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
352f6246509SMatthew Booth                               (char **)&state);
353f785a5aeSNicholas Thomas 
354bfb23b48SMax Reitz             if (error) {
355796a060bSRichard W.M. Jones                 static int errcount = 100;
356796a060bSRichard W.M. Jones 
357796a060bSRichard W.M. Jones                 /* Don't lose the original error message from curl, since
358796a060bSRichard W.M. Jones                  * it contains extra data.
359796a060bSRichard W.M. Jones                  */
360796a060bSRichard W.M. Jones                 if (errcount > 0) {
361796a060bSRichard W.M. Jones                     error_report("curl: %s", state->errmsg);
362796a060bSRichard W.M. Jones                     if (--errcount == 0) {
363796a060bSRichard W.M. Jones                         error_report("curl: further errors suppressed");
364796a060bSRichard W.M. Jones                     }
365796a060bSRichard W.M. Jones                 }
366bfb23b48SMax Reitz             }
367796a060bSRichard W.M. Jones 
368f785a5aeSNicholas Thomas             for (i = 0; i < CURL_NUM_ACB; i++) {
369f785a5aeSNicholas Thomas                 CURLAIOCB *acb = state->acb[i];
370f785a5aeSNicholas Thomas 
371f785a5aeSNicholas Thomas                 if (acb == NULL) {
372f785a5aeSNicholas Thomas                     continue;
373f785a5aeSNicholas Thomas                 }
374f785a5aeSNicholas Thomas 
375bfb23b48SMax Reitz                 if (!error) {
376bfb23b48SMax Reitz                     /* Assert that we have read all data */
377bfb23b48SMax Reitz                     assert(state->buf_off >= acb->end);
378bfb23b48SMax Reitz 
379bfb23b48SMax Reitz                     qemu_iovec_from_buf(acb->qiov, 0,
380bfb23b48SMax Reitz                                         state->orig_buf + acb->start,
381bfb23b48SMax Reitz                                         acb->end - acb->start);
382bfb23b48SMax Reitz 
383bfb23b48SMax Reitz                     if (acb->end - acb->start < acb->bytes) {
384bfb23b48SMax Reitz                         size_t offset = acb->end - acb->start;
385bfb23b48SMax Reitz                         qemu_iovec_memset(acb->qiov, offset, 0,
386bfb23b48SMax Reitz                                           acb->bytes - offset);
387bfb23b48SMax Reitz                     }
388bfb23b48SMax Reitz                 }
389bfb23b48SMax Reitz 
390bfb23b48SMax Reitz                 acb->ret = error ? -EIO : 0;
391f785a5aeSNicholas Thomas                 state->acb[i] = NULL;
39228256d82SPaolo Bonzini                 qemu_mutex_unlock(&s->mutex);
39328256d82SPaolo Bonzini                 aio_co_wake(acb->co);
39428256d82SPaolo Bonzini                 qemu_mutex_lock(&s->mutex);
395f785a5aeSNicholas Thomas             }
396f785a5aeSNicholas Thomas 
397769ce76dSAlexander Graf             curl_clean_state(state);
398769ce76dSAlexander Graf             break;
399769ce76dSAlexander Graf         }
400769ce76dSAlexander Graf     }
401769ce76dSAlexander Graf }
402769ce76dSAlexander Graf 
403ba3186c4SPaolo Bonzini /* Called with s->mutex held.  */
4049abaf9fcSMax Reitz static void curl_multi_do_locked(CURLSocket *socket)
405031fd1beSPeter Maydell {
4063663dca4SMax Reitz     BDRVCURLState *s = socket->s;
407031fd1beSPeter Maydell     int running;
408031fd1beSPeter Maydell     int r;
409031fd1beSPeter Maydell 
4109abaf9fcSMax Reitz     if (!s->multi) {
411031fd1beSPeter Maydell         return;
412031fd1beSPeter Maydell     }
413031fd1beSPeter Maydell 
414031fd1beSPeter Maydell     do {
4159abaf9fcSMax Reitz         r = curl_multi_socket_action(s->multi, socket->fd, 0, &running);
416031fd1beSPeter Maydell     } while (r == CURLM_CALL_MULTI_PERFORM);
417ff5ca166SMax Reitz }
418838ef602SMatthew Booth 
4199d456654SPaolo Bonzini static void curl_multi_do(void *arg)
4209d456654SPaolo Bonzini {
4219dbad87dSMax Reitz     CURLSocket *socket = arg;
4223663dca4SMax Reitz     BDRVCURLState *s = socket->s;
4239d456654SPaolo Bonzini 
4249dbad87dSMax Reitz     qemu_mutex_lock(&s->mutex);
4259dbad87dSMax Reitz     curl_multi_do_locked(socket);
4269dbad87dSMax Reitz     curl_multi_check_completion(s);
4279dbad87dSMax Reitz     qemu_mutex_unlock(&s->mutex);
428031fd1beSPeter Maydell }
429031fd1beSPeter Maydell 
430031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg)
431031fd1beSPeter Maydell {
432031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
433031fd1beSPeter Maydell     int running;
434031fd1beSPeter Maydell 
435031fd1beSPeter Maydell     if (!s->multi) {
436031fd1beSPeter Maydell         return;
437031fd1beSPeter Maydell     }
438031fd1beSPeter Maydell 
439ba3186c4SPaolo Bonzini     qemu_mutex_lock(&s->mutex);
440031fd1beSPeter Maydell     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
441031fd1beSPeter Maydell 
442838ef602SMatthew Booth     curl_multi_check_completion(s);
443ba3186c4SPaolo Bonzini     qemu_mutex_unlock(&s->mutex);
444031fd1beSPeter Maydell }
445031fd1beSPeter Maydell 
446456af346SPaolo Bonzini /* Called with s->mutex held.  */
4473ce6a729SPaolo Bonzini static CURLState *curl_find_state(BDRVCURLState *s)
448769ce76dSAlexander Graf {
449769ce76dSAlexander Graf     CURLState *state = NULL;
4503ce6a729SPaolo Bonzini     int i;
451769ce76dSAlexander Graf 
452769ce76dSAlexander Graf     for (i = 0; i < CURL_NUM_STATES; i++) {
4533ce6a729SPaolo Bonzini         if (!s->states[i].in_use) {
454769ce76dSAlexander Graf             state = &s->states[i];
455769ce76dSAlexander Graf             state->in_use = 1;
456769ce76dSAlexander Graf             break;
457769ce76dSAlexander Graf         }
458769ce76dSAlexander Graf     }
4593ce6a729SPaolo Bonzini     return state;
4603ce6a729SPaolo Bonzini }
461769ce76dSAlexander Graf 
4623ce6a729SPaolo Bonzini static int curl_init_state(BDRVCURLState *s, CURLState *state)
4633ce6a729SPaolo Bonzini {
4649e550b32SMatthew Booth     if (!state->curl) {
465769ce76dSAlexander Graf         state->curl = curl_easy_init();
4669e550b32SMatthew Booth         if (!state->curl) {
4673ce6a729SPaolo Bonzini             return -EIO;
4689e550b32SMatthew Booth         }
469b0ea6c98SPeter Maydell         if (curl_easy_setopt(state->curl, CURLOPT_URL, s->url) ||
47097a3ea57SMatthew Booth             curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
471b0ea6c98SPeter Maydell                              (long) s->sslverify) ||
472637fa44aSRichard W.M. Jones             curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST,
473b0ea6c98SPeter Maydell                              s->sslverify ? 2L : 0L)) {
474b0ea6c98SPeter Maydell             goto err;
475a94f83d9SRichard W.M. Jones         }
476b0ea6c98SPeter Maydell         if (s->cookie) {
477b0ea6c98SPeter Maydell             if (curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie)) {
478b0ea6c98SPeter Maydell                 goto err;
479b0ea6c98SPeter Maydell             }
480b0ea6c98SPeter Maydell         }
481b0ea6c98SPeter Maydell         if (curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout) ||
4829e550b32SMatthew Booth             curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
483b0ea6c98SPeter Maydell                              (void *)curl_read_cb) ||
484b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state) ||
485b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state) ||
486b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1) ||
487b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1) ||
488b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1) ||
489b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg) ||
490b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1)) {
491b0ea6c98SPeter Maydell             goto err;
492b0ea6c98SPeter Maydell         }
4931bff9606SDaniel P. Berrange         if (s->username) {
494b0ea6c98SPeter Maydell             if (curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username)) {
495b0ea6c98SPeter Maydell                 goto err;
496b0ea6c98SPeter Maydell             }
4971bff9606SDaniel P. Berrange         }
4981bff9606SDaniel P. Berrange         if (s->password) {
499b0ea6c98SPeter Maydell             if (curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password)) {
500b0ea6c98SPeter Maydell                 goto err;
501b0ea6c98SPeter Maydell             }
5021bff9606SDaniel P. Berrange         }
5031bff9606SDaniel P. Berrange         if (s->proxyusername) {
504b0ea6c98SPeter Maydell             if (curl_easy_setopt(state->curl,
505b0ea6c98SPeter Maydell                                  CURLOPT_PROXYUSERNAME, s->proxyusername)) {
506b0ea6c98SPeter Maydell                 goto err;
507b0ea6c98SPeter Maydell             }
5081bff9606SDaniel P. Berrange         }
5091bff9606SDaniel P. Berrange         if (s->proxypassword) {
510b0ea6c98SPeter Maydell             if (curl_easy_setopt(state->curl,
511b0ea6c98SPeter Maydell                                  CURLOPT_PROXYPASSWORD, s->proxypassword)) {
512b0ea6c98SPeter Maydell                 goto err;
513b0ea6c98SPeter Maydell             }
5141bff9606SDaniel P. Berrange         }
5151bff9606SDaniel P. Berrange 
516fb6d1bbdSStefan Hajnoczi         /* Restrict supported protocols to avoid security issues in the more
517fb6d1bbdSStefan Hajnoczi          * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
518fb6d1bbdSStefan Hajnoczi          * CVE-2013-0249.
5198a8f5840SStefan Hajnoczi          *
520e7b8d9d0SAnton Johansson          * Restricting protocols is only supported from 7.19.4 upwards. Note:
521e7b8d9d0SAnton Johansson          * version 7.85.0 deprecates CURLOPT_*PROTOCOLS in favour of a string
522e7b8d9d0SAnton Johansson          * based CURLOPT_*PROTOCOLS_STR API.
523fb6d1bbdSStefan Hajnoczi          */
524e7b8d9d0SAnton Johansson #if LIBCURL_VERSION_NUM >= 0x075500
525e7b8d9d0SAnton Johansson         if (curl_easy_setopt(state->curl,
526e7b8d9d0SAnton Johansson                              CURLOPT_PROTOCOLS_STR, PROTOCOLS) ||
527e7b8d9d0SAnton Johansson             curl_easy_setopt(state->curl,
528e7b8d9d0SAnton Johansson                              CURLOPT_REDIR_PROTOCOLS_STR, PROTOCOLS)) {
529e7b8d9d0SAnton Johansson             goto err;
530e7b8d9d0SAnton Johansson         }
531e7b8d9d0SAnton Johansson #elif LIBCURL_VERSION_NUM >= 0x071304
532b0ea6c98SPeter Maydell         if (curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS) ||
533b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS)) {
534b0ea6c98SPeter Maydell             goto err;
535b0ea6c98SPeter Maydell         }
5368a8f5840SStefan Hajnoczi #endif
537fb6d1bbdSStefan Hajnoczi 
538769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE
539b0ea6c98SPeter Maydell         if (curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1)) {
540b0ea6c98SPeter Maydell             goto err;
541b0ea6c98SPeter Maydell         }
542769ce76dSAlexander Graf #endif
5439e550b32SMatthew Booth     }
544769ce76dSAlexander Graf 
545769ce76dSAlexander Graf     state->s = s;
546769ce76dSAlexander Graf 
5473ce6a729SPaolo Bonzini     return 0;
548b0ea6c98SPeter Maydell 
549b0ea6c98SPeter Maydell err:
550b0ea6c98SPeter Maydell     curl_easy_cleanup(state->curl);
551b0ea6c98SPeter Maydell     state->curl = NULL;
552b0ea6c98SPeter Maydell     return -EIO;
553769ce76dSAlexander Graf }
554769ce76dSAlexander Graf 
555456af346SPaolo Bonzini /* Called with s->mutex held.  */
556769ce76dSAlexander Graf static void curl_clean_state(CURLState *s)
557769ce76dSAlexander Graf {
558675a7756SPaolo Bonzini     int j;
559675a7756SPaolo Bonzini     for (j = 0; j < CURL_NUM_ACB; j++) {
560675a7756SPaolo Bonzini         assert(!s->acb[j]);
561675a7756SPaolo Bonzini     }
562675a7756SPaolo Bonzini 
563769ce76dSAlexander Graf     if (s->s->multi)
564769ce76dSAlexander Graf         curl_multi_remove_handle(s->s->multi, s->curl);
565ff5ca166SMax Reitz 
566769ce76dSAlexander Graf     s->in_use = 0;
5672bb5c936SPaolo Bonzini 
568709f2132SPaolo Bonzini     qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex);
569769ce76dSAlexander Graf }
570769ce76dSAlexander Graf 
5718e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options,
5728e6d58cdSKevin Wolf                                 Error **errp)
573769ce76dSAlexander Graf {
57446f5ac20SEric Blake     qdict_put_str(options, CURL_BLOCK_OPT_URL, filename);
5758e6d58cdSKevin Wolf }
5768e6d58cdSKevin Wolf 
57763f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs)
57863f0f45fSStefan Hajnoczi {
57963f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
58063f0f45fSStefan Hajnoczi     int i;
58163f0f45fSStefan Hajnoczi 
582f5056b70SGan Qixin     WITH_QEMU_LOCK_GUARD(&s->mutex) {
5830f418a20SMax Reitz         curl_drop_all_sockets(s->sockets);
58463f0f45fSStefan Hajnoczi         for (i = 0; i < CURL_NUM_STATES; i++) {
58563f0f45fSStefan Hajnoczi             if (s->states[i].in_use) {
58663f0f45fSStefan Hajnoczi                 curl_clean_state(&s->states[i]);
58763f0f45fSStefan Hajnoczi             }
58863f0f45fSStefan Hajnoczi             if (s->states[i].curl) {
58963f0f45fSStefan Hajnoczi                 curl_easy_cleanup(s->states[i].curl);
59063f0f45fSStefan Hajnoczi                 s->states[i].curl = NULL;
59163f0f45fSStefan Hajnoczi             }
59263f0f45fSStefan Hajnoczi             g_free(s->states[i].orig_buf);
59363f0f45fSStefan Hajnoczi             s->states[i].orig_buf = NULL;
59463f0f45fSStefan Hajnoczi         }
59563f0f45fSStefan Hajnoczi         if (s->multi) {
59663f0f45fSStefan Hajnoczi             curl_multi_cleanup(s->multi);
59763f0f45fSStefan Hajnoczi             s->multi = NULL;
59863f0f45fSStefan Hajnoczi         }
599f5056b70SGan Qixin     }
60063f0f45fSStefan Hajnoczi 
60163f0f45fSStefan Hajnoczi     timer_del(&s->timer);
60263f0f45fSStefan Hajnoczi }
60363f0f45fSStefan Hajnoczi 
60463f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs,
60563f0f45fSStefan Hajnoczi                                     AioContext *new_context)
60663f0f45fSStefan Hajnoczi {
60763f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
60863f0f45fSStefan Hajnoczi 
60963f0f45fSStefan Hajnoczi     aio_timer_init(new_context, &s->timer,
61063f0f45fSStefan Hajnoczi                    QEMU_CLOCK_REALTIME, SCALE_NS,
61163f0f45fSStefan Hajnoczi                    curl_multi_timeout_do, s);
61263f0f45fSStefan Hajnoczi 
61363f0f45fSStefan Hajnoczi     assert(!s->multi);
61463f0f45fSStefan Hajnoczi     s->multi = curl_multi_init();
61563f0f45fSStefan Hajnoczi     s->aio_context = new_context;
61663f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
61763f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
61863f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
61963f0f45fSStefan Hajnoczi }
62063f0f45fSStefan Hajnoczi 
6218e6d58cdSKevin Wolf static QemuOptsList runtime_opts = {
6228e6d58cdSKevin Wolf     .name = "curl",
6238e6d58cdSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
6248e6d58cdSKevin Wolf     .desc = {
6258e6d58cdSKevin Wolf         {
626e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_URL,
6278e6d58cdSKevin Wolf             .type = QEMU_OPT_STRING,
6288e6d58cdSKevin Wolf             .help = "URL to open",
6298e6d58cdSKevin Wolf         },
6308e6d58cdSKevin Wolf         {
631e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_READAHEAD,
6328e6d58cdSKevin Wolf             .type = QEMU_OPT_SIZE,
6338e6d58cdSKevin Wolf             .help = "Readahead size",
6348e6d58cdSKevin Wolf         },
63597a3ea57SMatthew Booth         {
63697a3ea57SMatthew Booth             .name = CURL_BLOCK_OPT_SSLVERIFY,
63797a3ea57SMatthew Booth             .type = QEMU_OPT_BOOL,
63897a3ea57SMatthew Booth             .help = "Verify SSL certificate"
63997a3ea57SMatthew Booth         },
640212aefaaSDaniel Henrique Barboza         {
641212aefaaSDaniel Henrique Barboza             .name = CURL_BLOCK_OPT_TIMEOUT,
642212aefaaSDaniel Henrique Barboza             .type = QEMU_OPT_NUMBER,
643212aefaaSDaniel Henrique Barboza             .help = "Curl timeout"
644212aefaaSDaniel Henrique Barboza         },
645a94f83d9SRichard W.M. Jones         {
646a94f83d9SRichard W.M. Jones             .name = CURL_BLOCK_OPT_COOKIE,
647a94f83d9SRichard W.M. Jones             .type = QEMU_OPT_STRING,
648a94f83d9SRichard W.M. Jones             .help = "Pass the cookie or list of cookies with each request"
649a94f83d9SRichard W.M. Jones         },
6501bff9606SDaniel P. Berrange         {
651327c8ebdSPeter Krempa             .name = CURL_BLOCK_OPT_COOKIE_SECRET,
652327c8ebdSPeter Krempa             .type = QEMU_OPT_STRING,
653327c8ebdSPeter Krempa             .help = "ID of secret used as cookie passed with each request"
654327c8ebdSPeter Krempa         },
655327c8ebdSPeter Krempa         {
6561bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_USERNAME,
6571bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
6581bff9606SDaniel P. Berrange             .help = "Username for HTTP auth"
6591bff9606SDaniel P. Berrange         },
6601bff9606SDaniel P. Berrange         {
6611bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_PASSWORD_SECRET,
6621bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
6631bff9606SDaniel P. Berrange             .help = "ID of secret used as password for HTTP auth",
6641bff9606SDaniel P. Berrange         },
6651bff9606SDaniel P. Berrange         {
6661bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_PROXY_USERNAME,
6671bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
6681bff9606SDaniel P. Berrange             .help = "Username for HTTP proxy auth"
6691bff9606SDaniel P. Berrange         },
6701bff9606SDaniel P. Berrange         {
6711bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
6721bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
6731bff9606SDaniel P. Berrange             .help = "ID of secret used as password for HTTP proxy auth",
6741bff9606SDaniel P. Berrange         },
6758e6d58cdSKevin Wolf         { /* end of list */ }
6768e6d58cdSKevin Wolf     },
6778e6d58cdSKevin Wolf };
6788e6d58cdSKevin Wolf 
6791bff9606SDaniel P. Berrange 
680015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags,
681015a1036SMax Reitz                      Error **errp)
6828e6d58cdSKevin Wolf {
6838e6d58cdSKevin Wolf     BDRVCURLState *s = bs->opaque;
6848e6d58cdSKevin Wolf     CURLState *state = NULL;
6858e6d58cdSKevin Wolf     QemuOpts *opts;
6868e6d58cdSKevin Wolf     const char *file;
687a94f83d9SRichard W.M. Jones     const char *cookie;
688327c8ebdSPeter Krempa     const char *cookie_secret;
689e7b8d9d0SAnton Johansson     /* CURL >= 7.55.0 uses curl_off_t for content length instead of a double */
690e7b8d9d0SAnton Johansson #if LIBCURL_VERSION_NUM >= 0x073700
691e7b8d9d0SAnton Johansson     curl_off_t cl;
692e7b8d9d0SAnton Johansson #else
693e7b8d9d0SAnton Johansson     double cl;
694e7b8d9d0SAnton Johansson #endif
6951bff9606SDaniel P. Berrange     const char *secretid;
69634634ca2SMax Reitz     const char *protocol_delimiter;
6972d25964dSJeff Cody     int ret;
6988e6d58cdSKevin Wolf 
699018f9deaSKevin Wolf     bdrv_graph_rdlock_main_loop();
7006ceef36aSKevin Wolf     ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes",
7016ceef36aSKevin Wolf                                     errp);
702018f9deaSKevin Wolf     bdrv_graph_rdunlock_main_loop();
7036ceef36aSKevin Wolf     if (ret < 0) {
7046ceef36aSKevin Wolf         return ret;
705a7cea2baSRichard W.M. Jones     }
706a7cea2baSRichard W.M. Jones 
7072d25964dSJeff Cody     if (!libcurl_initialized) {
7082d25964dSJeff Cody         ret = curl_global_init(CURL_GLOBAL_ALL);
7092d25964dSJeff Cody         if (ret) {
7102d25964dSJeff Cody             error_setg(errp, "libcurl initialization failed with %d", ret);
7112d25964dSJeff Cody             return -EIO;
7122d25964dSJeff Cody         }
7132d25964dSJeff Cody         libcurl_initialized = true;
7142d25964dSJeff Cody     }
7152d25964dSJeff Cody 
716456af346SPaolo Bonzini     qemu_mutex_init(&s->mutex);
71787ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
718668f62ecSMarkus Armbruster     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
7198e6d58cdSKevin Wolf         goto out_noclean;
7208e6d58cdSKevin Wolf     }
7218e6d58cdSKevin Wolf 
722e3542c67SMatthew Booth     s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
723712b64e8SMax Reitz                                           CURL_BLOCK_OPT_READAHEAD_DEFAULT);
724c76f4952SNolan     if ((s->readahead_size & 0x1ff) != 0) {
7252a94fee3SPaolo Bonzini         error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
726c76f4952SNolan                    s->readahead_size);
727c76f4952SNolan         goto out_noclean;
728c76f4952SNolan     }
729c76f4952SNolan 
730212aefaaSDaniel Henrique Barboza     s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
731712b64e8SMax Reitz                                      CURL_BLOCK_OPT_TIMEOUT_DEFAULT);
732f76faedaSRichard W.M. Jones     if (s->timeout > CURL_TIMEOUT_MAX) {
733f76faedaSRichard W.M. Jones         error_setg(errp, "timeout parameter is too large or negative");
734f76faedaSRichard W.M. Jones         goto out_noclean;
735f76faedaSRichard W.M. Jones     }
736212aefaaSDaniel Henrique Barboza 
737712b64e8SMax Reitz     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY,
738712b64e8SMax Reitz                                      CURL_BLOCK_OPT_SSLVERIFY_DEFAULT);
73997a3ea57SMatthew Booth 
740a94f83d9SRichard W.M. Jones     cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
741327c8ebdSPeter Krempa     cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET);
742327c8ebdSPeter Krempa 
743327c8ebdSPeter Krempa     if (cookie && cookie_secret) {
744327c8ebdSPeter Krempa         error_setg(errp,
745327c8ebdSPeter Krempa                    "curl driver cannot handle both cookie and cookie secret");
746327c8ebdSPeter Krempa         goto out_noclean;
747327c8ebdSPeter Krempa     }
748327c8ebdSPeter Krempa 
749327c8ebdSPeter Krempa     if (cookie_secret) {
750327c8ebdSPeter Krempa         s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp);
751327c8ebdSPeter Krempa         if (!s->cookie) {
752327c8ebdSPeter Krempa             goto out_noclean;
753327c8ebdSPeter Krempa         }
754327c8ebdSPeter Krempa     } else {
755a94f83d9SRichard W.M. Jones         s->cookie = g_strdup(cookie);
756327c8ebdSPeter Krempa     }
757a94f83d9SRichard W.M. Jones 
758e3542c67SMatthew Booth     file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
7598e6d58cdSKevin Wolf     if (file == NULL) {
7602a94fee3SPaolo Bonzini         error_setg(errp, "curl block driver requires an 'url' option");
7618e6d58cdSKevin Wolf         goto out_noclean;
7628e6d58cdSKevin Wolf     }
7638e6d58cdSKevin Wolf 
76434634ca2SMax Reitz     if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) ||
76534634ca2SMax Reitz         !strstart(protocol_delimiter, "://", NULL))
76634634ca2SMax Reitz     {
76734634ca2SMax Reitz         error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not "
76834634ca2SMax Reitz                    "start with '%s://')", bs->drv->protocol_name, file,
76934634ca2SMax Reitz                    bs->drv->protocol_name);
77034634ca2SMax Reitz         goto out_noclean;
77134634ca2SMax Reitz     }
77234634ca2SMax Reitz 
7731bff9606SDaniel P. Berrange     s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME));
7741bff9606SDaniel P. Berrange     secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET);
7751bff9606SDaniel P. Berrange 
7761bff9606SDaniel P. Berrange     if (secretid) {
7771bff9606SDaniel P. Berrange         s->password = qcrypto_secret_lookup_as_utf8(secretid, errp);
7781bff9606SDaniel P. Berrange         if (!s->password) {
7791bff9606SDaniel P. Berrange             goto out_noclean;
7801bff9606SDaniel P. Berrange         }
7811bff9606SDaniel P. Berrange     }
7821bff9606SDaniel P. Berrange 
7831bff9606SDaniel P. Berrange     s->proxyusername = g_strdup(
7841bff9606SDaniel P. Berrange         qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME));
7851bff9606SDaniel P. Berrange     secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET);
7861bff9606SDaniel P. Berrange     if (secretid) {
7871bff9606SDaniel P. Berrange         s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp);
7881bff9606SDaniel P. Berrange         if (!s->proxypassword) {
7891bff9606SDaniel P. Berrange             goto out_noclean;
7901bff9606SDaniel P. Berrange         }
7911bff9606SDaniel P. Berrange     }
7921bff9606SDaniel P. Berrange 
793ed2a66deSLaurent Vivier     trace_curl_open(file);
794709f2132SPaolo Bonzini     qemu_co_queue_init(&s->free_state_waitq);
79563f0f45fSStefan Hajnoczi     s->aio_context = bdrv_get_aio_context(bs);
7968e6d58cdSKevin Wolf     s->url = g_strdup(file);
7970f418a20SMax Reitz     s->sockets = g_hash_table_new_full(NULL, NULL, NULL, g_free);
798456af346SPaolo Bonzini     qemu_mutex_lock(&s->mutex);
7993ce6a729SPaolo Bonzini     state = curl_find_state(s);
800456af346SPaolo Bonzini     qemu_mutex_unlock(&s->mutex);
8013ce6a729SPaolo Bonzini     if (!state) {
802769ce76dSAlexander Graf         goto out_noclean;
8033ce6a729SPaolo Bonzini     }
804769ce76dSAlexander Graf 
805769ce76dSAlexander Graf     // Get file size
806769ce76dSAlexander Graf 
8073ce6a729SPaolo Bonzini     if (curl_init_state(s, state) < 0) {
8082ea7dfcdSPeter Maydell         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
8092ea7dfcdSPeter Maydell                 "curl library initialization failed.");
8103ce6a729SPaolo Bonzini         goto out;
8113ce6a729SPaolo Bonzini     }
8123ce6a729SPaolo Bonzini 
8133494d650SFam Zheng     s->accept_range = false;
814b0ea6c98SPeter Maydell     if (curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1) ||
815b0ea6c98SPeter Maydell         curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, curl_header_cb) ||
816b0ea6c98SPeter Maydell         curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s)) {
817b0ea6c98SPeter Maydell         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
818b0ea6c98SPeter Maydell                 "curl library initialization failed.");
819b0ea6c98SPeter Maydell         goto out;
820b0ea6c98SPeter Maydell     }
821769ce76dSAlexander Graf     if (curl_easy_perform(state->curl))
822769ce76dSAlexander Graf         goto out;
823e7b8d9d0SAnton Johansson     /* CURL 7.55.0 deprecates CURLINFO_CONTENT_LENGTH_DOWNLOAD in favour of
824e7b8d9d0SAnton Johansson      * the *_T version which returns a more sensible type for content length.
825e7b8d9d0SAnton Johansson      */
826e7b8d9d0SAnton Johansson #if LIBCURL_VERSION_NUM >= 0x073700
827e7b8d9d0SAnton Johansson     if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl)) {
828769ce76dSAlexander Graf         goto out;
829a41c4578STomáš Golembiovský     }
830e7b8d9d0SAnton Johansson #else
831e7b8d9d0SAnton Johansson     if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl)) {
832e7b8d9d0SAnton Johansson         goto out;
833e7b8d9d0SAnton Johansson     }
834e7b8d9d0SAnton Johansson #endif
835a41c4578STomáš Golembiovský     /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
836a41c4578STomáš Golembiovský      * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
83750d6a8a3SStefan Weil      * known and zero if it is really zero-length file. */
838a41c4578STomáš Golembiovský #if LIBCURL_VERSION_NUM >= 0x071304
839e7b8d9d0SAnton Johansson     if (cl < 0) {
840a41c4578STomáš Golembiovský         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
841a41c4578STomáš Golembiovský                 "Server didn't report file size.");
842a41c4578STomáš Golembiovský         goto out;
843a41c4578STomáš Golembiovský     }
844a41c4578STomáš Golembiovský #else
845e7b8d9d0SAnton Johansson     if (cl <= 0) {
846a41c4578STomáš Golembiovský         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
847a41c4578STomáš Golembiovský                 "Unknown file size or zero-length file.");
848a41c4578STomáš Golembiovský         goto out;
849a41c4578STomáš Golembiovský     }
850a41c4578STomáš Golembiovský #endif
851a41c4578STomáš Golembiovský 
852e7b8d9d0SAnton Johansson     s->len = cl;
853a41c4578STomáš Golembiovský 
8543494d650SFam Zheng     if ((!strncasecmp(s->url, "http://", strlen("http://"))
8553494d650SFam Zheng         || !strncasecmp(s->url, "https://", strlen("https://")))
8563494d650SFam Zheng         && !s->accept_range) {
8573494d650SFam Zheng         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
8583494d650SFam Zheng                 "Server does not support 'range' (byte ranges).");
8593494d650SFam Zheng         goto out;
8603494d650SFam Zheng     }
861ed2a66deSLaurent Vivier     trace_curl_open_size(s->len);
862769ce76dSAlexander Graf 
863456af346SPaolo Bonzini     qemu_mutex_lock(&s->mutex);
864769ce76dSAlexander Graf     curl_clean_state(state);
865456af346SPaolo Bonzini     qemu_mutex_unlock(&s->mutex);
866769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
867769ce76dSAlexander Graf     state->curl = NULL;
868769ce76dSAlexander Graf 
86963f0f45fSStefan Hajnoczi     curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
870769ce76dSAlexander Graf 
8718e6d58cdSKevin Wolf     qemu_opts_del(opts);
872769ce76dSAlexander Graf     return 0;
873769ce76dSAlexander Graf 
874769ce76dSAlexander Graf out:
875acd7fdc6SMaria Kustova     error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
876769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
877769ce76dSAlexander Graf     state->curl = NULL;
878769ce76dSAlexander Graf out_noclean:
879456af346SPaolo Bonzini     qemu_mutex_destroy(&s->mutex);
880a94f83d9SRichard W.M. Jones     g_free(s->cookie);
8818e6d58cdSKevin Wolf     g_free(s->url);
882996922deSJeff Cody     g_free(s->username);
883996922deSJeff Cody     g_free(s->proxyusername);
884996922deSJeff Cody     g_free(s->proxypassword);
8851e84cf79SHanna Czenczek     if (s->sockets) {
8860f418a20SMax Reitz         curl_drop_all_sockets(s->sockets);
8870f418a20SMax Reitz         g_hash_table_destroy(s->sockets);
8881e84cf79SHanna Czenczek     }
8898e6d58cdSKevin Wolf     qemu_opts_del(opts);
890769ce76dSAlexander Graf     return -EINVAL;
891769ce76dSAlexander Graf }
892769ce76dSAlexander Graf 
8939bae2acaSPaolo Bonzini static void coroutine_fn curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)
894769ce76dSAlexander Graf {
895769ce76dSAlexander Graf     CURLState *state;
896b69cdef8SMatthew Booth     int running;
897769ce76dSAlexander Graf 
8981919631eSPaolo Bonzini     BDRVCURLState *s = bs->opaque;
899769ce76dSAlexander Graf 
9002125e5eaSPaolo Bonzini     uint64_t start = acb->offset;
9012125e5eaSPaolo Bonzini     uint64_t end;
902769ce76dSAlexander Graf 
903ba3186c4SPaolo Bonzini     qemu_mutex_lock(&s->mutex);
9041919631eSPaolo Bonzini 
905769ce76dSAlexander Graf     // In case we have the requested data already (e.g. read-ahead),
906769ce76dSAlexander Graf     // we can just call the callback and be done.
90728256d82SPaolo Bonzini     if (curl_find_buf(s, start, acb->bytes, acb)) {
90834db05e7SPaolo Bonzini         goto out;
909769ce76dSAlexander Graf     }
910769ce76dSAlexander Graf 
911769ce76dSAlexander Graf     // No cache found, so let's start a new request
9123ce6a729SPaolo Bonzini     for (;;) {
9133ce6a729SPaolo Bonzini         state = curl_find_state(s);
9143ce6a729SPaolo Bonzini         if (state) {
9153ce6a729SPaolo Bonzini             break;
9163ce6a729SPaolo Bonzini         }
917709f2132SPaolo Bonzini         qemu_co_queue_wait(&s->free_state_waitq, &s->mutex);
9183ce6a729SPaolo Bonzini     }
9193ce6a729SPaolo Bonzini 
9203ce6a729SPaolo Bonzini     if (curl_init_state(s, state) < 0) {
9213ce6a729SPaolo Bonzini         curl_clean_state(state);
92228256d82SPaolo Bonzini         acb->ret = -EIO;
9231919631eSPaolo Bonzini         goto out;
924363c3c85SNick Thomas     }
925769ce76dSAlexander Graf 
926769ce76dSAlexander Graf     acb->start = 0;
9272125e5eaSPaolo Bonzini     acb->end = MIN(acb->bytes, s->len - start);
928769ce76dSAlexander Graf 
929769ce76dSAlexander Graf     state->buf_off = 0;
9307267c094SAnthony Liguori     g_free(state->orig_buf);
931769ce76dSAlexander Graf     state->buf_start = start;
9324e504535SMax Reitz     state->buf_len = MIN(acb->end + s->readahead_size, s->len - start);
9334e504535SMax Reitz     end = start + state->buf_len - 1;
9348dc7a772SKevin Wolf     state->orig_buf = g_try_malloc(state->buf_len);
9358dc7a772SKevin Wolf     if (state->buf_len && state->orig_buf == NULL) {
9368dc7a772SKevin Wolf         curl_clean_state(state);
93728256d82SPaolo Bonzini         acb->ret = -ENOMEM;
9381919631eSPaolo Bonzini         goto out;
9398dc7a772SKevin Wolf     }
940769ce76dSAlexander Graf     state->acb[0] = acb;
941769ce76dSAlexander Graf 
9422125e5eaSPaolo Bonzini     snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end);
943ed2a66deSLaurent Vivier     trace_curl_setup_preadv(acb->bytes, start, state->range);
944b0ea6c98SPeter Maydell     if (curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range) ||
945b0ea6c98SPeter Maydell         curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) {
946c34dc07fSMax Reitz         state->acb[0] = NULL;
947c34dc07fSMax Reitz         acb->ret = -EIO;
948c34dc07fSMax Reitz 
949c34dc07fSMax Reitz         curl_clean_state(state);
950c34dc07fSMax Reitz         goto out;
951c34dc07fSMax Reitz     }
952769ce76dSAlexander Graf 
953b69cdef8SMatthew Booth     /* Tell curl it needs to kick things off */
954b69cdef8SMatthew Booth     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
9551919631eSPaolo Bonzini 
9561919631eSPaolo Bonzini out:
957ba3186c4SPaolo Bonzini     qemu_mutex_unlock(&s->mutex);
958363c3c85SNick Thomas }
959363c3c85SNick Thomas 
96028256d82SPaolo Bonzini static int coroutine_fn curl_co_preadv(BlockDriverState *bs,
961f7ef38ddSVladimir Sementsov-Ogievskiy         int64_t offset, int64_t bytes, QEMUIOVector *qiov,
962f7ef38ddSVladimir Sementsov-Ogievskiy         BdrvRequestFlags flags)
963363c3c85SNick Thomas {
96428256d82SPaolo Bonzini     CURLAIOCB acb = {
96528256d82SPaolo Bonzini         .co = qemu_coroutine_self(),
96628256d82SPaolo Bonzini         .ret = -EINPROGRESS,
96728256d82SPaolo Bonzini         .qiov = qiov,
96828256d82SPaolo Bonzini         .offset = offset,
96928256d82SPaolo Bonzini         .bytes = bytes
97028256d82SPaolo Bonzini     };
971363c3c85SNick Thomas 
97228256d82SPaolo Bonzini     curl_setup_preadv(bs, &acb);
97328256d82SPaolo Bonzini     while (acb.ret == -EINPROGRESS) {
97428256d82SPaolo Bonzini         qemu_coroutine_yield();
97528256d82SPaolo Bonzini     }
97628256d82SPaolo Bonzini     return acb.ret;
977769ce76dSAlexander Graf }
978769ce76dSAlexander Graf 
979769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs)
980769ce76dSAlexander Graf {
981769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
982769ce76dSAlexander Graf 
983ed2a66deSLaurent Vivier     trace_curl_close();
98463f0f45fSStefan Hajnoczi     curl_detach_aio_context(bs);
985ba3186c4SPaolo Bonzini     qemu_mutex_destroy(&s->mutex);
986031fd1beSPeter Maydell 
9870f418a20SMax Reitz     g_hash_table_destroy(s->sockets);
988a94f83d9SRichard W.M. Jones     g_free(s->cookie);
98945724d6dSStefan Weil     g_free(s->url);
990996922deSJeff Cody     g_free(s->username);
991996922deSJeff Cody     g_free(s->proxyusername);
992996922deSJeff Cody     g_free(s->proxypassword);
993769ce76dSAlexander Graf }
994769ce76dSAlexander Graf 
995c86422c5SEmanuele Giuseppe Esposito static int64_t coroutine_fn curl_co_getlength(BlockDriverState *bs)
996769ce76dSAlexander Graf {
997769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
998769ce76dSAlexander Graf     return s->len;
999769ce76dSAlexander Graf }
1000769ce76dSAlexander Graf 
1001937c007bSMax Reitz static void curl_refresh_filename(BlockDriverState *bs)
1002937c007bSMax Reitz {
1003937c007bSMax Reitz     BDRVCURLState *s = bs->opaque;
1004937c007bSMax Reitz 
1005937c007bSMax Reitz     /* "readahead" and "timeout" do not change the guest-visible data,
1006937c007bSMax Reitz      * so ignore them */
1007937c007bSMax Reitz     if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT ||
1008937c007bSMax Reitz         s->cookie || s->username || s->password || s->proxyusername ||
1009937c007bSMax Reitz         s->proxypassword)
1010937c007bSMax Reitz     {
1011937c007bSMax Reitz         return;
1012937c007bSMax Reitz     }
1013937c007bSMax Reitz 
1014937c007bSMax Reitz     pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url);
1015937c007bSMax Reitz }
1016937c007bSMax Reitz 
1017937c007bSMax Reitz 
10182654267cSMax Reitz static const char *const curl_strong_runtime_opts[] = {
10192654267cSMax Reitz     CURL_BLOCK_OPT_URL,
10202654267cSMax Reitz     CURL_BLOCK_OPT_SSLVERIFY,
10212654267cSMax Reitz     CURL_BLOCK_OPT_COOKIE,
10222654267cSMax Reitz     CURL_BLOCK_OPT_COOKIE_SECRET,
10232654267cSMax Reitz     CURL_BLOCK_OPT_USERNAME,
10242654267cSMax Reitz     CURL_BLOCK_OPT_PASSWORD_SECRET,
10252654267cSMax Reitz     CURL_BLOCK_OPT_PROXY_USERNAME,
10262654267cSMax Reitz     CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
10272654267cSMax Reitz 
10282654267cSMax Reitz     NULL
10292654267cSMax Reitz };
10302654267cSMax Reitz 
1031769ce76dSAlexander Graf static BlockDriver bdrv_http = {
1032769ce76dSAlexander Graf     .format_name                = "http",
1033769ce76dSAlexander Graf     .protocol_name              = "http",
1034769ce76dSAlexander Graf 
1035769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
10368e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
1037*44b424dcSPaolo Bonzini     .bdrv_open                  = curl_open,
1038769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
1039c86422c5SEmanuele Giuseppe Esposito     .bdrv_co_getlength          = curl_co_getlength,
1040769ce76dSAlexander Graf 
104128256d82SPaolo Bonzini     .bdrv_co_preadv             = curl_co_preadv,
104263f0f45fSStefan Hajnoczi 
104363f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
104463f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
10452654267cSMax Reitz 
1046937c007bSMax Reitz     .bdrv_refresh_filename      = curl_refresh_filename,
10472654267cSMax Reitz     .strong_runtime_opts        = curl_strong_runtime_opts,
1048769ce76dSAlexander Graf };
1049769ce76dSAlexander Graf 
1050769ce76dSAlexander Graf static BlockDriver bdrv_https = {
1051769ce76dSAlexander Graf     .format_name                = "https",
1052769ce76dSAlexander Graf     .protocol_name              = "https",
1053769ce76dSAlexander Graf 
1054769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
10558e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
1056*44b424dcSPaolo Bonzini     .bdrv_open                  = curl_open,
1057769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
1058c86422c5SEmanuele Giuseppe Esposito     .bdrv_co_getlength          = curl_co_getlength,
1059769ce76dSAlexander Graf 
106028256d82SPaolo Bonzini     .bdrv_co_preadv             = curl_co_preadv,
106163f0f45fSStefan Hajnoczi 
106263f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
106363f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
10642654267cSMax Reitz 
1065937c007bSMax Reitz     .bdrv_refresh_filename      = curl_refresh_filename,
10662654267cSMax Reitz     .strong_runtime_opts        = curl_strong_runtime_opts,
1067769ce76dSAlexander Graf };
1068769ce76dSAlexander Graf 
1069769ce76dSAlexander Graf static BlockDriver bdrv_ftp = {
1070769ce76dSAlexander Graf     .format_name                = "ftp",
1071769ce76dSAlexander Graf     .protocol_name              = "ftp",
1072769ce76dSAlexander Graf 
1073769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
10748e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
1075*44b424dcSPaolo Bonzini     .bdrv_open                  = curl_open,
1076769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
1077c86422c5SEmanuele Giuseppe Esposito     .bdrv_co_getlength          = curl_co_getlength,
1078769ce76dSAlexander Graf 
107928256d82SPaolo Bonzini     .bdrv_co_preadv             = curl_co_preadv,
108063f0f45fSStefan Hajnoczi 
108163f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
108263f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
10832654267cSMax Reitz 
1084937c007bSMax Reitz     .bdrv_refresh_filename      = curl_refresh_filename,
10852654267cSMax Reitz     .strong_runtime_opts        = curl_strong_runtime_opts,
1086769ce76dSAlexander Graf };
1087769ce76dSAlexander Graf 
1088769ce76dSAlexander Graf static BlockDriver bdrv_ftps = {
1089769ce76dSAlexander Graf     .format_name                = "ftps",
1090769ce76dSAlexander Graf     .protocol_name              = "ftps",
1091769ce76dSAlexander Graf 
1092769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
10938e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
1094*44b424dcSPaolo Bonzini     .bdrv_open                  = curl_open,
1095769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
1096c86422c5SEmanuele Giuseppe Esposito     .bdrv_co_getlength          = curl_co_getlength,
1097769ce76dSAlexander Graf 
109828256d82SPaolo Bonzini     .bdrv_co_preadv             = curl_co_preadv,
109963f0f45fSStefan Hajnoczi 
110063f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
110163f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
11022654267cSMax Reitz 
1103937c007bSMax Reitz     .bdrv_refresh_filename      = curl_refresh_filename,
11042654267cSMax Reitz     .strong_runtime_opts        = curl_strong_runtime_opts,
1105769ce76dSAlexander Graf };
1106769ce76dSAlexander Graf 
1107769ce76dSAlexander Graf static void curl_block_init(void)
1108769ce76dSAlexander Graf {
1109769ce76dSAlexander Graf     bdrv_register(&bdrv_http);
1110769ce76dSAlexander Graf     bdrv_register(&bdrv_https);
1111769ce76dSAlexander Graf     bdrv_register(&bdrv_ftp);
1112769ce76dSAlexander Graf     bdrv_register(&bdrv_ftps);
1113769ce76dSAlexander Graf }
1114769ce76dSAlexander Graf 
1115769ce76dSAlexander Graf block_init(curl_block_init);
1116