xref: /openbmc/qemu/block/curl.c (revision da34e65cb4025728566d6504a99916f6e7e1dd6a)
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  */
2480c71a24SPeter Maydell #include "qemu/osdep.h"
25*da34e65cSMarkus Armbruster #include "qapi/error.h"
26769ce76dSAlexander Graf #include "qemu-common.h"
27796a060bSRichard W.M. Jones #include "qemu/error-report.h"
28737e150eSPaolo Bonzini #include "block/block_int.h"
2997a3ea57SMatthew Booth #include "qapi/qmp/qbool.h"
30d49b6836SMarkus Armbruster #include "qapi/qmp/qstring.h"
311bff9606SDaniel P. Berrange #include "crypto/secret.h"
32769ce76dSAlexander Graf #include <curl/curl.h>
33769ce76dSAlexander Graf 
3441c23467SRichard W.M. Jones // #define DEBUG_CURL
35769ce76dSAlexander Graf // #define DEBUG_VERBOSE
36769ce76dSAlexander Graf 
37769ce76dSAlexander Graf #ifdef DEBUG_CURL
38d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
39769ce76dSAlexander Graf #else
40d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { } while (0)
41769ce76dSAlexander Graf #endif
42769ce76dSAlexander Graf 
43031fd1beSPeter Maydell #if LIBCURL_VERSION_NUM >= 0x071000
44031fd1beSPeter Maydell /* The multi interface timer callback was introduced in 7.16.0 */
45031fd1beSPeter Maydell #define NEED_CURL_TIMER_CALLBACK
469aedd5a5SMatthew Booth #define HAVE_SOCKET_ACTION
479aedd5a5SMatthew Booth #endif
489aedd5a5SMatthew Booth 
499aedd5a5SMatthew Booth #ifndef HAVE_SOCKET_ACTION
509aedd5a5SMatthew Booth /* If curl_multi_socket_action isn't available, define it statically here in
519aedd5a5SMatthew Booth  * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
529aedd5a5SMatthew Booth  * less efficient but still safe. */
539aedd5a5SMatthew Booth static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
549aedd5a5SMatthew Booth                                             curl_socket_t sockfd,
559aedd5a5SMatthew Booth                                             int ev_bitmask,
569aedd5a5SMatthew Booth                                             int *running_handles)
579aedd5a5SMatthew Booth {
589aedd5a5SMatthew Booth     return curl_multi_socket(multi_handle, sockfd, running_handles);
599aedd5a5SMatthew Booth }
609aedd5a5SMatthew Booth #define curl_multi_socket_action __curl_multi_socket_action
61031fd1beSPeter Maydell #endif
62031fd1beSPeter Maydell 
63fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
64fb6d1bbdSStefan Hajnoczi                    CURLPROTO_FTP | CURLPROTO_FTPS | \
65fb6d1bbdSStefan Hajnoczi                    CURLPROTO_TFTP)
66fb6d1bbdSStefan Hajnoczi 
67769ce76dSAlexander Graf #define CURL_NUM_STATES 8
68769ce76dSAlexander Graf #define CURL_NUM_ACB    8
69769ce76dSAlexander Graf #define SECTOR_SIZE     512
70e3542c67SMatthew Booth #define READ_AHEAD_DEFAULT (256 * 1024)
71212aefaaSDaniel Henrique Barboza #define CURL_TIMEOUT_DEFAULT 5
72f76faedaSRichard W.M. Jones #define CURL_TIMEOUT_MAX 10000
73769ce76dSAlexander Graf 
74769ce76dSAlexander Graf #define FIND_RET_NONE   0
75769ce76dSAlexander Graf #define FIND_RET_OK     1
76769ce76dSAlexander Graf #define FIND_RET_WAIT   2
77769ce76dSAlexander Graf 
78e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL       "url"
79e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead"
8097a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
81212aefaaSDaniel Henrique Barboza #define CURL_BLOCK_OPT_TIMEOUT "timeout"
82a94f83d9SRichard W.M. Jones #define CURL_BLOCK_OPT_COOKIE    "cookie"
831bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_USERNAME "username"
841bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
851bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
861bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
87e3542c67SMatthew Booth 
88769ce76dSAlexander Graf struct BDRVCURLState;
89769ce76dSAlexander Graf 
90769ce76dSAlexander Graf typedef struct CURLAIOCB {
917c84b1b8SMarkus Armbruster     BlockAIOCB common;
92363c3c85SNick Thomas     QEMUBH *bh;
93769ce76dSAlexander Graf     QEMUIOVector *qiov;
94363c3c85SNick Thomas 
95363c3c85SNick Thomas     int64_t sector_num;
96363c3c85SNick Thomas     int nb_sectors;
97363c3c85SNick Thomas 
98769ce76dSAlexander Graf     size_t start;
99769ce76dSAlexander Graf     size_t end;
100769ce76dSAlexander Graf } CURLAIOCB;
101769ce76dSAlexander Graf 
102769ce76dSAlexander Graf typedef struct CURLState
103769ce76dSAlexander Graf {
104769ce76dSAlexander Graf     struct BDRVCURLState *s;
105769ce76dSAlexander Graf     CURLAIOCB *acb[CURL_NUM_ACB];
106769ce76dSAlexander Graf     CURL *curl;
107838ef602SMatthew Booth     curl_socket_t sock_fd;
108769ce76dSAlexander Graf     char *orig_buf;
109769ce76dSAlexander Graf     size_t buf_start;
110769ce76dSAlexander Graf     size_t buf_off;
111769ce76dSAlexander Graf     size_t buf_len;
112769ce76dSAlexander Graf     char range[128];
113769ce76dSAlexander Graf     char errmsg[CURL_ERROR_SIZE];
114769ce76dSAlexander Graf     char in_use;
115769ce76dSAlexander Graf } CURLState;
116769ce76dSAlexander Graf 
117769ce76dSAlexander Graf typedef struct BDRVCURLState {
118769ce76dSAlexander Graf     CURLM *multi;
119031fd1beSPeter Maydell     QEMUTimer timer;
120769ce76dSAlexander Graf     size_t len;
121769ce76dSAlexander Graf     CURLState states[CURL_NUM_STATES];
122769ce76dSAlexander Graf     char *url;
123c76f4952SNolan     size_t readahead_size;
12497a3ea57SMatthew Booth     bool sslverify;
125f76faedaSRichard W.M. Jones     uint64_t timeout;
126a94f83d9SRichard W.M. Jones     char *cookie;
1273494d650SFam Zheng     bool accept_range;
12863f0f45fSStefan Hajnoczi     AioContext *aio_context;
1291bff9606SDaniel P. Berrange     char *username;
1301bff9606SDaniel P. Berrange     char *password;
1311bff9606SDaniel P. Berrange     char *proxyusername;
1321bff9606SDaniel P. Berrange     char *proxypassword;
133769ce76dSAlexander Graf } BDRVCURLState;
134769ce76dSAlexander Graf 
135769ce76dSAlexander Graf static void curl_clean_state(CURLState *s);
136769ce76dSAlexander Graf static void curl_multi_do(void *arg);
137838ef602SMatthew Booth static void curl_multi_read(void *arg);
138769ce76dSAlexander Graf 
139031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
140031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
141031fd1beSPeter Maydell {
142031fd1beSPeter Maydell     BDRVCURLState *s = opaque;
143031fd1beSPeter Maydell 
144031fd1beSPeter Maydell     DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
145031fd1beSPeter Maydell     if (timeout_ms == -1) {
146031fd1beSPeter Maydell         timer_del(&s->timer);
147031fd1beSPeter Maydell     } else {
148031fd1beSPeter Maydell         int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
149031fd1beSPeter Maydell         timer_mod(&s->timer,
150031fd1beSPeter Maydell                   qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
151031fd1beSPeter Maydell     }
152031fd1beSPeter Maydell     return 0;
153031fd1beSPeter Maydell }
154031fd1beSPeter Maydell #endif
155031fd1beSPeter Maydell 
156769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
15763f0f45fSStefan Hajnoczi                         void *userp, void *sp)
158769ce76dSAlexander Graf {
15963f0f45fSStefan Hajnoczi     BDRVCURLState *s;
160838ef602SMatthew Booth     CURLState *state = NULL;
161838ef602SMatthew Booth     curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
162838ef602SMatthew Booth     state->sock_fd = fd;
16363f0f45fSStefan Hajnoczi     s = state->s;
164838ef602SMatthew Booth 
165d0f2c4c6Smalc     DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
166769ce76dSAlexander Graf     switch (action) {
167769ce76dSAlexander Graf         case CURL_POLL_IN:
168dca21ef2SFam Zheng             aio_set_fd_handler(s->aio_context, fd, false,
169dca21ef2SFam Zheng                                curl_multi_read, NULL, state);
170769ce76dSAlexander Graf             break;
171769ce76dSAlexander Graf         case CURL_POLL_OUT:
172dca21ef2SFam Zheng             aio_set_fd_handler(s->aio_context, fd, false,
173dca21ef2SFam Zheng                                NULL, curl_multi_do, state);
174769ce76dSAlexander Graf             break;
175769ce76dSAlexander Graf         case CURL_POLL_INOUT:
176dca21ef2SFam Zheng             aio_set_fd_handler(s->aio_context, fd, false,
177dca21ef2SFam Zheng                                curl_multi_read, curl_multi_do, state);
178769ce76dSAlexander Graf             break;
179769ce76dSAlexander Graf         case CURL_POLL_REMOVE:
180dca21ef2SFam Zheng             aio_set_fd_handler(s->aio_context, fd, false,
181dca21ef2SFam Zheng                                NULL, NULL, NULL);
182769ce76dSAlexander Graf             break;
183769ce76dSAlexander Graf     }
184769ce76dSAlexander Graf 
185769ce76dSAlexander Graf     return 0;
186769ce76dSAlexander Graf }
187769ce76dSAlexander Graf 
1883494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
189769ce76dSAlexander Graf {
1903494d650SFam Zheng     BDRVCURLState *s = opaque;
191769ce76dSAlexander Graf     size_t realsize = size * nmemb;
1923494d650SFam Zheng     const char *accept_line = "Accept-Ranges: bytes";
193769ce76dSAlexander Graf 
1943494d650SFam Zheng     if (realsize >= strlen(accept_line)
1953494d650SFam Zheng         && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
1963494d650SFam Zheng         s->accept_range = true;
1970bfcd599SBlue Swirl     }
198769ce76dSAlexander Graf 
199769ce76dSAlexander Graf     return realsize;
200769ce76dSAlexander Graf }
201769ce76dSAlexander Graf 
202769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
203769ce76dSAlexander Graf {
204769ce76dSAlexander Graf     CURLState *s = ((CURLState*)opaque);
205769ce76dSAlexander Graf     size_t realsize = size * nmemb;
206769ce76dSAlexander Graf     int i;
207769ce76dSAlexander Graf 
2080bfcd599SBlue Swirl     DPRINTF("CURL: Just reading %zd bytes\n", realsize);
209769ce76dSAlexander Graf 
210769ce76dSAlexander Graf     if (!s || !s->orig_buf)
21138bbc0a5SMatthew Booth         return 0;
212769ce76dSAlexander Graf 
2136d4b9e55SFam Zheng     if (s->buf_off >= s->buf_len) {
2146d4b9e55SFam Zheng         /* buffer full, read nothing */
2156d4b9e55SFam Zheng         return 0;
2166d4b9e55SFam Zheng     }
2176d4b9e55SFam Zheng     realsize = MIN(realsize, s->buf_len - s->buf_off);
218769ce76dSAlexander Graf     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
219769ce76dSAlexander Graf     s->buf_off += realsize;
220769ce76dSAlexander Graf 
221769ce76dSAlexander Graf     for(i=0; i<CURL_NUM_ACB; i++) {
222769ce76dSAlexander Graf         CURLAIOCB *acb = s->acb[i];
223769ce76dSAlexander Graf 
224769ce76dSAlexander Graf         if (!acb)
225769ce76dSAlexander Graf             continue;
226769ce76dSAlexander Graf 
227769ce76dSAlexander Graf         if ((s->buf_off >= acb->end)) {
22803396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
229769ce76dSAlexander Graf                                 acb->end - acb->start);
230769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
2318007429aSFam Zheng             qemu_aio_unref(acb);
232769ce76dSAlexander Graf             s->acb[i] = NULL;
233769ce76dSAlexander Graf         }
234769ce76dSAlexander Graf     }
235769ce76dSAlexander Graf 
236769ce76dSAlexander Graf     return realsize;
237769ce76dSAlexander Graf }
238769ce76dSAlexander Graf 
239769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
240769ce76dSAlexander Graf                          CURLAIOCB *acb)
241769ce76dSAlexander Graf {
242769ce76dSAlexander Graf     int i;
243769ce76dSAlexander Graf     size_t end = start + len;
244769ce76dSAlexander Graf 
245769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
246769ce76dSAlexander Graf         CURLState *state = &s->states[i];
247769ce76dSAlexander Graf         size_t buf_end = (state->buf_start + state->buf_off);
248769ce76dSAlexander Graf         size_t buf_fend = (state->buf_start + state->buf_len);
249769ce76dSAlexander Graf 
250769ce76dSAlexander Graf         if (!state->orig_buf)
251769ce76dSAlexander Graf             continue;
252769ce76dSAlexander Graf         if (!state->buf_off)
253769ce76dSAlexander Graf             continue;
254769ce76dSAlexander Graf 
255769ce76dSAlexander Graf         // Does the existing buffer cover our section?
256769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
257769ce76dSAlexander Graf             (start <= buf_end) &&
258769ce76dSAlexander Graf             (end >= state->buf_start) &&
259769ce76dSAlexander Graf             (end <= buf_end))
260769ce76dSAlexander Graf         {
261769ce76dSAlexander Graf             char *buf = state->orig_buf + (start - state->buf_start);
262769ce76dSAlexander Graf 
26303396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, buf, len);
264769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
265769ce76dSAlexander Graf 
266769ce76dSAlexander Graf             return FIND_RET_OK;
267769ce76dSAlexander Graf         }
268769ce76dSAlexander Graf 
269769ce76dSAlexander Graf         // Wait for unfinished chunks
270b7079df4SMatthew Booth         if (state->in_use &&
271b7079df4SMatthew Booth             (start >= state->buf_start) &&
272769ce76dSAlexander Graf             (start <= buf_fend) &&
273769ce76dSAlexander Graf             (end >= state->buf_start) &&
274769ce76dSAlexander Graf             (end <= buf_fend))
275769ce76dSAlexander Graf         {
276769ce76dSAlexander Graf             int j;
277769ce76dSAlexander Graf 
278769ce76dSAlexander Graf             acb->start = start - state->buf_start;
279769ce76dSAlexander Graf             acb->end = acb->start + len;
280769ce76dSAlexander Graf 
281769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++) {
282769ce76dSAlexander Graf                 if (!state->acb[j]) {
283769ce76dSAlexander Graf                     state->acb[j] = acb;
284769ce76dSAlexander Graf                     return FIND_RET_WAIT;
285769ce76dSAlexander Graf                 }
286769ce76dSAlexander Graf             }
287769ce76dSAlexander Graf         }
288769ce76dSAlexander Graf     }
289769ce76dSAlexander Graf 
290769ce76dSAlexander Graf     return FIND_RET_NONE;
291769ce76dSAlexander Graf }
292769ce76dSAlexander Graf 
293838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s)
294769ce76dSAlexander Graf {
295769ce76dSAlexander Graf     int msgs_in_queue;
296769ce76dSAlexander Graf 
297769ce76dSAlexander Graf     /* Try to find done transfers, so we can free the easy
298769ce76dSAlexander Graf      * handle again. */
2991f2cead3SMatthew Booth     for (;;) {
300769ce76dSAlexander Graf         CURLMsg *msg;
301769ce76dSAlexander Graf         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
302769ce76dSAlexander Graf 
3031f2cead3SMatthew Booth         /* Quit when there are no more completions */
304769ce76dSAlexander Graf         if (!msg)
305769ce76dSAlexander Graf             break;
306769ce76dSAlexander Graf 
3071f2cead3SMatthew Booth         if (msg->msg == CURLMSG_DONE) {
308769ce76dSAlexander Graf             CURLState *state = NULL;
309f6246509SMatthew Booth             curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
310f6246509SMatthew Booth                               (char **)&state);
311f785a5aeSNicholas Thomas 
312f785a5aeSNicholas Thomas             /* ACBs for successful messages get completed in curl_read_cb */
313f785a5aeSNicholas Thomas             if (msg->data.result != CURLE_OK) {
314f785a5aeSNicholas Thomas                 int i;
315796a060bSRichard W.M. Jones                 static int errcount = 100;
316796a060bSRichard W.M. Jones 
317796a060bSRichard W.M. Jones                 /* Don't lose the original error message from curl, since
318796a060bSRichard W.M. Jones                  * it contains extra data.
319796a060bSRichard W.M. Jones                  */
320796a060bSRichard W.M. Jones                 if (errcount > 0) {
321796a060bSRichard W.M. Jones                     error_report("curl: %s", state->errmsg);
322796a060bSRichard W.M. Jones                     if (--errcount == 0) {
323796a060bSRichard W.M. Jones                         error_report("curl: further errors suppressed");
324796a060bSRichard W.M. Jones                     }
325796a060bSRichard W.M. Jones                 }
326796a060bSRichard W.M. Jones 
327f785a5aeSNicholas Thomas                 for (i = 0; i < CURL_NUM_ACB; i++) {
328f785a5aeSNicholas Thomas                     CURLAIOCB *acb = state->acb[i];
329f785a5aeSNicholas Thomas 
330f785a5aeSNicholas Thomas                     if (acb == NULL) {
331f785a5aeSNicholas Thomas                         continue;
332f785a5aeSNicholas Thomas                     }
333f785a5aeSNicholas Thomas 
334796a060bSRichard W.M. Jones                     acb->common.cb(acb->common.opaque, -EPROTO);
3358007429aSFam Zheng                     qemu_aio_unref(acb);
336f785a5aeSNicholas Thomas                     state->acb[i] = NULL;
337f785a5aeSNicholas Thomas                 }
338f785a5aeSNicholas Thomas             }
339f785a5aeSNicholas Thomas 
340769ce76dSAlexander Graf             curl_clean_state(state);
341769ce76dSAlexander Graf             break;
342769ce76dSAlexander Graf         }
343769ce76dSAlexander Graf     }
344769ce76dSAlexander Graf }
345769ce76dSAlexander Graf 
346031fd1beSPeter Maydell static void curl_multi_do(void *arg)
347031fd1beSPeter Maydell {
348838ef602SMatthew Booth     CURLState *s = (CURLState *)arg;
349031fd1beSPeter Maydell     int running;
350031fd1beSPeter Maydell     int r;
351031fd1beSPeter Maydell 
352838ef602SMatthew Booth     if (!s->s->multi) {
353031fd1beSPeter Maydell         return;
354031fd1beSPeter Maydell     }
355031fd1beSPeter Maydell 
356031fd1beSPeter Maydell     do {
357838ef602SMatthew Booth         r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running);
358031fd1beSPeter Maydell     } while(r == CURLM_CALL_MULTI_PERFORM);
359031fd1beSPeter Maydell 
360838ef602SMatthew Booth }
361838ef602SMatthew Booth 
362838ef602SMatthew Booth static void curl_multi_read(void *arg)
363838ef602SMatthew Booth {
364838ef602SMatthew Booth     CURLState *s = (CURLState *)arg;
365838ef602SMatthew Booth 
366838ef602SMatthew Booth     curl_multi_do(arg);
367838ef602SMatthew Booth     curl_multi_check_completion(s->s);
368031fd1beSPeter Maydell }
369031fd1beSPeter Maydell 
370031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg)
371031fd1beSPeter Maydell {
372031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
373031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
374031fd1beSPeter Maydell     int running;
375031fd1beSPeter Maydell 
376031fd1beSPeter Maydell     if (!s->multi) {
377031fd1beSPeter Maydell         return;
378031fd1beSPeter Maydell     }
379031fd1beSPeter Maydell 
380031fd1beSPeter Maydell     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
381031fd1beSPeter Maydell 
382838ef602SMatthew Booth     curl_multi_check_completion(s);
383031fd1beSPeter Maydell #else
384031fd1beSPeter Maydell     abort();
385031fd1beSPeter Maydell #endif
386031fd1beSPeter Maydell }
387031fd1beSPeter Maydell 
388a2f468e4SRichard W.M. Jones static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)
389769ce76dSAlexander Graf {
390769ce76dSAlexander Graf     CURLState *state = NULL;
391769ce76dSAlexander Graf     int i, j;
392769ce76dSAlexander Graf 
393769ce76dSAlexander Graf     do {
394769ce76dSAlexander Graf         for (i=0; i<CURL_NUM_STATES; i++) {
395769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++)
396769ce76dSAlexander Graf                 if (s->states[i].acb[j])
397769ce76dSAlexander Graf                     continue;
398769ce76dSAlexander Graf             if (s->states[i].in_use)
399769ce76dSAlexander Graf                 continue;
400769ce76dSAlexander Graf 
401769ce76dSAlexander Graf             state = &s->states[i];
402769ce76dSAlexander Graf             state->in_use = 1;
403769ce76dSAlexander Graf             break;
404769ce76dSAlexander Graf         }
405769ce76dSAlexander Graf         if (!state) {
406a2f468e4SRichard W.M. Jones             aio_poll(bdrv_get_aio_context(bs), true);
407769ce76dSAlexander Graf         }
408769ce76dSAlexander Graf     } while(!state);
409769ce76dSAlexander Graf 
4109e550b32SMatthew Booth     if (!state->curl) {
411769ce76dSAlexander Graf         state->curl = curl_easy_init();
4129e550b32SMatthew Booth         if (!state->curl) {
413769ce76dSAlexander Graf             return NULL;
4149e550b32SMatthew Booth         }
415769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
41697a3ea57SMatthew Booth         curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
41797a3ea57SMatthew Booth                          (long) s->sslverify);
418a94f83d9SRichard W.M. Jones         if (s->cookie) {
419a94f83d9SRichard W.M. Jones             curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
420a94f83d9SRichard W.M. Jones         }
421f76faedaSRichard W.M. Jones         curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
4229e550b32SMatthew Booth         curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
4239e550b32SMatthew Booth                          (void *)curl_read_cb);
424769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
425769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
426769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
427769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
428769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
429769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
430f785a5aeSNicholas Thomas         curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
431769ce76dSAlexander Graf 
4321bff9606SDaniel P. Berrange         if (s->username) {
4331bff9606SDaniel P. Berrange             curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username);
4341bff9606SDaniel P. Berrange         }
4351bff9606SDaniel P. Berrange         if (s->password) {
4361bff9606SDaniel P. Berrange             curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password);
4371bff9606SDaniel P. Berrange         }
4381bff9606SDaniel P. Berrange         if (s->proxyusername) {
4391bff9606SDaniel P. Berrange             curl_easy_setopt(state->curl,
4401bff9606SDaniel P. Berrange                              CURLOPT_PROXYUSERNAME, s->proxyusername);
4411bff9606SDaniel P. Berrange         }
4421bff9606SDaniel P. Berrange         if (s->proxypassword) {
4431bff9606SDaniel P. Berrange             curl_easy_setopt(state->curl,
4441bff9606SDaniel P. Berrange                              CURLOPT_PROXYPASSWORD, s->proxypassword);
4451bff9606SDaniel P. Berrange         }
4461bff9606SDaniel P. Berrange 
447fb6d1bbdSStefan Hajnoczi         /* Restrict supported protocols to avoid security issues in the more
448fb6d1bbdSStefan Hajnoczi          * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
449fb6d1bbdSStefan Hajnoczi          * CVE-2013-0249.
4508a8f5840SStefan Hajnoczi          *
4518a8f5840SStefan Hajnoczi          * Restricting protocols is only supported from 7.19.4 upwards.
452fb6d1bbdSStefan Hajnoczi          */
4538a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304
454fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
455fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
4568a8f5840SStefan Hajnoczi #endif
457fb6d1bbdSStefan Hajnoczi 
458769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE
459769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
460769ce76dSAlexander Graf #endif
4619e550b32SMatthew Booth     }
462769ce76dSAlexander Graf 
463769ce76dSAlexander Graf     state->s = s;
464769ce76dSAlexander Graf 
465769ce76dSAlexander Graf     return state;
466769ce76dSAlexander Graf }
467769ce76dSAlexander Graf 
468769ce76dSAlexander Graf static void curl_clean_state(CURLState *s)
469769ce76dSAlexander Graf {
470769ce76dSAlexander Graf     if (s->s->multi)
471769ce76dSAlexander Graf         curl_multi_remove_handle(s->s->multi, s->curl);
472769ce76dSAlexander Graf     s->in_use = 0;
473769ce76dSAlexander Graf }
474769ce76dSAlexander Graf 
4758e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options,
4768e6d58cdSKevin Wolf                                 Error **errp)
477769ce76dSAlexander Graf {
478e3542c67SMatthew Booth     qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename));
4798e6d58cdSKevin Wolf }
4808e6d58cdSKevin Wolf 
48163f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs)
48263f0f45fSStefan Hajnoczi {
48363f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
48463f0f45fSStefan Hajnoczi     int i;
48563f0f45fSStefan Hajnoczi 
48663f0f45fSStefan Hajnoczi     for (i = 0; i < CURL_NUM_STATES; i++) {
48763f0f45fSStefan Hajnoczi         if (s->states[i].in_use) {
48863f0f45fSStefan Hajnoczi             curl_clean_state(&s->states[i]);
48963f0f45fSStefan Hajnoczi         }
49063f0f45fSStefan Hajnoczi         if (s->states[i].curl) {
49163f0f45fSStefan Hajnoczi             curl_easy_cleanup(s->states[i].curl);
49263f0f45fSStefan Hajnoczi             s->states[i].curl = NULL;
49363f0f45fSStefan Hajnoczi         }
49463f0f45fSStefan Hajnoczi         g_free(s->states[i].orig_buf);
49563f0f45fSStefan Hajnoczi         s->states[i].orig_buf = NULL;
49663f0f45fSStefan Hajnoczi     }
49763f0f45fSStefan Hajnoczi     if (s->multi) {
49863f0f45fSStefan Hajnoczi         curl_multi_cleanup(s->multi);
49963f0f45fSStefan Hajnoczi         s->multi = NULL;
50063f0f45fSStefan Hajnoczi     }
50163f0f45fSStefan Hajnoczi 
50263f0f45fSStefan Hajnoczi     timer_del(&s->timer);
50363f0f45fSStefan Hajnoczi }
50463f0f45fSStefan Hajnoczi 
50563f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs,
50663f0f45fSStefan Hajnoczi                                     AioContext *new_context)
50763f0f45fSStefan Hajnoczi {
50863f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
50963f0f45fSStefan Hajnoczi 
51063f0f45fSStefan Hajnoczi     aio_timer_init(new_context, &s->timer,
51163f0f45fSStefan Hajnoczi                    QEMU_CLOCK_REALTIME, SCALE_NS,
51263f0f45fSStefan Hajnoczi                    curl_multi_timeout_do, s);
51363f0f45fSStefan Hajnoczi 
51463f0f45fSStefan Hajnoczi     assert(!s->multi);
51563f0f45fSStefan Hajnoczi     s->multi = curl_multi_init();
51663f0f45fSStefan Hajnoczi     s->aio_context = new_context;
51763f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
51863f0f45fSStefan Hajnoczi #ifdef NEED_CURL_TIMER_CALLBACK
51963f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
52063f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
52163f0f45fSStefan Hajnoczi #endif
52263f0f45fSStefan Hajnoczi }
52363f0f45fSStefan Hajnoczi 
5248e6d58cdSKevin Wolf static QemuOptsList runtime_opts = {
5258e6d58cdSKevin Wolf     .name = "curl",
5268e6d58cdSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
5278e6d58cdSKevin Wolf     .desc = {
5288e6d58cdSKevin Wolf         {
529e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_URL,
5308e6d58cdSKevin Wolf             .type = QEMU_OPT_STRING,
5318e6d58cdSKevin Wolf             .help = "URL to open",
5328e6d58cdSKevin Wolf         },
5338e6d58cdSKevin Wolf         {
534e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_READAHEAD,
5358e6d58cdSKevin Wolf             .type = QEMU_OPT_SIZE,
5368e6d58cdSKevin Wolf             .help = "Readahead size",
5378e6d58cdSKevin Wolf         },
53897a3ea57SMatthew Booth         {
53997a3ea57SMatthew Booth             .name = CURL_BLOCK_OPT_SSLVERIFY,
54097a3ea57SMatthew Booth             .type = QEMU_OPT_BOOL,
54197a3ea57SMatthew Booth             .help = "Verify SSL certificate"
54297a3ea57SMatthew Booth         },
543212aefaaSDaniel Henrique Barboza         {
544212aefaaSDaniel Henrique Barboza             .name = CURL_BLOCK_OPT_TIMEOUT,
545212aefaaSDaniel Henrique Barboza             .type = QEMU_OPT_NUMBER,
546212aefaaSDaniel Henrique Barboza             .help = "Curl timeout"
547212aefaaSDaniel Henrique Barboza         },
548a94f83d9SRichard W.M. Jones         {
549a94f83d9SRichard W.M. Jones             .name = CURL_BLOCK_OPT_COOKIE,
550a94f83d9SRichard W.M. Jones             .type = QEMU_OPT_STRING,
551a94f83d9SRichard W.M. Jones             .help = "Pass the cookie or list of cookies with each request"
552a94f83d9SRichard W.M. Jones         },
5531bff9606SDaniel P. Berrange         {
5541bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_USERNAME,
5551bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
5561bff9606SDaniel P. Berrange             .help = "Username for HTTP auth"
5571bff9606SDaniel P. Berrange         },
5581bff9606SDaniel P. Berrange         {
5591bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_PASSWORD_SECRET,
5601bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
5611bff9606SDaniel P. Berrange             .help = "ID of secret used as password for HTTP auth",
5621bff9606SDaniel P. Berrange         },
5631bff9606SDaniel P. Berrange         {
5641bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_PROXY_USERNAME,
5651bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
5661bff9606SDaniel P. Berrange             .help = "Username for HTTP proxy auth"
5671bff9606SDaniel P. Berrange         },
5681bff9606SDaniel P. Berrange         {
5691bff9606SDaniel P. Berrange             .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
5701bff9606SDaniel P. Berrange             .type = QEMU_OPT_STRING,
5711bff9606SDaniel P. Berrange             .help = "ID of secret used as password for HTTP proxy auth",
5721bff9606SDaniel P. Berrange         },
5738e6d58cdSKevin Wolf         { /* end of list */ }
5748e6d58cdSKevin Wolf     },
5758e6d58cdSKevin Wolf };
5768e6d58cdSKevin Wolf 
5771bff9606SDaniel P. Berrange 
578015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags,
579015a1036SMax Reitz                      Error **errp)
5808e6d58cdSKevin Wolf {
5818e6d58cdSKevin Wolf     BDRVCURLState *s = bs->opaque;
5828e6d58cdSKevin Wolf     CURLState *state = NULL;
5838e6d58cdSKevin Wolf     QemuOpts *opts;
5848e6d58cdSKevin Wolf     Error *local_err = NULL;
5858e6d58cdSKevin Wolf     const char *file;
586a94f83d9SRichard W.M. Jones     const char *cookie;
5878e6d58cdSKevin Wolf     double d;
5881bff9606SDaniel P. Berrange     const char *secretid;
5898e6d58cdSKevin Wolf 
5908e6d58cdSKevin Wolf     static int inited = 0;
5918e6d58cdSKevin Wolf 
592a7cea2baSRichard W.M. Jones     if (flags & BDRV_O_RDWR) {
5932a94fee3SPaolo Bonzini         error_setg(errp, "curl block device does not support writes");
594a7cea2baSRichard W.M. Jones         return -EROFS;
595a7cea2baSRichard W.M. Jones     }
596a7cea2baSRichard W.M. Jones 
59787ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
5988e6d58cdSKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
59984d18f06SMarkus Armbruster     if (local_err) {
6002a94fee3SPaolo Bonzini         error_propagate(errp, local_err);
6018e6d58cdSKevin Wolf         goto out_noclean;
6028e6d58cdSKevin Wolf     }
6038e6d58cdSKevin Wolf 
604e3542c67SMatthew Booth     s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
605e3542c67SMatthew Booth                                           READ_AHEAD_DEFAULT);
606c76f4952SNolan     if ((s->readahead_size & 0x1ff) != 0) {
6072a94fee3SPaolo Bonzini         error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
608c76f4952SNolan                    s->readahead_size);
609c76f4952SNolan         goto out_noclean;
610c76f4952SNolan     }
611c76f4952SNolan 
612212aefaaSDaniel Henrique Barboza     s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
613212aefaaSDaniel Henrique Barboza                                      CURL_TIMEOUT_DEFAULT);
614f76faedaSRichard W.M. Jones     if (s->timeout > CURL_TIMEOUT_MAX) {
615f76faedaSRichard W.M. Jones         error_setg(errp, "timeout parameter is too large or negative");
616f76faedaSRichard W.M. Jones         goto out_noclean;
617f76faedaSRichard W.M. Jones     }
618212aefaaSDaniel Henrique Barboza 
61997a3ea57SMatthew Booth     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
62097a3ea57SMatthew Booth 
621a94f83d9SRichard W.M. Jones     cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
622a94f83d9SRichard W.M. Jones     s->cookie = g_strdup(cookie);
623a94f83d9SRichard W.M. Jones 
624e3542c67SMatthew Booth     file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
6258e6d58cdSKevin Wolf     if (file == NULL) {
6262a94fee3SPaolo Bonzini         error_setg(errp, "curl block driver requires an 'url' option");
6278e6d58cdSKevin Wolf         goto out_noclean;
6288e6d58cdSKevin Wolf     }
6298e6d58cdSKevin Wolf 
6301bff9606SDaniel P. Berrange     s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME));
6311bff9606SDaniel P. Berrange     secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET);
6321bff9606SDaniel P. Berrange 
6331bff9606SDaniel P. Berrange     if (secretid) {
6341bff9606SDaniel P. Berrange         s->password = qcrypto_secret_lookup_as_utf8(secretid, errp);
6351bff9606SDaniel P. Berrange         if (!s->password) {
6361bff9606SDaniel P. Berrange             goto out_noclean;
6371bff9606SDaniel P. Berrange         }
6381bff9606SDaniel P. Berrange     }
6391bff9606SDaniel P. Berrange 
6401bff9606SDaniel P. Berrange     s->proxyusername = g_strdup(
6411bff9606SDaniel P. Berrange         qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME));
6421bff9606SDaniel P. Berrange     secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET);
6431bff9606SDaniel P. Berrange     if (secretid) {
6441bff9606SDaniel P. Berrange         s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp);
6451bff9606SDaniel P. Berrange         if (!s->proxypassword) {
6461bff9606SDaniel P. Berrange             goto out_noclean;
6471bff9606SDaniel P. Berrange         }
6481bff9606SDaniel P. Berrange     }
6491bff9606SDaniel P. Berrange 
650769ce76dSAlexander Graf     if (!inited) {
651769ce76dSAlexander Graf         curl_global_init(CURL_GLOBAL_ALL);
652769ce76dSAlexander Graf         inited = 1;
653769ce76dSAlexander Graf     }
654769ce76dSAlexander Graf 
655d0f2c4c6Smalc     DPRINTF("CURL: Opening %s\n", file);
65663f0f45fSStefan Hajnoczi     s->aio_context = bdrv_get_aio_context(bs);
6578e6d58cdSKevin Wolf     s->url = g_strdup(file);
658a2f468e4SRichard W.M. Jones     state = curl_init_state(bs, s);
659769ce76dSAlexander Graf     if (!state)
660769ce76dSAlexander Graf         goto out_noclean;
661769ce76dSAlexander Graf 
662769ce76dSAlexander Graf     // Get file size
663769ce76dSAlexander Graf 
6643494d650SFam Zheng     s->accept_range = false;
665769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
6663494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
6673494d650SFam Zheng                      curl_header_cb);
6683494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
669769ce76dSAlexander Graf     if (curl_easy_perform(state->curl))
670769ce76dSAlexander Graf         goto out;
671769ce76dSAlexander Graf     curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
672769ce76dSAlexander Graf     if (d)
673769ce76dSAlexander Graf         s->len = (size_t)d;
674769ce76dSAlexander Graf     else if(!s->len)
675769ce76dSAlexander Graf         goto out;
6763494d650SFam Zheng     if ((!strncasecmp(s->url, "http://", strlen("http://"))
6773494d650SFam Zheng         || !strncasecmp(s->url, "https://", strlen("https://")))
6783494d650SFam Zheng         && !s->accept_range) {
6793494d650SFam Zheng         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
6803494d650SFam Zheng                 "Server does not support 'range' (byte ranges).");
6813494d650SFam Zheng         goto out;
6823494d650SFam Zheng     }
6830bfcd599SBlue Swirl     DPRINTF("CURL: Size = %zd\n", s->len);
684769ce76dSAlexander Graf 
685769ce76dSAlexander Graf     curl_clean_state(state);
686769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
687769ce76dSAlexander Graf     state->curl = NULL;
688769ce76dSAlexander Graf 
68963f0f45fSStefan Hajnoczi     curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
690769ce76dSAlexander Graf 
6918e6d58cdSKevin Wolf     qemu_opts_del(opts);
692769ce76dSAlexander Graf     return 0;
693769ce76dSAlexander Graf 
694769ce76dSAlexander Graf out:
695acd7fdc6SMaria Kustova     error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
696769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
697769ce76dSAlexander Graf     state->curl = NULL;
698769ce76dSAlexander Graf out_noclean:
699a94f83d9SRichard W.M. Jones     g_free(s->cookie);
7008e6d58cdSKevin Wolf     g_free(s->url);
7018e6d58cdSKevin Wolf     qemu_opts_del(opts);
702769ce76dSAlexander Graf     return -EINVAL;
703769ce76dSAlexander Graf }
704769ce76dSAlexander Graf 
705d7331bedSStefan Hajnoczi static const AIOCBInfo curl_aiocb_info = {
706c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(CURLAIOCB),
707c16b5a2cSChristoph Hellwig };
708c16b5a2cSChristoph Hellwig 
709363c3c85SNick Thomas 
710363c3c85SNick Thomas static void curl_readv_bh_cb(void *p)
711769ce76dSAlexander Graf {
712769ce76dSAlexander Graf     CURLState *state;
713b69cdef8SMatthew Booth     int running;
714769ce76dSAlexander Graf 
715363c3c85SNick Thomas     CURLAIOCB *acb = p;
716363c3c85SNick Thomas     BDRVCURLState *s = acb->common.bs->opaque;
717769ce76dSAlexander Graf 
718363c3c85SNick Thomas     qemu_bh_delete(acb->bh);
719363c3c85SNick Thomas     acb->bh = NULL;
720363c3c85SNick Thomas 
721363c3c85SNick Thomas     size_t start = acb->sector_num * SECTOR_SIZE;
722363c3c85SNick Thomas     size_t end;
723769ce76dSAlexander Graf 
724769ce76dSAlexander Graf     // In case we have the requested data already (e.g. read-ahead),
725769ce76dSAlexander Graf     // we can just call the callback and be done.
726363c3c85SNick Thomas     switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
727769ce76dSAlexander Graf         case FIND_RET_OK:
7288007429aSFam Zheng             qemu_aio_unref(acb);
729769ce76dSAlexander Graf             // fall through
730769ce76dSAlexander Graf         case FIND_RET_WAIT:
731363c3c85SNick Thomas             return;
732769ce76dSAlexander Graf         default:
733769ce76dSAlexander Graf             break;
734769ce76dSAlexander Graf     }
735769ce76dSAlexander Graf 
736769ce76dSAlexander Graf     // No cache found, so let's start a new request
737a2f468e4SRichard W.M. Jones     state = curl_init_state(acb->common.bs, s);
738363c3c85SNick Thomas     if (!state) {
739363c3c85SNick Thomas         acb->common.cb(acb->common.opaque, -EIO);
7408007429aSFam Zheng         qemu_aio_unref(acb);
741363c3c85SNick Thomas         return;
742363c3c85SNick Thomas     }
743769ce76dSAlexander Graf 
744769ce76dSAlexander Graf     acb->start = 0;
745363c3c85SNick Thomas     acb->end = (acb->nb_sectors * SECTOR_SIZE);
746769ce76dSAlexander Graf 
747769ce76dSAlexander Graf     state->buf_off = 0;
7487267c094SAnthony Liguori     g_free(state->orig_buf);
749769ce76dSAlexander Graf     state->buf_start = start;
750c76f4952SNolan     state->buf_len = acb->end + s->readahead_size;
751769ce76dSAlexander Graf     end = MIN(start + state->buf_len, s->len) - 1;
7528dc7a772SKevin Wolf     state->orig_buf = g_try_malloc(state->buf_len);
7538dc7a772SKevin Wolf     if (state->buf_len && state->orig_buf == NULL) {
7548dc7a772SKevin Wolf         curl_clean_state(state);
7558dc7a772SKevin Wolf         acb->common.cb(acb->common.opaque, -ENOMEM);
7568007429aSFam Zheng         qemu_aio_unref(acb);
7578dc7a772SKevin Wolf         return;
7588dc7a772SKevin Wolf     }
759769ce76dSAlexander Graf     state->acb[0] = acb;
760769ce76dSAlexander Graf 
7610bfcd599SBlue Swirl     snprintf(state->range, 127, "%zd-%zd", start, end);
7620bfcd599SBlue Swirl     DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
763363c3c85SNick Thomas             (acb->nb_sectors * SECTOR_SIZE), start, state->range);
764769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
765769ce76dSAlexander Graf 
766769ce76dSAlexander Graf     curl_multi_add_handle(s->multi, state->curl);
767769ce76dSAlexander Graf 
768b69cdef8SMatthew Booth     /* Tell curl it needs to kick things off */
769b69cdef8SMatthew Booth     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
770363c3c85SNick Thomas }
771363c3c85SNick Thomas 
7727c84b1b8SMarkus Armbruster static BlockAIOCB *curl_aio_readv(BlockDriverState *bs,
773363c3c85SNick Thomas         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
774097310b5SMarkus Armbruster         BlockCompletionFunc *cb, void *opaque)
775363c3c85SNick Thomas {
776363c3c85SNick Thomas     CURLAIOCB *acb;
777363c3c85SNick Thomas 
778d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
779363c3c85SNick Thomas 
780363c3c85SNick Thomas     acb->qiov = qiov;
781363c3c85SNick Thomas     acb->sector_num = sector_num;
782363c3c85SNick Thomas     acb->nb_sectors = nb_sectors;
783363c3c85SNick Thomas 
78463f0f45fSStefan Hajnoczi     acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
785363c3c85SNick Thomas     qemu_bh_schedule(acb->bh);
786769ce76dSAlexander Graf     return &acb->common;
787769ce76dSAlexander Graf }
788769ce76dSAlexander Graf 
789769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs)
790769ce76dSAlexander Graf {
791769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
792769ce76dSAlexander Graf 
793d0f2c4c6Smalc     DPRINTF("CURL: Close\n");
79463f0f45fSStefan Hajnoczi     curl_detach_aio_context(bs);
795031fd1beSPeter Maydell 
796a94f83d9SRichard W.M. Jones     g_free(s->cookie);
79745724d6dSStefan Weil     g_free(s->url);
798769ce76dSAlexander Graf }
799769ce76dSAlexander Graf 
800769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs)
801769ce76dSAlexander Graf {
802769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
803769ce76dSAlexander Graf     return s->len;
804769ce76dSAlexander Graf }
805769ce76dSAlexander Graf 
806769ce76dSAlexander Graf static BlockDriver bdrv_http = {
807769ce76dSAlexander Graf     .format_name                = "http",
808769ce76dSAlexander Graf     .protocol_name              = "http",
809769ce76dSAlexander Graf 
810769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
8118e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
81266f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
813769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
814769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
815769ce76dSAlexander Graf 
816769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
81763f0f45fSStefan Hajnoczi 
81863f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
81963f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
820769ce76dSAlexander Graf };
821769ce76dSAlexander Graf 
822769ce76dSAlexander Graf static BlockDriver bdrv_https = {
823769ce76dSAlexander Graf     .format_name                = "https",
824769ce76dSAlexander Graf     .protocol_name              = "https",
825769ce76dSAlexander Graf 
826769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
8278e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
82866f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
829769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
830769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
831769ce76dSAlexander Graf 
832769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
83363f0f45fSStefan Hajnoczi 
83463f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
83563f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
836769ce76dSAlexander Graf };
837769ce76dSAlexander Graf 
838769ce76dSAlexander Graf static BlockDriver bdrv_ftp = {
839769ce76dSAlexander Graf     .format_name                = "ftp",
840769ce76dSAlexander Graf     .protocol_name              = "ftp",
841769ce76dSAlexander Graf 
842769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
8438e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
84466f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
845769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
846769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
847769ce76dSAlexander Graf 
848769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
84963f0f45fSStefan Hajnoczi 
85063f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
85163f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
852769ce76dSAlexander Graf };
853769ce76dSAlexander Graf 
854769ce76dSAlexander Graf static BlockDriver bdrv_ftps = {
855769ce76dSAlexander Graf     .format_name                = "ftps",
856769ce76dSAlexander Graf     .protocol_name              = "ftps",
857769ce76dSAlexander Graf 
858769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
8598e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
86066f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
861769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
862769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
863769ce76dSAlexander Graf 
864769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
86563f0f45fSStefan Hajnoczi 
86663f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
86763f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
868769ce76dSAlexander Graf };
869769ce76dSAlexander Graf 
870769ce76dSAlexander Graf static BlockDriver bdrv_tftp = {
871769ce76dSAlexander Graf     .format_name                = "tftp",
872769ce76dSAlexander Graf     .protocol_name              = "tftp",
873769ce76dSAlexander Graf 
874769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
8758e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
87666f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
877769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
878769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
879769ce76dSAlexander Graf 
880769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
88163f0f45fSStefan Hajnoczi 
88263f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
88363f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
884769ce76dSAlexander Graf };
885769ce76dSAlexander Graf 
886769ce76dSAlexander Graf static void curl_block_init(void)
887769ce76dSAlexander Graf {
888769ce76dSAlexander Graf     bdrv_register(&bdrv_http);
889769ce76dSAlexander Graf     bdrv_register(&bdrv_https);
890769ce76dSAlexander Graf     bdrv_register(&bdrv_ftp);
891769ce76dSAlexander Graf     bdrv_register(&bdrv_ftps);
892769ce76dSAlexander Graf     bdrv_register(&bdrv_tftp);
893769ce76dSAlexander Graf }
894769ce76dSAlexander Graf 
895769ce76dSAlexander Graf block_init(curl_block_init);
896