xref: /openbmc/qemu/block/curl.c (revision 2abc22e6)
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 
curl_drop_socket(void * key,void * value,void * opaque)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 
curl_drop_all_sockets(GHashTable * sockets)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.  */
curl_timer_cb(CURLM * multi,long timeout_ms,void * opaque)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.  */
curl_sock_cb(CURL * curl,curl_socket_t fd,int action,void * userp,void * sp)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.  */
curl_header_cb(void * ptr,size_t size,size_t nmemb,void * opaque)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;
213*2abc22e6SMichael Tokarev     const char *p = ptr;
214*2abc22e6SMichael Tokarev     const char *end = p + realsize;
215*2abc22e6SMichael Tokarev     const char *t = "accept-ranges : bytes "; /* A lowercase template */
216769ce76dSAlexander Graf 
217*2abc22e6SMichael Tokarev     /* check if header matches the "t" template */
218*2abc22e6SMichael Tokarev     for (;;) {
219*2abc22e6SMichael Tokarev         if (*t == ' ') { /* space in t matches any amount of isspace in p */
220*2abc22e6SMichael Tokarev             if (p < end && g_ascii_isspace(*p)) {
221*2abc22e6SMichael Tokarev                 ++p;
222*2abc22e6SMichael Tokarev             } else {
223*2abc22e6SMichael Tokarev                 ++t;
224*2abc22e6SMichael Tokarev             }
225*2abc22e6SMichael Tokarev         } else if (*t && p < end && *t == g_ascii_tolower(*p)) {
226*2abc22e6SMichael Tokarev             ++p, ++t;
227*2abc22e6SMichael Tokarev         } else {
228*2abc22e6SMichael Tokarev             break;
229*2abc22e6SMichael Tokarev         }
2307788a319SDavid Edmondson     }
2317788a319SDavid Edmondson 
232*2abc22e6SMichael Tokarev     if (!*t && p == end) { /* if we managed to reach ends of both strings */
2333494d650SFam Zheng         s->accept_range = true;
2340bfcd599SBlue Swirl     }
235769ce76dSAlexander Graf 
236769ce76dSAlexander Graf     return realsize;
237769ce76dSAlexander Graf }
238769ce76dSAlexander Graf 
23934db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held.  */
curl_read_cb(void * ptr,size_t size,size_t nmemb,void * opaque)240769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
241769ce76dSAlexander Graf {
242769ce76dSAlexander Graf     CURLState *s = ((CURLState*)opaque);
243769ce76dSAlexander Graf     size_t realsize = size * nmemb;
244769ce76dSAlexander Graf 
245ed2a66deSLaurent Vivier     trace_curl_read_cb(realsize);
246769ce76dSAlexander Graf 
2474e767657SMax Reitz     if (!s || !s->orig_buf) {
2484e767657SMax Reitz         goto read_end;
2494e767657SMax Reitz     }
250769ce76dSAlexander Graf 
2516d4b9e55SFam Zheng     if (s->buf_off >= s->buf_len) {
2526d4b9e55SFam Zheng         /* buffer full, read nothing */
2534e767657SMax Reitz         goto read_end;
2546d4b9e55SFam Zheng     }
2556d4b9e55SFam Zheng     realsize = MIN(realsize, s->buf_len - s->buf_off);
256769ce76dSAlexander Graf     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
257769ce76dSAlexander Graf     s->buf_off += realsize;
258769ce76dSAlexander Graf 
2594e767657SMax Reitz read_end:
2604e767657SMax Reitz     /* curl will error out if we do not return this value */
2614e767657SMax Reitz     return size * nmemb;
262769ce76dSAlexander Graf }
263769ce76dSAlexander Graf 
264456af346SPaolo Bonzini /* Called with s->mutex held.  */
curl_find_buf(BDRVCURLState * s,uint64_t start,uint64_t len,CURLAIOCB * acb)26528256d82SPaolo Bonzini static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
266769ce76dSAlexander Graf                           CURLAIOCB *acb)
267769ce76dSAlexander Graf {
268769ce76dSAlexander Graf     int i;
2692125e5eaSPaolo Bonzini     uint64_t end = start + len;
2702125e5eaSPaolo Bonzini     uint64_t clamped_end = MIN(end, s->len);
2712125e5eaSPaolo Bonzini     uint64_t clamped_len = clamped_end - start;
272769ce76dSAlexander Graf 
273769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
274769ce76dSAlexander Graf         CURLState *state = &s->states[i];
2752125e5eaSPaolo Bonzini         uint64_t buf_end = (state->buf_start + state->buf_off);
2762125e5eaSPaolo Bonzini         uint64_t buf_fend = (state->buf_start + state->buf_len);
277769ce76dSAlexander Graf 
278769ce76dSAlexander Graf         if (!state->orig_buf)
279769ce76dSAlexander Graf             continue;
280769ce76dSAlexander Graf         if (!state->buf_off)
281769ce76dSAlexander Graf             continue;
282769ce76dSAlexander Graf 
283769ce76dSAlexander Graf         // Does the existing buffer cover our section?
284769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
285769ce76dSAlexander Graf             (start <= buf_end) &&
2864e504535SMax Reitz             (clamped_end >= state->buf_start) &&
2874e504535SMax Reitz             (clamped_end <= buf_end))
288769ce76dSAlexander Graf         {
289769ce76dSAlexander Graf             char *buf = state->orig_buf + (start - state->buf_start);
290769ce76dSAlexander Graf 
2914e504535SMax Reitz             qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len);
2924e504535SMax Reitz             if (clamped_len < len) {
2934e504535SMax Reitz                 qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len);
2944e504535SMax Reitz             }
29528256d82SPaolo Bonzini             acb->ret = 0;
29628256d82SPaolo Bonzini             return true;
297769ce76dSAlexander Graf         }
298769ce76dSAlexander Graf 
299769ce76dSAlexander Graf         // Wait for unfinished chunks
300b7079df4SMatthew Booth         if (state->in_use &&
301b7079df4SMatthew Booth             (start >= state->buf_start) &&
302769ce76dSAlexander Graf             (start <= buf_fend) &&
3034e504535SMax Reitz             (clamped_end >= state->buf_start) &&
3044e504535SMax Reitz             (clamped_end <= buf_fend))
305769ce76dSAlexander Graf         {
306769ce76dSAlexander Graf             int j;
307769ce76dSAlexander Graf 
308769ce76dSAlexander Graf             acb->start = start - state->buf_start;
3094e504535SMax Reitz             acb->end = acb->start + clamped_len;
310769ce76dSAlexander Graf 
311769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++) {
312769ce76dSAlexander Graf                 if (!state->acb[j]) {
313769ce76dSAlexander Graf                     state->acb[j] = acb;
31428256d82SPaolo Bonzini                     return true;
315769ce76dSAlexander Graf                 }
316769ce76dSAlexander Graf             }
317769ce76dSAlexander Graf         }
318769ce76dSAlexander Graf     }
319769ce76dSAlexander Graf 
32028256d82SPaolo Bonzini     return false;
321769ce76dSAlexander Graf }
322769ce76dSAlexander Graf 
323ba3186c4SPaolo Bonzini /* Called with s->mutex held.  */
curl_multi_check_completion(BDRVCURLState * s)324838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s)
325769ce76dSAlexander Graf {
326769ce76dSAlexander Graf     int msgs_in_queue;
327769ce76dSAlexander Graf 
328769ce76dSAlexander Graf     /* Try to find done transfers, so we can free the easy
329769ce76dSAlexander Graf      * handle again. */
3301f2cead3SMatthew Booth     for (;;) {
331769ce76dSAlexander Graf         CURLMsg *msg;
332769ce76dSAlexander Graf         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
333769ce76dSAlexander Graf 
3341f2cead3SMatthew Booth         /* Quit when there are no more completions */
335769ce76dSAlexander Graf         if (!msg)
336769ce76dSAlexander Graf             break;
337769ce76dSAlexander Graf 
3381f2cead3SMatthew Booth         if (msg->msg == CURLMSG_DONE) {
339bfb23b48SMax Reitz             int i;
340769ce76dSAlexander Graf             CURLState *state = NULL;
341bfb23b48SMax Reitz             bool error = msg->data.result != CURLE_OK;
342bfb23b48SMax Reitz 
343f6246509SMatthew Booth             curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
344f6246509SMatthew Booth                               (char **)&state);
345f785a5aeSNicholas Thomas 
346bfb23b48SMax Reitz             if (error) {
347796a060bSRichard W.M. Jones                 static int errcount = 100;
348796a060bSRichard W.M. Jones 
349796a060bSRichard W.M. Jones                 /* Don't lose the original error message from curl, since
350796a060bSRichard W.M. Jones                  * it contains extra data.
351796a060bSRichard W.M. Jones                  */
352796a060bSRichard W.M. Jones                 if (errcount > 0) {
353796a060bSRichard W.M. Jones                     error_report("curl: %s", state->errmsg);
354796a060bSRichard W.M. Jones                     if (--errcount == 0) {
355796a060bSRichard W.M. Jones                         error_report("curl: further errors suppressed");
356796a060bSRichard W.M. Jones                     }
357796a060bSRichard W.M. Jones                 }
358bfb23b48SMax Reitz             }
359796a060bSRichard W.M. Jones 
360f785a5aeSNicholas Thomas             for (i = 0; i < CURL_NUM_ACB; i++) {
361f785a5aeSNicholas Thomas                 CURLAIOCB *acb = state->acb[i];
362f785a5aeSNicholas Thomas 
363f785a5aeSNicholas Thomas                 if (acb == NULL) {
364f785a5aeSNicholas Thomas                     continue;
365f785a5aeSNicholas Thomas                 }
366f785a5aeSNicholas Thomas 
367bfb23b48SMax Reitz                 if (!error) {
368bfb23b48SMax Reitz                     /* Assert that we have read all data */
369bfb23b48SMax Reitz                     assert(state->buf_off >= acb->end);
370bfb23b48SMax Reitz 
371bfb23b48SMax Reitz                     qemu_iovec_from_buf(acb->qiov, 0,
372bfb23b48SMax Reitz                                         state->orig_buf + acb->start,
373bfb23b48SMax Reitz                                         acb->end - acb->start);
374bfb23b48SMax Reitz 
375bfb23b48SMax Reitz                     if (acb->end - acb->start < acb->bytes) {
376bfb23b48SMax Reitz                         size_t offset = acb->end - acb->start;
377bfb23b48SMax Reitz                         qemu_iovec_memset(acb->qiov, offset, 0,
378bfb23b48SMax Reitz                                           acb->bytes - offset);
379bfb23b48SMax Reitz                     }
380bfb23b48SMax Reitz                 }
381bfb23b48SMax Reitz 
382bfb23b48SMax Reitz                 acb->ret = error ? -EIO : 0;
383f785a5aeSNicholas Thomas                 state->acb[i] = NULL;
38428256d82SPaolo Bonzini                 qemu_mutex_unlock(&s->mutex);
38528256d82SPaolo Bonzini                 aio_co_wake(acb->co);
38628256d82SPaolo Bonzini                 qemu_mutex_lock(&s->mutex);
387f785a5aeSNicholas Thomas             }
388f785a5aeSNicholas Thomas 
389769ce76dSAlexander Graf             curl_clean_state(state);
390769ce76dSAlexander Graf             break;
391769ce76dSAlexander Graf         }
392769ce76dSAlexander Graf     }
393769ce76dSAlexander Graf }
394769ce76dSAlexander Graf 
395ba3186c4SPaolo Bonzini /* Called with s->mutex held.  */
curl_multi_do_locked(CURLSocket * socket)3969abaf9fcSMax Reitz static void curl_multi_do_locked(CURLSocket *socket)
397031fd1beSPeter Maydell {
3983663dca4SMax Reitz     BDRVCURLState *s = socket->s;
399031fd1beSPeter Maydell     int running;
400031fd1beSPeter Maydell     int r;
401031fd1beSPeter Maydell 
4029abaf9fcSMax Reitz     if (!s->multi) {
403031fd1beSPeter Maydell         return;
404031fd1beSPeter Maydell     }
405031fd1beSPeter Maydell 
406031fd1beSPeter Maydell     do {
4079abaf9fcSMax Reitz         r = curl_multi_socket_action(s->multi, socket->fd, 0, &running);
408031fd1beSPeter Maydell     } while (r == CURLM_CALL_MULTI_PERFORM);
409ff5ca166SMax Reitz }
410838ef602SMatthew Booth 
curl_multi_do(void * arg)4119d456654SPaolo Bonzini static void curl_multi_do(void *arg)
4129d456654SPaolo Bonzini {
4139dbad87dSMax Reitz     CURLSocket *socket = arg;
4143663dca4SMax Reitz     BDRVCURLState *s = socket->s;
4159d456654SPaolo Bonzini 
4169dbad87dSMax Reitz     qemu_mutex_lock(&s->mutex);
4179dbad87dSMax Reitz     curl_multi_do_locked(socket);
4189dbad87dSMax Reitz     curl_multi_check_completion(s);
4199dbad87dSMax Reitz     qemu_mutex_unlock(&s->mutex);
420031fd1beSPeter Maydell }
421031fd1beSPeter Maydell 
curl_multi_timeout_do(void * arg)422031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg)
423031fd1beSPeter Maydell {
424031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
425031fd1beSPeter Maydell     int running;
426031fd1beSPeter Maydell 
427031fd1beSPeter Maydell     if (!s->multi) {
428031fd1beSPeter Maydell         return;
429031fd1beSPeter Maydell     }
430031fd1beSPeter Maydell 
431ba3186c4SPaolo Bonzini     qemu_mutex_lock(&s->mutex);
432031fd1beSPeter Maydell     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
433031fd1beSPeter Maydell 
434838ef602SMatthew Booth     curl_multi_check_completion(s);
435ba3186c4SPaolo Bonzini     qemu_mutex_unlock(&s->mutex);
436031fd1beSPeter Maydell }
437031fd1beSPeter Maydell 
438456af346SPaolo Bonzini /* Called with s->mutex held.  */
curl_find_state(BDRVCURLState * s)4393ce6a729SPaolo Bonzini static CURLState *curl_find_state(BDRVCURLState *s)
440769ce76dSAlexander Graf {
441769ce76dSAlexander Graf     CURLState *state = NULL;
4423ce6a729SPaolo Bonzini     int i;
443769ce76dSAlexander Graf 
444769ce76dSAlexander Graf     for (i = 0; i < CURL_NUM_STATES; i++) {
4453ce6a729SPaolo Bonzini         if (!s->states[i].in_use) {
446769ce76dSAlexander Graf             state = &s->states[i];
447769ce76dSAlexander Graf             state->in_use = 1;
448769ce76dSAlexander Graf             break;
449769ce76dSAlexander Graf         }
450769ce76dSAlexander Graf     }
4513ce6a729SPaolo Bonzini     return state;
4523ce6a729SPaolo Bonzini }
453769ce76dSAlexander Graf 
curl_init_state(BDRVCURLState * s,CURLState * state)4543ce6a729SPaolo Bonzini static int curl_init_state(BDRVCURLState *s, CURLState *state)
4553ce6a729SPaolo Bonzini {
4569e550b32SMatthew Booth     if (!state->curl) {
457769ce76dSAlexander Graf         state->curl = curl_easy_init();
4589e550b32SMatthew Booth         if (!state->curl) {
4593ce6a729SPaolo Bonzini             return -EIO;
4609e550b32SMatthew Booth         }
461b0ea6c98SPeter Maydell         if (curl_easy_setopt(state->curl, CURLOPT_URL, s->url) ||
46297a3ea57SMatthew Booth             curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
463b0ea6c98SPeter Maydell                              (long) s->sslverify) ||
464637fa44aSRichard W.M. Jones             curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST,
465b0ea6c98SPeter Maydell                              s->sslverify ? 2L : 0L)) {
466b0ea6c98SPeter Maydell             goto err;
467a94f83d9SRichard W.M. Jones         }
468b0ea6c98SPeter Maydell         if (s->cookie) {
469b0ea6c98SPeter Maydell             if (curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie)) {
470b0ea6c98SPeter Maydell                 goto err;
471b0ea6c98SPeter Maydell             }
472b0ea6c98SPeter Maydell         }
473b0ea6c98SPeter Maydell         if (curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout) ||
4749e550b32SMatthew Booth             curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
475b0ea6c98SPeter Maydell                              (void *)curl_read_cb) ||
476b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state) ||
477b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state) ||
478b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1) ||
479b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1) ||
480b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1) ||
481b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg) ||
482b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1)) {
483b0ea6c98SPeter Maydell             goto err;
484b0ea6c98SPeter Maydell         }
4851bff9606SDaniel P. Berrange         if (s->username) {
486b0ea6c98SPeter Maydell             if (curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username)) {
487b0ea6c98SPeter Maydell                 goto err;
488b0ea6c98SPeter Maydell             }
4891bff9606SDaniel P. Berrange         }
4901bff9606SDaniel P. Berrange         if (s->password) {
491b0ea6c98SPeter Maydell             if (curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password)) {
492b0ea6c98SPeter Maydell                 goto err;
493b0ea6c98SPeter Maydell             }
4941bff9606SDaniel P. Berrange         }
4951bff9606SDaniel P. Berrange         if (s->proxyusername) {
496b0ea6c98SPeter Maydell             if (curl_easy_setopt(state->curl,
497b0ea6c98SPeter Maydell                                  CURLOPT_PROXYUSERNAME, s->proxyusername)) {
498b0ea6c98SPeter Maydell                 goto err;
499b0ea6c98SPeter Maydell             }
5001bff9606SDaniel P. Berrange         }
5011bff9606SDaniel P. Berrange         if (s->proxypassword) {
502b0ea6c98SPeter Maydell             if (curl_easy_setopt(state->curl,
503b0ea6c98SPeter Maydell                                  CURLOPT_PROXYPASSWORD, s->proxypassword)) {
504b0ea6c98SPeter Maydell                 goto err;
505b0ea6c98SPeter Maydell             }
5061bff9606SDaniel P. Berrange         }
5071bff9606SDaniel P. Berrange 
508fb6d1bbdSStefan Hajnoczi         /* Restrict supported protocols to avoid security issues in the more
509fb6d1bbdSStefan Hajnoczi          * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
510fb6d1bbdSStefan Hajnoczi          * CVE-2013-0249.
5118a8f5840SStefan Hajnoczi          *
512e7b8d9d0SAnton Johansson          * Restricting protocols is only supported from 7.19.4 upwards. Note:
513e7b8d9d0SAnton Johansson          * version 7.85.0 deprecates CURLOPT_*PROTOCOLS in favour of a string
514e7b8d9d0SAnton Johansson          * based CURLOPT_*PROTOCOLS_STR API.
515fb6d1bbdSStefan Hajnoczi          */
516e7b8d9d0SAnton Johansson #if LIBCURL_VERSION_NUM >= 0x075500
517e7b8d9d0SAnton Johansson         if (curl_easy_setopt(state->curl,
518e7b8d9d0SAnton Johansson                              CURLOPT_PROTOCOLS_STR, PROTOCOLS) ||
519e7b8d9d0SAnton Johansson             curl_easy_setopt(state->curl,
520e7b8d9d0SAnton Johansson                              CURLOPT_REDIR_PROTOCOLS_STR, PROTOCOLS)) {
521e7b8d9d0SAnton Johansson             goto err;
522e7b8d9d0SAnton Johansson         }
523e7b8d9d0SAnton Johansson #elif LIBCURL_VERSION_NUM >= 0x071304
524b0ea6c98SPeter Maydell         if (curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS) ||
525b0ea6c98SPeter Maydell             curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS)) {
526b0ea6c98SPeter Maydell             goto err;
527b0ea6c98SPeter Maydell         }
5288a8f5840SStefan Hajnoczi #endif
529fb6d1bbdSStefan Hajnoczi 
530769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE
531b0ea6c98SPeter Maydell         if (curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1)) {
532b0ea6c98SPeter Maydell             goto err;
533b0ea6c98SPeter Maydell         }
534769ce76dSAlexander Graf #endif
5359e550b32SMatthew Booth     }
536769ce76dSAlexander Graf 
537769ce76dSAlexander Graf     state->s = s;
538769ce76dSAlexander Graf 
5393ce6a729SPaolo Bonzini     return 0;
540b0ea6c98SPeter Maydell 
541b0ea6c98SPeter Maydell err:
542b0ea6c98SPeter Maydell     curl_easy_cleanup(state->curl);
543b0ea6c98SPeter Maydell     state->curl = NULL;
544b0ea6c98SPeter Maydell     return -EIO;
545769ce76dSAlexander Graf }
546769ce76dSAlexander Graf 
547456af346SPaolo Bonzini /* Called with s->mutex held.  */
curl_clean_state(CURLState * s)548769ce76dSAlexander Graf static void curl_clean_state(CURLState *s)
549769ce76dSAlexander Graf {
550675a7756SPaolo Bonzini     int j;
551675a7756SPaolo Bonzini     for (j = 0; j < CURL_NUM_ACB; j++) {
552675a7756SPaolo Bonzini         assert(!s->acb[j]);
553675a7756SPaolo Bonzini     }
554675a7756SPaolo Bonzini 
555769ce76dSAlexander Graf     if (s->s->multi)
556769ce76dSAlexander Graf         curl_multi_remove_handle(s->s->multi, s->curl);
557ff5ca166SMax Reitz 
558769ce76dSAlexander Graf     s->in_use = 0;
5592bb5c936SPaolo Bonzini 
560709f2132SPaolo Bonzini     qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex);
561769ce76dSAlexander Graf }
562769ce76dSAlexander Graf 
curl_parse_filename(const char * filename,QDict * options,Error ** errp)5638e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options,
5648e6d58cdSKevin Wolf                                 Error **errp)
565769ce76dSAlexander Graf {
56646f5ac20SEric Blake     qdict_put_str(options, CURL_BLOCK_OPT_URL, filename);
5678e6d58cdSKevin Wolf }
5688e6d58cdSKevin Wolf 
curl_detach_aio_context(BlockDriverState * bs)56963f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs)
57063f0f45fSStefan Hajnoczi {
57163f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
57263f0f45fSStefan Hajnoczi     int i;
57363f0f45fSStefan Hajnoczi 
574f5056b70SGan Qixin     WITH_QEMU_LOCK_GUARD(&s->mutex) {
5750f418a20SMax Reitz         curl_drop_all_sockets(s->sockets);
57663f0f45fSStefan Hajnoczi         for (i = 0; i < CURL_NUM_STATES; i++) {
57763f0f45fSStefan Hajnoczi             if (s->states[i].in_use) {
57863f0f45fSStefan Hajnoczi                 curl_clean_state(&s->states[i]);
57963f0f45fSStefan Hajnoczi             }
58063f0f45fSStefan Hajnoczi             if (s->states[i].curl) {
58163f0f45fSStefan Hajnoczi                 curl_easy_cleanup(s->states[i].curl);
58263f0f45fSStefan Hajnoczi                 s->states[i].curl = NULL;
58363f0f45fSStefan Hajnoczi             }
58463f0f45fSStefan Hajnoczi             g_free(s->states[i].orig_buf);
58563f0f45fSStefan Hajnoczi             s->states[i].orig_buf = NULL;
58663f0f45fSStefan Hajnoczi         }
58763f0f45fSStefan Hajnoczi         if (s->multi) {
58863f0f45fSStefan Hajnoczi             curl_multi_cleanup(s->multi);
58963f0f45fSStefan Hajnoczi             s->multi = NULL;
59063f0f45fSStefan Hajnoczi         }
591f5056b70SGan Qixin     }
59263f0f45fSStefan Hajnoczi 
59363f0f45fSStefan Hajnoczi     timer_del(&s->timer);
59463f0f45fSStefan Hajnoczi }
59563f0f45fSStefan Hajnoczi 
curl_attach_aio_context(BlockDriverState * bs,AioContext * new_context)59663f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs,
59763f0f45fSStefan Hajnoczi                                     AioContext *new_context)
59863f0f45fSStefan Hajnoczi {
59963f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
60063f0f45fSStefan Hajnoczi 
60163f0f45fSStefan Hajnoczi     aio_timer_init(new_context, &s->timer,
60263f0f45fSStefan Hajnoczi                    QEMU_CLOCK_REALTIME, SCALE_NS,
60363f0f45fSStefan Hajnoczi                    curl_multi_timeout_do, s);
60463f0f45fSStefan Hajnoczi 
60563f0f45fSStefan Hajnoczi     assert(!s->multi);
60663f0f45fSStefan Hajnoczi     s->multi = curl_multi_init();
60763f0f45fSStefan Hajnoczi     s->aio_context = new_context;
60863f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
60963f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
61063f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
61163f0f45fSStefan Hajnoczi }
61263f0f45fSStefan Hajnoczi 
6138e6d58cdSKevin Wolf static QemuOptsList runtime_opts = {
6148e6d58cdSKevin Wolf     .name = "curl",
6158e6d58cdSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
6168e6d58cdSKevin Wolf     .desc = {
6178e6d58cdSKevin Wolf         {
618e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_URL,
6198e6d58cdSKevin Wolf             .type = QEMU_OPT_STRING,
6208e6d58cdSKevin Wolf             .help = "URL to open",
6218e6d58cdSKevin Wolf         },
6228e6d58cdSKevin Wolf         {
623e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_READAHEAD,
6248e6d58cdSKevin Wolf             .type = QEMU_OPT_SIZE,
6258e6d58cdSKevin Wolf             .help = "Readahead size",
6268e6d58cdSKevin Wolf         },
62797a3ea57SMatthew Booth         {
62897a3ea57SMatthew Booth             .name = CURL_BLOCK_OPT_SSLVERIFY,
62997a3ea57SMatthew Booth             .type = QEMU_OPT_BOOL,
63097a3ea57SMatthew Booth             .help = "Verify SSL certificate"
63197a3ea57SMatthew Booth         },
632212aefaaSDaniel Henrique Barboza         {
633212aefaaSDaniel Henrique Barboza             .name = CURL_BLOCK_OPT_TIMEOUT,
634212aefaaSDaniel Henrique Barboza             .type = QEMU_OPT_NUMBER,
635212aefaaSDaniel Henrique Barboza             .help = "Curl timeout"
636212aefaaSDaniel Henrique Barboza         },
637a94f83d9SRichard W.M. Jones         {
638a94f83d9SRichard W.M. Jones             .name = CURL_BLOCK_OPT_COOKIE,
639a94f83d9SRichard W.M. Jones             .type = QEMU_OPT_STRING,
640a94f83d9SRichard W.M. Jones             .help = "Pass the cookie or list of cookies with each request"
641a94f83d9SRichard W.M. Jones         },
6421bff9606SDaniel P. Berrange         {
643327c8ebdSPeter Krempa             .name = CURL_BLOCK_OPT_COOKIE_SECRET,
644327c8ebdSPeter Krempa             .type = QEMU_OPT_STRING,
645327c8ebdSPeter Krempa             .help = "ID of secret used as cookie passed with each request"
646327c8ebdSPeter Krempa         },
647327c8ebdSPeter Krempa         {
6481bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_USERNAME,
6491bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
6501bff9606SDaniel P. Berrange             .help = "Username for HTTP auth"
6511bff9606SDaniel P. Berrange         },
6521bff9606SDaniel P. Berrange         {
6531bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_PASSWORD_SECRET,
6541bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
6551bff9606SDaniel P. Berrange             .help = "ID of secret used as password for HTTP auth",
6561bff9606SDaniel P. Berrange         },
6571bff9606SDaniel P. Berrange         {
6581bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_PROXY_USERNAME,
6591bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
6601bff9606SDaniel P. Berrange             .help = "Username for HTTP proxy auth"
6611bff9606SDaniel P. Berrange         },
6621bff9606SDaniel P. Berrange         {
6631bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
6641bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
6651bff9606SDaniel P. Berrange             .help = "ID of secret used as password for HTTP proxy auth",
6661bff9606SDaniel P. Berrange         },
6678e6d58cdSKevin Wolf         { /* end of list */ }
6688e6d58cdSKevin Wolf     },
6698e6d58cdSKevin Wolf };
6708e6d58cdSKevin Wolf 
6711bff9606SDaniel P. Berrange 
curl_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)672015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags,
673015a1036SMax Reitz                      Error **errp)
6748e6d58cdSKevin Wolf {
6758e6d58cdSKevin Wolf     BDRVCURLState *s = bs->opaque;
6768e6d58cdSKevin Wolf     CURLState *state = NULL;
6778e6d58cdSKevin Wolf     QemuOpts *opts;
6788e6d58cdSKevin Wolf     const char *file;
679a94f83d9SRichard W.M. Jones     const char *cookie;
680327c8ebdSPeter Krempa     const char *cookie_secret;
681e7b8d9d0SAnton Johansson     /* CURL >= 7.55.0 uses curl_off_t for content length instead of a double */
682e7b8d9d0SAnton Johansson #if LIBCURL_VERSION_NUM >= 0x073700
683e7b8d9d0SAnton Johansson     curl_off_t cl;
684e7b8d9d0SAnton Johansson #else
685e7b8d9d0SAnton Johansson     double cl;
686e7b8d9d0SAnton Johansson #endif
6871bff9606SDaniel P. Berrange     const char *secretid;
68834634ca2SMax Reitz     const char *protocol_delimiter;
6892d25964dSJeff Cody     int ret;
6908e6d58cdSKevin Wolf 
691018f9deaSKevin Wolf     bdrv_graph_rdlock_main_loop();
6926ceef36aSKevin Wolf     ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes",
6936ceef36aSKevin Wolf                                     errp);
694018f9deaSKevin Wolf     bdrv_graph_rdunlock_main_loop();
6956ceef36aSKevin Wolf     if (ret < 0) {
6966ceef36aSKevin Wolf         return ret;
697a7cea2baSRichard W.M. Jones     }
698a7cea2baSRichard W.M. Jones 
6992d25964dSJeff Cody     if (!libcurl_initialized) {
7002d25964dSJeff Cody         ret = curl_global_init(CURL_GLOBAL_ALL);
7012d25964dSJeff Cody         if (ret) {
7022d25964dSJeff Cody             error_setg(errp, "libcurl initialization failed with %d", ret);
7032d25964dSJeff Cody             return -EIO;
7042d25964dSJeff Cody         }
7052d25964dSJeff Cody         libcurl_initialized = true;
7062d25964dSJeff Cody     }
7072d25964dSJeff Cody 
708456af346SPaolo Bonzini     qemu_mutex_init(&s->mutex);
70987ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
710668f62ecSMarkus Armbruster     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
7118e6d58cdSKevin Wolf         goto out_noclean;
7128e6d58cdSKevin Wolf     }
7138e6d58cdSKevin Wolf 
714e3542c67SMatthew Booth     s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
715712b64e8SMax Reitz                                           CURL_BLOCK_OPT_READAHEAD_DEFAULT);
716c76f4952SNolan     if ((s->readahead_size & 0x1ff) != 0) {
7172a94fee3SPaolo Bonzini         error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
718c76f4952SNolan                    s->readahead_size);
719c76f4952SNolan         goto out_noclean;
720c76f4952SNolan     }
721c76f4952SNolan 
722212aefaaSDaniel Henrique Barboza     s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
723712b64e8SMax Reitz                                      CURL_BLOCK_OPT_TIMEOUT_DEFAULT);
724f76faedaSRichard W.M. Jones     if (s->timeout > CURL_TIMEOUT_MAX) {
725f76faedaSRichard W.M. Jones         error_setg(errp, "timeout parameter is too large or negative");
726f76faedaSRichard W.M. Jones         goto out_noclean;
727f76faedaSRichard W.M. Jones     }
728212aefaaSDaniel Henrique Barboza 
729712b64e8SMax Reitz     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY,
730712b64e8SMax Reitz                                      CURL_BLOCK_OPT_SSLVERIFY_DEFAULT);
73197a3ea57SMatthew Booth 
732a94f83d9SRichard W.M. Jones     cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
733327c8ebdSPeter Krempa     cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET);
734327c8ebdSPeter Krempa 
735327c8ebdSPeter Krempa     if (cookie && cookie_secret) {
736327c8ebdSPeter Krempa         error_setg(errp,
737327c8ebdSPeter Krempa                    "curl driver cannot handle both cookie and cookie secret");
738327c8ebdSPeter Krempa         goto out_noclean;
739327c8ebdSPeter Krempa     }
740327c8ebdSPeter Krempa 
741327c8ebdSPeter Krempa     if (cookie_secret) {
742327c8ebdSPeter Krempa         s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp);
743327c8ebdSPeter Krempa         if (!s->cookie) {
744327c8ebdSPeter Krempa             goto out_noclean;
745327c8ebdSPeter Krempa         }
746327c8ebdSPeter Krempa     } else {
747a94f83d9SRichard W.M. Jones         s->cookie = g_strdup(cookie);
748327c8ebdSPeter Krempa     }
749a94f83d9SRichard W.M. Jones 
750e3542c67SMatthew Booth     file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
7518e6d58cdSKevin Wolf     if (file == NULL) {
7522a94fee3SPaolo Bonzini         error_setg(errp, "curl block driver requires an 'url' option");
7538e6d58cdSKevin Wolf         goto out_noclean;
7548e6d58cdSKevin Wolf     }
7558e6d58cdSKevin Wolf 
75634634ca2SMax Reitz     if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) ||
75734634ca2SMax Reitz         !strstart(protocol_delimiter, "://", NULL))
75834634ca2SMax Reitz     {
75934634ca2SMax Reitz         error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not "
76034634ca2SMax Reitz                    "start with '%s://')", bs->drv->protocol_name, file,
76134634ca2SMax Reitz                    bs->drv->protocol_name);
76234634ca2SMax Reitz         goto out_noclean;
76334634ca2SMax Reitz     }
76434634ca2SMax Reitz 
7651bff9606SDaniel P. Berrange     s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME));
7661bff9606SDaniel P. Berrange     secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET);
7671bff9606SDaniel P. Berrange 
7681bff9606SDaniel P. Berrange     if (secretid) {
7691bff9606SDaniel P. Berrange         s->password = qcrypto_secret_lookup_as_utf8(secretid, errp);
7701bff9606SDaniel P. Berrange         if (!s->password) {
7711bff9606SDaniel P. Berrange             goto out_noclean;
7721bff9606SDaniel P. Berrange         }
7731bff9606SDaniel P. Berrange     }
7741bff9606SDaniel P. Berrange 
7751bff9606SDaniel P. Berrange     s->proxyusername = g_strdup(
7761bff9606SDaniel P. Berrange         qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME));
7771bff9606SDaniel P. Berrange     secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET);
7781bff9606SDaniel P. Berrange     if (secretid) {
7791bff9606SDaniel P. Berrange         s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp);
7801bff9606SDaniel P. Berrange         if (!s->proxypassword) {
7811bff9606SDaniel P. Berrange             goto out_noclean;
7821bff9606SDaniel P. Berrange         }
7831bff9606SDaniel P. Berrange     }
7841bff9606SDaniel P. Berrange 
785ed2a66deSLaurent Vivier     trace_curl_open(file);
786709f2132SPaolo Bonzini     qemu_co_queue_init(&s->free_state_waitq);
78763f0f45fSStefan Hajnoczi     s->aio_context = bdrv_get_aio_context(bs);
7888e6d58cdSKevin Wolf     s->url = g_strdup(file);
7890f418a20SMax Reitz     s->sockets = g_hash_table_new_full(NULL, NULL, NULL, g_free);
790456af346SPaolo Bonzini     qemu_mutex_lock(&s->mutex);
7913ce6a729SPaolo Bonzini     state = curl_find_state(s);
792456af346SPaolo Bonzini     qemu_mutex_unlock(&s->mutex);
7933ce6a729SPaolo Bonzini     if (!state) {
794769ce76dSAlexander Graf         goto out_noclean;
7953ce6a729SPaolo Bonzini     }
796769ce76dSAlexander Graf 
797769ce76dSAlexander Graf     // Get file size
798769ce76dSAlexander Graf 
7993ce6a729SPaolo Bonzini     if (curl_init_state(s, state) < 0) {
8002ea7dfcdSPeter Maydell         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
8012ea7dfcdSPeter Maydell                 "curl library initialization failed.");
8023ce6a729SPaolo Bonzini         goto out;
8033ce6a729SPaolo Bonzini     }
8043ce6a729SPaolo Bonzini 
8053494d650SFam Zheng     s->accept_range = false;
806b0ea6c98SPeter Maydell     if (curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1) ||
807b0ea6c98SPeter Maydell         curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, curl_header_cb) ||
808b0ea6c98SPeter Maydell         curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s)) {
809b0ea6c98SPeter Maydell         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
810b0ea6c98SPeter Maydell                 "curl library initialization failed.");
811b0ea6c98SPeter Maydell         goto out;
812b0ea6c98SPeter Maydell     }
813769ce76dSAlexander Graf     if (curl_easy_perform(state->curl))
814769ce76dSAlexander Graf         goto out;
815e7b8d9d0SAnton Johansson     /* CURL 7.55.0 deprecates CURLINFO_CONTENT_LENGTH_DOWNLOAD in favour of
816e7b8d9d0SAnton Johansson      * the *_T version which returns a more sensible type for content length.
817e7b8d9d0SAnton Johansson      */
818e7b8d9d0SAnton Johansson #if LIBCURL_VERSION_NUM >= 0x073700
819e7b8d9d0SAnton Johansson     if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl)) {
820769ce76dSAlexander Graf         goto out;
821a41c4578STomáš Golembiovský     }
822e7b8d9d0SAnton Johansson #else
823e7b8d9d0SAnton Johansson     if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl)) {
824e7b8d9d0SAnton Johansson         goto out;
825e7b8d9d0SAnton Johansson     }
826e7b8d9d0SAnton Johansson #endif
827a41c4578STomáš Golembiovský     /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
828a41c4578STomáš Golembiovský      * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
82950d6a8a3SStefan Weil      * known and zero if it is really zero-length file. */
830a41c4578STomáš Golembiovský #if LIBCURL_VERSION_NUM >= 0x071304
831e7b8d9d0SAnton Johansson     if (cl < 0) {
832a41c4578STomáš Golembiovský         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
833a41c4578STomáš Golembiovský                 "Server didn't report file size.");
834a41c4578STomáš Golembiovský         goto out;
835a41c4578STomáš Golembiovský     }
836a41c4578STomáš Golembiovský #else
837e7b8d9d0SAnton Johansson     if (cl <= 0) {
838a41c4578STomáš Golembiovský         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
839a41c4578STomáš Golembiovský                 "Unknown file size or zero-length file.");
840a41c4578STomáš Golembiovský         goto out;
841a41c4578STomáš Golembiovský     }
842a41c4578STomáš Golembiovský #endif
843a41c4578STomáš Golembiovský 
844e7b8d9d0SAnton Johansson     s->len = cl;
845a41c4578STomáš Golembiovský 
8463494d650SFam Zheng     if ((!strncasecmp(s->url, "http://", strlen("http://"))
8473494d650SFam Zheng         || !strncasecmp(s->url, "https://", strlen("https://")))
8483494d650SFam Zheng         && !s->accept_range) {
8493494d650SFam Zheng         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
8503494d650SFam Zheng                 "Server does not support 'range' (byte ranges).");
8513494d650SFam Zheng         goto out;
8523494d650SFam Zheng     }
853ed2a66deSLaurent Vivier     trace_curl_open_size(s->len);
854769ce76dSAlexander Graf 
855456af346SPaolo Bonzini     qemu_mutex_lock(&s->mutex);
856769ce76dSAlexander Graf     curl_clean_state(state);
857456af346SPaolo Bonzini     qemu_mutex_unlock(&s->mutex);
858769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
859769ce76dSAlexander Graf     state->curl = NULL;
860769ce76dSAlexander Graf 
86163f0f45fSStefan Hajnoczi     curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
862769ce76dSAlexander Graf 
8638e6d58cdSKevin Wolf     qemu_opts_del(opts);
864769ce76dSAlexander Graf     return 0;
865769ce76dSAlexander Graf 
866769ce76dSAlexander Graf out:
867acd7fdc6SMaria Kustova     error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
868769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
869769ce76dSAlexander Graf     state->curl = NULL;
870769ce76dSAlexander Graf out_noclean:
871456af346SPaolo Bonzini     qemu_mutex_destroy(&s->mutex);
872a94f83d9SRichard W.M. Jones     g_free(s->cookie);
8738e6d58cdSKevin Wolf     g_free(s->url);
874996922deSJeff Cody     g_free(s->username);
875996922deSJeff Cody     g_free(s->proxyusername);
876996922deSJeff Cody     g_free(s->proxypassword);
8771e84cf79SHanna Czenczek     if (s->sockets) {
8780f418a20SMax Reitz         curl_drop_all_sockets(s->sockets);
8790f418a20SMax Reitz         g_hash_table_destroy(s->sockets);
8801e84cf79SHanna Czenczek     }
8818e6d58cdSKevin Wolf     qemu_opts_del(opts);
882769ce76dSAlexander Graf     return -EINVAL;
883769ce76dSAlexander Graf }
884769ce76dSAlexander Graf 
curl_setup_preadv(BlockDriverState * bs,CURLAIOCB * acb)8859bae2acaSPaolo Bonzini static void coroutine_fn curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)
886769ce76dSAlexander Graf {
887769ce76dSAlexander Graf     CURLState *state;
888b69cdef8SMatthew Booth     int running;
889769ce76dSAlexander Graf 
8901919631eSPaolo Bonzini     BDRVCURLState *s = bs->opaque;
891769ce76dSAlexander Graf 
8922125e5eaSPaolo Bonzini     uint64_t start = acb->offset;
8932125e5eaSPaolo Bonzini     uint64_t end;
894769ce76dSAlexander Graf 
895ba3186c4SPaolo Bonzini     qemu_mutex_lock(&s->mutex);
8961919631eSPaolo Bonzini 
897769ce76dSAlexander Graf     // In case we have the requested data already (e.g. read-ahead),
898769ce76dSAlexander Graf     // we can just call the callback and be done.
89928256d82SPaolo Bonzini     if (curl_find_buf(s, start, acb->bytes, acb)) {
90034db05e7SPaolo Bonzini         goto out;
901769ce76dSAlexander Graf     }
902769ce76dSAlexander Graf 
903769ce76dSAlexander Graf     // No cache found, so let's start a new request
9043ce6a729SPaolo Bonzini     for (;;) {
9053ce6a729SPaolo Bonzini         state = curl_find_state(s);
9063ce6a729SPaolo Bonzini         if (state) {
9073ce6a729SPaolo Bonzini             break;
9083ce6a729SPaolo Bonzini         }
909709f2132SPaolo Bonzini         qemu_co_queue_wait(&s->free_state_waitq, &s->mutex);
9103ce6a729SPaolo Bonzini     }
9113ce6a729SPaolo Bonzini 
9123ce6a729SPaolo Bonzini     if (curl_init_state(s, state) < 0) {
9133ce6a729SPaolo Bonzini         curl_clean_state(state);
91428256d82SPaolo Bonzini         acb->ret = -EIO;
9151919631eSPaolo Bonzini         goto out;
916363c3c85SNick Thomas     }
917769ce76dSAlexander Graf 
918769ce76dSAlexander Graf     acb->start = 0;
9192125e5eaSPaolo Bonzini     acb->end = MIN(acb->bytes, s->len - start);
920769ce76dSAlexander Graf 
921769ce76dSAlexander Graf     state->buf_off = 0;
9227267c094SAnthony Liguori     g_free(state->orig_buf);
923769ce76dSAlexander Graf     state->buf_start = start;
9244e504535SMax Reitz     state->buf_len = MIN(acb->end + s->readahead_size, s->len - start);
9254e504535SMax Reitz     end = start + state->buf_len - 1;
9268dc7a772SKevin Wolf     state->orig_buf = g_try_malloc(state->buf_len);
9278dc7a772SKevin Wolf     if (state->buf_len && state->orig_buf == NULL) {
9288dc7a772SKevin Wolf         curl_clean_state(state);
92928256d82SPaolo Bonzini         acb->ret = -ENOMEM;
9301919631eSPaolo Bonzini         goto out;
9318dc7a772SKevin Wolf     }
932769ce76dSAlexander Graf     state->acb[0] = acb;
933769ce76dSAlexander Graf 
9342125e5eaSPaolo Bonzini     snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end);
935ed2a66deSLaurent Vivier     trace_curl_setup_preadv(acb->bytes, start, state->range);
936b0ea6c98SPeter Maydell     if (curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range) ||
937b0ea6c98SPeter Maydell         curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) {
938c34dc07fSMax Reitz         state->acb[0] = NULL;
939c34dc07fSMax Reitz         acb->ret = -EIO;
940c34dc07fSMax Reitz 
941c34dc07fSMax Reitz         curl_clean_state(state);
942c34dc07fSMax Reitz         goto out;
943c34dc07fSMax Reitz     }
944769ce76dSAlexander Graf 
945b69cdef8SMatthew Booth     /* Tell curl it needs to kick things off */
946b69cdef8SMatthew Booth     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
9471919631eSPaolo Bonzini 
9481919631eSPaolo Bonzini out:
949ba3186c4SPaolo Bonzini     qemu_mutex_unlock(&s->mutex);
950363c3c85SNick Thomas }
951363c3c85SNick Thomas 
curl_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)95228256d82SPaolo Bonzini static int coroutine_fn curl_co_preadv(BlockDriverState *bs,
953f7ef38ddSVladimir Sementsov-Ogievskiy         int64_t offset, int64_t bytes, QEMUIOVector *qiov,
954f7ef38ddSVladimir Sementsov-Ogievskiy         BdrvRequestFlags flags)
955363c3c85SNick Thomas {
95628256d82SPaolo Bonzini     CURLAIOCB acb = {
95728256d82SPaolo Bonzini         .co = qemu_coroutine_self(),
95828256d82SPaolo Bonzini         .ret = -EINPROGRESS,
95928256d82SPaolo Bonzini         .qiov = qiov,
96028256d82SPaolo Bonzini         .offset = offset,
96128256d82SPaolo Bonzini         .bytes = bytes
96228256d82SPaolo Bonzini     };
963363c3c85SNick Thomas 
96428256d82SPaolo Bonzini     curl_setup_preadv(bs, &acb);
96528256d82SPaolo Bonzini     while (acb.ret == -EINPROGRESS) {
96628256d82SPaolo Bonzini         qemu_coroutine_yield();
96728256d82SPaolo Bonzini     }
96828256d82SPaolo Bonzini     return acb.ret;
969769ce76dSAlexander Graf }
970769ce76dSAlexander Graf 
curl_close(BlockDriverState * bs)971769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs)
972769ce76dSAlexander Graf {
973769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
974769ce76dSAlexander Graf 
975ed2a66deSLaurent Vivier     trace_curl_close();
97663f0f45fSStefan Hajnoczi     curl_detach_aio_context(bs);
977ba3186c4SPaolo Bonzini     qemu_mutex_destroy(&s->mutex);
978031fd1beSPeter Maydell 
9790f418a20SMax Reitz     g_hash_table_destroy(s->sockets);
980a94f83d9SRichard W.M. Jones     g_free(s->cookie);
98145724d6dSStefan Weil     g_free(s->url);
982996922deSJeff Cody     g_free(s->username);
983996922deSJeff Cody     g_free(s->proxyusername);
984996922deSJeff Cody     g_free(s->proxypassword);
985769ce76dSAlexander Graf }
986769ce76dSAlexander Graf 
curl_co_getlength(BlockDriverState * bs)987c86422c5SEmanuele Giuseppe Esposito static int64_t coroutine_fn curl_co_getlength(BlockDriverState *bs)
988769ce76dSAlexander Graf {
989769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
990769ce76dSAlexander Graf     return s->len;
991769ce76dSAlexander Graf }
992769ce76dSAlexander Graf 
curl_refresh_filename(BlockDriverState * bs)993937c007bSMax Reitz static void curl_refresh_filename(BlockDriverState *bs)
994937c007bSMax Reitz {
995937c007bSMax Reitz     BDRVCURLState *s = bs->opaque;
996937c007bSMax Reitz 
997937c007bSMax Reitz     /* "readahead" and "timeout" do not change the guest-visible data,
998937c007bSMax Reitz      * so ignore them */
999937c007bSMax Reitz     if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT ||
1000937c007bSMax Reitz         s->cookie || s->username || s->password || s->proxyusername ||
1001937c007bSMax Reitz         s->proxypassword)
1002937c007bSMax Reitz     {
1003937c007bSMax Reitz         return;
1004937c007bSMax Reitz     }
1005937c007bSMax Reitz 
1006937c007bSMax Reitz     pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url);
1007937c007bSMax Reitz }
1008937c007bSMax Reitz 
1009937c007bSMax Reitz 
10102654267cSMax Reitz static const char *const curl_strong_runtime_opts[] = {
10112654267cSMax Reitz     CURL_BLOCK_OPT_URL,
10122654267cSMax Reitz     CURL_BLOCK_OPT_SSLVERIFY,
10132654267cSMax Reitz     CURL_BLOCK_OPT_COOKIE,
10142654267cSMax Reitz     CURL_BLOCK_OPT_COOKIE_SECRET,
10152654267cSMax Reitz     CURL_BLOCK_OPT_USERNAME,
10162654267cSMax Reitz     CURL_BLOCK_OPT_PASSWORD_SECRET,
10172654267cSMax Reitz     CURL_BLOCK_OPT_PROXY_USERNAME,
10182654267cSMax Reitz     CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
10192654267cSMax Reitz 
10202654267cSMax Reitz     NULL
10212654267cSMax Reitz };
10222654267cSMax Reitz 
1023769ce76dSAlexander Graf static BlockDriver bdrv_http = {
1024769ce76dSAlexander Graf     .format_name                = "http",
1025769ce76dSAlexander Graf     .protocol_name              = "http",
1026769ce76dSAlexander Graf 
1027769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
10288e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
102944b424dcSPaolo Bonzini     .bdrv_open                  = curl_open,
1030769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
1031c86422c5SEmanuele Giuseppe Esposito     .bdrv_co_getlength          = curl_co_getlength,
1032769ce76dSAlexander Graf 
103328256d82SPaolo Bonzini     .bdrv_co_preadv             = curl_co_preadv,
103463f0f45fSStefan Hajnoczi 
103563f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
103663f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
10372654267cSMax Reitz 
1038937c007bSMax Reitz     .bdrv_refresh_filename      = curl_refresh_filename,
10392654267cSMax Reitz     .strong_runtime_opts        = curl_strong_runtime_opts,
1040769ce76dSAlexander Graf };
1041769ce76dSAlexander Graf 
1042769ce76dSAlexander Graf static BlockDriver bdrv_https = {
1043769ce76dSAlexander Graf     .format_name                = "https",
1044769ce76dSAlexander Graf     .protocol_name              = "https",
1045769ce76dSAlexander Graf 
1046769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
10478e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
104844b424dcSPaolo Bonzini     .bdrv_open                  = curl_open,
1049769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
1050c86422c5SEmanuele Giuseppe Esposito     .bdrv_co_getlength          = curl_co_getlength,
1051769ce76dSAlexander Graf 
105228256d82SPaolo Bonzini     .bdrv_co_preadv             = curl_co_preadv,
105363f0f45fSStefan Hajnoczi 
105463f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
105563f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
10562654267cSMax Reitz 
1057937c007bSMax Reitz     .bdrv_refresh_filename      = curl_refresh_filename,
10582654267cSMax Reitz     .strong_runtime_opts        = curl_strong_runtime_opts,
1059769ce76dSAlexander Graf };
1060769ce76dSAlexander Graf 
1061769ce76dSAlexander Graf static BlockDriver bdrv_ftp = {
1062769ce76dSAlexander Graf     .format_name                = "ftp",
1063769ce76dSAlexander Graf     .protocol_name              = "ftp",
1064769ce76dSAlexander Graf 
1065769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
10668e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
106744b424dcSPaolo Bonzini     .bdrv_open                  = curl_open,
1068769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
1069c86422c5SEmanuele Giuseppe Esposito     .bdrv_co_getlength          = curl_co_getlength,
1070769ce76dSAlexander Graf 
107128256d82SPaolo Bonzini     .bdrv_co_preadv             = curl_co_preadv,
107263f0f45fSStefan Hajnoczi 
107363f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
107463f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
10752654267cSMax Reitz 
1076937c007bSMax Reitz     .bdrv_refresh_filename      = curl_refresh_filename,
10772654267cSMax Reitz     .strong_runtime_opts        = curl_strong_runtime_opts,
1078769ce76dSAlexander Graf };
1079769ce76dSAlexander Graf 
1080769ce76dSAlexander Graf static BlockDriver bdrv_ftps = {
1081769ce76dSAlexander Graf     .format_name                = "ftps",
1082769ce76dSAlexander Graf     .protocol_name              = "ftps",
1083769ce76dSAlexander Graf 
1084769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
10858e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
108644b424dcSPaolo Bonzini     .bdrv_open                  = curl_open,
1087769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
1088c86422c5SEmanuele Giuseppe Esposito     .bdrv_co_getlength          = curl_co_getlength,
1089769ce76dSAlexander Graf 
109028256d82SPaolo Bonzini     .bdrv_co_preadv             = curl_co_preadv,
109163f0f45fSStefan Hajnoczi 
109263f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
109363f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
10942654267cSMax Reitz 
1095937c007bSMax Reitz     .bdrv_refresh_filename      = curl_refresh_filename,
10962654267cSMax Reitz     .strong_runtime_opts        = curl_strong_runtime_opts,
1097769ce76dSAlexander Graf };
1098769ce76dSAlexander Graf 
curl_block_init(void)1099769ce76dSAlexander Graf static void curl_block_init(void)
1100769ce76dSAlexander Graf {
1101769ce76dSAlexander Graf     bdrv_register(&bdrv_http);
1102769ce76dSAlexander Graf     bdrv_register(&bdrv_https);
1103769ce76dSAlexander Graf     bdrv_register(&bdrv_ftp);
1104769ce76dSAlexander Graf     bdrv_register(&bdrv_ftps);
1105769ce76dSAlexander Graf }
1106769ce76dSAlexander Graf 
1107769ce76dSAlexander Graf block_init(curl_block_init);
1108