blob: d4c8e94f3e0fe26ee221e763356e300359d42946 [file] [log] [blame]
Alexander Graf769ce762009-05-11 17:41:42 +02001/*
2 * QEMU Block driver for CURL images
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010024
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Richard W.M. Jones796a0602015-07-08 14:37:48 +010027#include "qemu/error-report.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020028#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010029#include "qemu/option.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010030#include "block/block_int.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010031#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010032#include "qapi/qmp/qstring.h"
Daniel P. Berrange1bff9602016-01-21 14:19:20 +000033#include "crypto/secret.h"
Alexander Graf769ce762009-05-11 17:41:42 +020034#include <curl/curl.h>
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020035#include "qemu/cutils.h"
Laurent Viviered2a66d2018-12-13 17:27:25 +010036#include "trace.h"
Alexander Graf769ce762009-05-11 17:41:42 +020037
Alexander Graf769ce762009-05-11 17:41:42 +020038// #define DEBUG_VERBOSE
39
Peter Maydell031fd1b2014-01-24 14:56:17 +010040#if LIBCURL_VERSION_NUM >= 0x071000
41/* The multi interface timer callback was introduced in 7.16.0 */
42#define NEED_CURL_TIMER_CALLBACK
Matthew Booth9aedd5a2014-05-14 19:28:40 -040043#define HAVE_SOCKET_ACTION
44#endif
45
46#ifndef HAVE_SOCKET_ACTION
47/* If curl_multi_socket_action isn't available, define it statically here in
48 * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
49 * less efficient but still safe. */
50static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
51 curl_socket_t sockfd,
52 int ev_bitmask,
53 int *running_handles)
54{
55 return curl_multi_socket(multi_handle, sockfd, running_handles);
56}
57#define curl_multi_socket_action __curl_multi_socket_action
Peter Maydell031fd1b2014-01-24 14:56:17 +010058#endif
59
Stefan Hajnoczifb6d1bb2013-02-08 08:49:10 +010060#define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
Max Reitz23dce382016-11-02 18:55:37 +010061 CURLPROTO_FTP | CURLPROTO_FTPS)
Stefan Hajnoczifb6d1bb2013-02-08 08:49:10 +010062
Alexander Graf769ce762009-05-11 17:41:42 +020063#define CURL_NUM_STATES 8
64#define CURL_NUM_ACB 8
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +000065#define CURL_TIMEOUT_MAX 10000
Alexander Graf769ce762009-05-11 17:41:42 +020066
Matthew Boothe3542c62014-05-14 19:28:41 -040067#define CURL_BLOCK_OPT_URL "url"
68#define CURL_BLOCK_OPT_READAHEAD "readahead"
Matthew Booth97a3ea52014-05-14 19:28:42 -040069#define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -030070#define CURL_BLOCK_OPT_TIMEOUT "timeout"
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +010071#define CURL_BLOCK_OPT_COOKIE "cookie"
Peter Krempa327c8eb2017-05-04 16:00:06 +020072#define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
Daniel P. Berrange1bff9602016-01-21 14:19:20 +000073#define CURL_BLOCK_OPT_USERNAME "username"
74#define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
75#define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
76#define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
Matthew Boothe3542c62014-05-14 19:28:41 -040077
Max Reitz712b64e2019-02-01 20:29:31 +010078#define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024)
79#define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true
80#define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5
81
Alexander Graf769ce762009-05-11 17:41:42 +020082struct BDRVCURLState;
83
Jeff Cody2d259642017-11-07 17:27:22 -050084static bool libcurl_initialized;
85
Alexander Graf769ce762009-05-11 17:41:42 +020086typedef struct CURLAIOCB {
Paolo Bonzini28256d82017-05-15 12:00:58 +020087 Coroutine *co;
Alexander Graf769ce762009-05-11 17:41:42 +020088 QEMUIOVector *qiov;
Nick Thomas363c3c82011-09-21 11:55:50 +010089
Paolo Bonzini2125e5e2017-05-15 12:00:57 +020090 uint64_t offset;
91 uint64_t bytes;
Paolo Bonzini28256d82017-05-15 12:00:58 +020092 int ret;
Nick Thomas363c3c82011-09-21 11:55:50 +010093
Alexander Graf769ce762009-05-11 17:41:42 +020094 size_t start;
95 size_t end;
96} CURLAIOCB;
97
Max Reitzff5ca162016-10-25 04:54:30 +020098typedef struct CURLSocket {
99 int fd;
100 QLIST_ENTRY(CURLSocket) next;
101} CURLSocket;
102
Alexander Graf769ce762009-05-11 17:41:42 +0200103typedef struct CURLState
104{
105 struct BDRVCURLState *s;
106 CURLAIOCB *acb[CURL_NUM_ACB];
107 CURL *curl;
Max Reitzff5ca162016-10-25 04:54:30 +0200108 QLIST_HEAD(, CURLSocket) sockets;
Alexander Graf769ce762009-05-11 17:41:42 +0200109 char *orig_buf;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200110 uint64_t buf_start;
Alexander Graf769ce762009-05-11 17:41:42 +0200111 size_t buf_off;
112 size_t buf_len;
113 char range[128];
114 char errmsg[CURL_ERROR_SIZE];
115 char in_use;
116} CURLState;
117
118typedef struct BDRVCURLState {
119 CURLM *multi;
Peter Maydell031fd1b2014-01-24 14:56:17 +0100120 QEMUTimer timer;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200121 uint64_t len;
Alexander Graf769ce762009-05-11 17:41:42 +0200122 CURLState states[CURL_NUM_STATES];
123 char *url;
Nolanc76f4952009-07-01 17:16:52 -0700124 size_t readahead_size;
Matthew Booth97a3ea52014-05-14 19:28:42 -0400125 bool sslverify;
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +0000126 uint64_t timeout;
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100127 char *cookie;
Fam Zheng3494d652013-07-02 15:19:21 +0800128 bool accept_range;
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200129 AioContext *aio_context;
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100130 QemuMutex mutex;
Paolo Bonzini709f2132018-02-03 10:39:35 -0500131 CoQueue free_state_waitq;
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000132 char *username;
133 char *password;
134 char *proxyusername;
135 char *proxypassword;
Alexander Graf769ce762009-05-11 17:41:42 +0200136} BDRVCURLState;
137
138static void curl_clean_state(CURLState *s);
139static void curl_multi_do(void *arg);
Matthew Booth838ef602014-04-29 16:03:30 +0100140static void curl_multi_read(void *arg);
Alexander Graf769ce762009-05-11 17:41:42 +0200141
Peter Maydell031fd1b2014-01-24 14:56:17 +0100142#ifdef NEED_CURL_TIMER_CALLBACK
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200143/* Called from curl_multi_do_locked, with s->mutex held. */
Peter Maydell031fd1b2014-01-24 14:56:17 +0100144static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
145{
146 BDRVCURLState *s = opaque;
147
Laurent Viviered2a66d2018-12-13 17:27:25 +0100148 trace_curl_timer_cb(timeout_ms);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100149 if (timeout_ms == -1) {
150 timer_del(&s->timer);
151 } else {
152 int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
153 timer_mod(&s->timer,
154 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
155 }
156 return 0;
157}
158#endif
159
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200160/* Called from curl_multi_do_locked, with s->mutex held. */
Alexander Graf769ce762009-05-11 17:41:42 +0200161static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200162 void *userp, void *sp)
Alexander Graf769ce762009-05-11 17:41:42 +0200163{
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200164 BDRVCURLState *s;
Matthew Booth838ef602014-04-29 16:03:30 +0100165 CURLState *state = NULL;
Max Reitzff5ca162016-10-25 04:54:30 +0200166 CURLSocket *socket;
167
Matthew Booth838ef602014-04-29 16:03:30 +0100168 curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200169 s = state->s;
Matthew Booth838ef602014-04-29 16:03:30 +0100170
Max Reitzff5ca162016-10-25 04:54:30 +0200171 QLIST_FOREACH(socket, &state->sockets, next) {
172 if (socket->fd == fd) {
173 if (action == CURL_POLL_REMOVE) {
174 QLIST_REMOVE(socket, next);
175 g_free(socket);
176 }
177 break;
178 }
179 }
180 if (!socket) {
181 socket = g_new0(CURLSocket, 1);
182 socket->fd = fd;
183 QLIST_INSERT_HEAD(&state->sockets, socket, next);
184 }
185 socket = NULL;
186
Laurent Viviered2a66d2018-12-13 17:27:25 +0100187 trace_curl_sock_cb(action, (int)fd);
Alexander Graf769ce762009-05-11 17:41:42 +0200188 switch (action) {
189 case CURL_POLL_IN:
Fam Zhengdca21ef2015-10-23 11:08:05 +0800190 aio_set_fd_handler(s->aio_context, fd, false,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000191 curl_multi_read, NULL, NULL, state);
Alexander Graf769ce762009-05-11 17:41:42 +0200192 break;
193 case CURL_POLL_OUT:
Fam Zhengdca21ef2015-10-23 11:08:05 +0800194 aio_set_fd_handler(s->aio_context, fd, false,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000195 NULL, curl_multi_do, NULL, state);
Alexander Graf769ce762009-05-11 17:41:42 +0200196 break;
197 case CURL_POLL_INOUT:
Fam Zhengdca21ef2015-10-23 11:08:05 +0800198 aio_set_fd_handler(s->aio_context, fd, false,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000199 curl_multi_read, curl_multi_do, NULL, state);
Alexander Graf769ce762009-05-11 17:41:42 +0200200 break;
201 case CURL_POLL_REMOVE:
Fam Zhengdca21ef2015-10-23 11:08:05 +0800202 aio_set_fd_handler(s->aio_context, fd, false,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000203 NULL, NULL, NULL, NULL);
Alexander Graf769ce762009-05-11 17:41:42 +0200204 break;
205 }
206
207 return 0;
208}
209
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200210/* Called from curl_multi_do_locked, with s->mutex held. */
Fam Zheng3494d652013-07-02 15:19:21 +0800211static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
Alexander Graf769ce762009-05-11 17:41:42 +0200212{
Fam Zheng3494d652013-07-02 15:19:21 +0800213 BDRVCURLState *s = opaque;
Alexander Graf769ce762009-05-11 17:41:42 +0200214 size_t realsize = size * nmemb;
Fam Zheng3494d652013-07-02 15:19:21 +0800215 const char *accept_line = "Accept-Ranges: bytes";
Alexander Graf769ce762009-05-11 17:41:42 +0200216
Fam Zheng3494d652013-07-02 15:19:21 +0800217 if (realsize >= strlen(accept_line)
218 && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
219 s->accept_range = true;
Blue Swirl0bfcd592010-05-22 08:02:12 +0000220 }
Alexander Graf769ce762009-05-11 17:41:42 +0200221
222 return realsize;
223}
224
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200225/* Called from curl_multi_do_locked, with s->mutex held. */
Alexander Graf769ce762009-05-11 17:41:42 +0200226static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
227{
228 CURLState *s = ((CURLState*)opaque);
229 size_t realsize = size * nmemb;
230 int i;
231
Laurent Viviered2a66d2018-12-13 17:27:25 +0100232 trace_curl_read_cb(realsize);
Alexander Graf769ce762009-05-11 17:41:42 +0200233
Max Reitz4e767652016-10-25 04:54:29 +0200234 if (!s || !s->orig_buf) {
235 goto read_end;
236 }
Alexander Graf769ce762009-05-11 17:41:42 +0200237
Fam Zheng6d4b9e52014-03-26 13:05:40 +0100238 if (s->buf_off >= s->buf_len) {
239 /* buffer full, read nothing */
Max Reitz4e767652016-10-25 04:54:29 +0200240 goto read_end;
Fam Zheng6d4b9e52014-03-26 13:05:40 +0100241 }
242 realsize = MIN(realsize, s->buf_len - s->buf_off);
Alexander Graf769ce762009-05-11 17:41:42 +0200243 memcpy(s->orig_buf + s->buf_off, ptr, realsize);
244 s->buf_off += realsize;
245
246 for(i=0; i<CURL_NUM_ACB; i++) {
247 CURLAIOCB *acb = s->acb[i];
248
249 if (!acb)
250 continue;
251
252 if ((s->buf_off >= acb->end)) {
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200253 size_t request_length = acb->bytes;
Max Reitz4e504532016-10-25 04:54:31 +0200254
Michael Tokarev03396142012-06-07 20:17:55 +0400255 qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
256 acb->end - acb->start);
Max Reitz4e504532016-10-25 04:54:31 +0200257
258 if (acb->end - acb->start < request_length) {
259 size_t offset = acb->end - acb->start;
260 qemu_iovec_memset(acb->qiov, offset, 0,
261 request_length - offset);
262 }
263
Paolo Bonzini28256d82017-05-15 12:00:58 +0200264 acb->ret = 0;
Alexander Graf769ce762009-05-11 17:41:42 +0200265 s->acb[i] = NULL;
Paolo Bonzini28256d82017-05-15 12:00:58 +0200266 qemu_mutex_unlock(&s->s->mutex);
267 aio_co_wake(acb->co);
268 qemu_mutex_lock(&s->s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200269 }
270 }
271
Max Reitz4e767652016-10-25 04:54:29 +0200272read_end:
273 /* curl will error out if we do not return this value */
274 return size * nmemb;
Alexander Graf769ce762009-05-11 17:41:42 +0200275}
276
Paolo Bonzini456af342017-05-15 12:00:55 +0200277/* Called with s->mutex held. */
Paolo Bonzini28256d82017-05-15 12:00:58 +0200278static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
279 CURLAIOCB *acb)
Alexander Graf769ce762009-05-11 17:41:42 +0200280{
281 int i;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200282 uint64_t end = start + len;
283 uint64_t clamped_end = MIN(end, s->len);
284 uint64_t clamped_len = clamped_end - start;
Alexander Graf769ce762009-05-11 17:41:42 +0200285
286 for (i=0; i<CURL_NUM_STATES; i++) {
287 CURLState *state = &s->states[i];
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200288 uint64_t buf_end = (state->buf_start + state->buf_off);
289 uint64_t buf_fend = (state->buf_start + state->buf_len);
Alexander Graf769ce762009-05-11 17:41:42 +0200290
291 if (!state->orig_buf)
292 continue;
293 if (!state->buf_off)
294 continue;
295
296 // Does the existing buffer cover our section?
297 if ((start >= state->buf_start) &&
298 (start <= buf_end) &&
Max Reitz4e504532016-10-25 04:54:31 +0200299 (clamped_end >= state->buf_start) &&
300 (clamped_end <= buf_end))
Alexander Graf769ce762009-05-11 17:41:42 +0200301 {
302 char *buf = state->orig_buf + (start - state->buf_start);
303
Max Reitz4e504532016-10-25 04:54:31 +0200304 qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len);
305 if (clamped_len < len) {
306 qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len);
307 }
Paolo Bonzini28256d82017-05-15 12:00:58 +0200308 acb->ret = 0;
309 return true;
Alexander Graf769ce762009-05-11 17:41:42 +0200310 }
311
312 // Wait for unfinished chunks
Matthew Boothb7079df2014-04-29 16:03:32 +0100313 if (state->in_use &&
314 (start >= state->buf_start) &&
Alexander Graf769ce762009-05-11 17:41:42 +0200315 (start <= buf_fend) &&
Max Reitz4e504532016-10-25 04:54:31 +0200316 (clamped_end >= state->buf_start) &&
317 (clamped_end <= buf_fend))
Alexander Graf769ce762009-05-11 17:41:42 +0200318 {
319 int j;
320
321 acb->start = start - state->buf_start;
Max Reitz4e504532016-10-25 04:54:31 +0200322 acb->end = acb->start + clamped_len;
Alexander Graf769ce762009-05-11 17:41:42 +0200323
324 for (j=0; j<CURL_NUM_ACB; j++) {
325 if (!state->acb[j]) {
326 state->acb[j] = acb;
Paolo Bonzini28256d82017-05-15 12:00:58 +0200327 return true;
Alexander Graf769ce762009-05-11 17:41:42 +0200328 }
329 }
330 }
331 }
332
Paolo Bonzini28256d82017-05-15 12:00:58 +0200333 return false;
Alexander Graf769ce762009-05-11 17:41:42 +0200334}
335
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100336/* Called with s->mutex held. */
Matthew Booth838ef602014-04-29 16:03:30 +0100337static void curl_multi_check_completion(BDRVCURLState *s)
Alexander Graf769ce762009-05-11 17:41:42 +0200338{
Alexander Graf769ce762009-05-11 17:41:42 +0200339 int msgs_in_queue;
340
Alexander Graf769ce762009-05-11 17:41:42 +0200341 /* Try to find done transfers, so we can free the easy
342 * handle again. */
Matthew Booth1f2cead2014-04-29 16:03:31 +0100343 for (;;) {
Alexander Graf769ce762009-05-11 17:41:42 +0200344 CURLMsg *msg;
345 msg = curl_multi_info_read(s->multi, &msgs_in_queue);
346
Matthew Booth1f2cead2014-04-29 16:03:31 +0100347 /* Quit when there are no more completions */
Alexander Graf769ce762009-05-11 17:41:42 +0200348 if (!msg)
349 break;
Alexander Graf769ce762009-05-11 17:41:42 +0200350
Matthew Booth1f2cead2014-04-29 16:03:31 +0100351 if (msg->msg == CURLMSG_DONE) {
352 CURLState *state = NULL;
353 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
354 (char **)&state);
Nicholas Thomasf785a5a2011-08-15 10:00:34 +0100355
Matthew Booth1f2cead2014-04-29 16:03:31 +0100356 /* ACBs for successful messages get completed in curl_read_cb */
357 if (msg->data.result != CURLE_OK) {
358 int i;
Richard W.M. Jones796a0602015-07-08 14:37:48 +0100359 static int errcount = 100;
360
361 /* Don't lose the original error message from curl, since
362 * it contains extra data.
363 */
364 if (errcount > 0) {
365 error_report("curl: %s", state->errmsg);
366 if (--errcount == 0) {
367 error_report("curl: further errors suppressed");
368 }
369 }
370
Matthew Booth1f2cead2014-04-29 16:03:31 +0100371 for (i = 0; i < CURL_NUM_ACB; i++) {
372 CURLAIOCB *acb = state->acb[i];
Nicholas Thomasf785a5a2011-08-15 10:00:34 +0100373
Matthew Booth1f2cead2014-04-29 16:03:31 +0100374 if (acb == NULL) {
375 continue;
Nicholas Thomasf785a5a2011-08-15 10:00:34 +0100376 }
Nicholas Thomasf785a5a2011-08-15 10:00:34 +0100377
Paolo Bonzini28256d82017-05-15 12:00:58 +0200378 acb->ret = -EIO;
Matthew Booth1f2cead2014-04-29 16:03:31 +0100379 state->acb[i] = NULL;
Paolo Bonzini28256d82017-05-15 12:00:58 +0200380 qemu_mutex_unlock(&s->mutex);
381 aio_co_wake(acb->co);
382 qemu_mutex_lock(&s->mutex);
Matthew Booth1f2cead2014-04-29 16:03:31 +0100383 }
Alexander Graf769ce762009-05-11 17:41:42 +0200384 }
Matthew Booth1f2cead2014-04-29 16:03:31 +0100385
386 curl_clean_state(state);
387 break;
Alexander Graf769ce762009-05-11 17:41:42 +0200388 }
Matthew Booth1f2cead2014-04-29 16:03:31 +0100389 }
Alexander Graf769ce762009-05-11 17:41:42 +0200390}
391
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100392/* Called with s->mutex held. */
Paolo Bonzini9d456652017-02-13 14:52:30 +0100393static void curl_multi_do_locked(CURLState *s)
Peter Maydell031fd1b2014-01-24 14:56:17 +0100394{
Max Reitzff5ca162016-10-25 04:54:30 +0200395 CURLSocket *socket, *next_socket;
Peter Maydell031fd1b2014-01-24 14:56:17 +0100396 int running;
397 int r;
398
Matthew Booth838ef602014-04-29 16:03:30 +0100399 if (!s->s->multi) {
Peter Maydell031fd1b2014-01-24 14:56:17 +0100400 return;
401 }
402
Max Reitzff5ca162016-10-25 04:54:30 +0200403 /* Need to use _SAFE because curl_multi_socket_action() may trigger
404 * curl_sock_cb() which might modify this list */
405 QLIST_FOREACH_SAFE(socket, &s->sockets, next, next_socket) {
406 do {
407 r = curl_multi_socket_action(s->s->multi, socket->fd, 0, &running);
408 } while (r == CURLM_CALL_MULTI_PERFORM);
409 }
Matthew Booth838ef602014-04-29 16:03:30 +0100410}
411
Paolo Bonzini9d456652017-02-13 14:52:30 +0100412static void curl_multi_do(void *arg)
413{
414 CURLState *s = (CURLState *)arg;
415
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100416 qemu_mutex_lock(&s->s->mutex);
Paolo Bonzini9d456652017-02-13 14:52:30 +0100417 curl_multi_do_locked(s);
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100418 qemu_mutex_unlock(&s->s->mutex);
Paolo Bonzini9d456652017-02-13 14:52:30 +0100419}
420
Matthew Booth838ef602014-04-29 16:03:30 +0100421static void curl_multi_read(void *arg)
422{
423 CURLState *s = (CURLState *)arg;
424
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100425 qemu_mutex_lock(&s->s->mutex);
Paolo Bonzini9d456652017-02-13 14:52:30 +0100426 curl_multi_do_locked(s);
Matthew Booth838ef602014-04-29 16:03:30 +0100427 curl_multi_check_completion(s->s);
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100428 qemu_mutex_unlock(&s->s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100429}
430
431static void curl_multi_timeout_do(void *arg)
432{
433#ifdef NEED_CURL_TIMER_CALLBACK
434 BDRVCURLState *s = (BDRVCURLState *)arg;
435 int running;
436
437 if (!s->multi) {
438 return;
439 }
440
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100441 qemu_mutex_lock(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100442 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
443
Matthew Booth838ef602014-04-29 16:03:30 +0100444 curl_multi_check_completion(s);
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100445 qemu_mutex_unlock(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100446#else
447 abort();
448#endif
449}
450
Paolo Bonzini456af342017-05-15 12:00:55 +0200451/* Called with s->mutex held. */
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200452static CURLState *curl_find_state(BDRVCURLState *s)
Alexander Graf769ce762009-05-11 17:41:42 +0200453{
454 CURLState *state = NULL;
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200455 int i;
Alexander Graf769ce762009-05-11 17:41:42 +0200456
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200457 for (i = 0; i < CURL_NUM_STATES; i++) {
458 if (!s->states[i].in_use) {
Alexander Graf769ce762009-05-11 17:41:42 +0200459 state = &s->states[i];
460 state->in_use = 1;
461 break;
462 }
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200463 }
464 return state;
465}
Alexander Graf769ce762009-05-11 17:41:42 +0200466
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200467static int curl_init_state(BDRVCURLState *s, CURLState *state)
468{
Matthew Booth9e550b32014-04-29 16:03:26 +0100469 if (!state->curl) {
470 state->curl = curl_easy_init();
471 if (!state->curl) {
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200472 return -EIO;
Matthew Booth9e550b32014-04-29 16:03:26 +0100473 }
474 curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
Matthew Booth97a3ea52014-05-14 19:28:42 -0400475 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
476 (long) s->sslverify);
Richard W.M. Jones637fa442018-09-14 10:56:22 +0100477 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST,
478 s->sslverify ? 2L : 0L);
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100479 if (s->cookie) {
480 curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
481 }
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +0000482 curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
Matthew Booth9e550b32014-04-29 16:03:26 +0100483 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
484 (void *)curl_read_cb);
485 curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
486 curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
487 curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
488 curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
489 curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
490 curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
491 curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
Alexander Graf769ce762009-05-11 17:41:42 +0200492
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000493 if (s->username) {
494 curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username);
495 }
496 if (s->password) {
497 curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password);
498 }
499 if (s->proxyusername) {
500 curl_easy_setopt(state->curl,
501 CURLOPT_PROXYUSERNAME, s->proxyusername);
502 }
503 if (s->proxypassword) {
504 curl_easy_setopt(state->curl,
505 CURLOPT_PROXYPASSWORD, s->proxypassword);
506 }
507
Matthew Booth9e550b32014-04-29 16:03:26 +0100508 /* Restrict supported protocols to avoid security issues in the more
509 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
510 * CVE-2013-0249.
511 *
512 * Restricting protocols is only supported from 7.19.4 upwards.
513 */
Stefan Hajnoczi8a8f5842013-02-13 09:25:34 +0100514#if LIBCURL_VERSION_NUM >= 0x071304
Matthew Booth9e550b32014-04-29 16:03:26 +0100515 curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
516 curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
Stefan Hajnoczi8a8f5842013-02-13 09:25:34 +0100517#endif
Stefan Hajnoczifb6d1bb2013-02-08 08:49:10 +0100518
Alexander Graf769ce762009-05-11 17:41:42 +0200519#ifdef DEBUG_VERBOSE
Matthew Booth9e550b32014-04-29 16:03:26 +0100520 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
Alexander Graf769ce762009-05-11 17:41:42 +0200521#endif
Matthew Booth9e550b32014-04-29 16:03:26 +0100522 }
Alexander Graf769ce762009-05-11 17:41:42 +0200523
Max Reitzff5ca162016-10-25 04:54:30 +0200524 QLIST_INIT(&state->sockets);
Alexander Graf769ce762009-05-11 17:41:42 +0200525 state->s = s;
526
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200527 return 0;
Alexander Graf769ce762009-05-11 17:41:42 +0200528}
529
Paolo Bonzini456af342017-05-15 12:00:55 +0200530/* Called with s->mutex held. */
Alexander Graf769ce762009-05-11 17:41:42 +0200531static void curl_clean_state(CURLState *s)
532{
Paolo Bonzini675a7752017-05-15 12:00:53 +0200533 int j;
534 for (j = 0; j < CURL_NUM_ACB; j++) {
535 assert(!s->acb[j]);
536 }
537
Alexander Graf769ce762009-05-11 17:41:42 +0200538 if (s->s->multi)
539 curl_multi_remove_handle(s->s->multi, s->curl);
Max Reitzff5ca162016-10-25 04:54:30 +0200540
541 while (!QLIST_EMPTY(&s->sockets)) {
542 CURLSocket *socket = QLIST_FIRST(&s->sockets);
543
544 QLIST_REMOVE(socket, next);
545 g_free(socket);
546 }
547
Alexander Graf769ce762009-05-11 17:41:42 +0200548 s->in_use = 0;
Paolo Bonzini2bb5c932017-05-15 12:00:59 +0200549
Paolo Bonzini709f2132018-02-03 10:39:35 -0500550 qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200551}
552
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200553static void curl_parse_filename(const char *filename, QDict *options,
554 Error **errp)
Alexander Graf769ce762009-05-11 17:41:42 +0200555{
Eric Blake46f5ac22017-04-27 16:58:17 -0500556 qdict_put_str(options, CURL_BLOCK_OPT_URL, filename);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200557}
558
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200559static void curl_detach_aio_context(BlockDriverState *bs)
560{
561 BDRVCURLState *s = bs->opaque;
562 int i;
563
Paolo Bonzini456af342017-05-15 12:00:55 +0200564 qemu_mutex_lock(&s->mutex);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200565 for (i = 0; i < CURL_NUM_STATES; i++) {
566 if (s->states[i].in_use) {
567 curl_clean_state(&s->states[i]);
568 }
569 if (s->states[i].curl) {
570 curl_easy_cleanup(s->states[i].curl);
571 s->states[i].curl = NULL;
572 }
Markus Armbrusterf7047c22014-06-06 18:25:12 +0200573 g_free(s->states[i].orig_buf);
574 s->states[i].orig_buf = NULL;
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200575 }
576 if (s->multi) {
577 curl_multi_cleanup(s->multi);
578 s->multi = NULL;
579 }
Paolo Bonzini456af342017-05-15 12:00:55 +0200580 qemu_mutex_unlock(&s->mutex);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200581
582 timer_del(&s->timer);
583}
584
585static void curl_attach_aio_context(BlockDriverState *bs,
586 AioContext *new_context)
587{
588 BDRVCURLState *s = bs->opaque;
589
590 aio_timer_init(new_context, &s->timer,
591 QEMU_CLOCK_REALTIME, SCALE_NS,
592 curl_multi_timeout_do, s);
593
594 assert(!s->multi);
595 s->multi = curl_multi_init();
596 s->aio_context = new_context;
597 curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
598#ifdef NEED_CURL_TIMER_CALLBACK
599 curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
600 curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
601#endif
602}
603
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200604static QemuOptsList runtime_opts = {
605 .name = "curl",
606 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
607 .desc = {
608 {
Matthew Boothe3542c62014-05-14 19:28:41 -0400609 .name = CURL_BLOCK_OPT_URL,
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200610 .type = QEMU_OPT_STRING,
611 .help = "URL to open",
612 },
613 {
Matthew Boothe3542c62014-05-14 19:28:41 -0400614 .name = CURL_BLOCK_OPT_READAHEAD,
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200615 .type = QEMU_OPT_SIZE,
616 .help = "Readahead size",
617 },
Matthew Booth97a3ea52014-05-14 19:28:42 -0400618 {
619 .name = CURL_BLOCK_OPT_SSLVERIFY,
620 .type = QEMU_OPT_BOOL,
621 .help = "Verify SSL certificate"
622 },
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -0300623 {
624 .name = CURL_BLOCK_OPT_TIMEOUT,
625 .type = QEMU_OPT_NUMBER,
626 .help = "Curl timeout"
627 },
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100628 {
629 .name = CURL_BLOCK_OPT_COOKIE,
630 .type = QEMU_OPT_STRING,
631 .help = "Pass the cookie or list of cookies with each request"
632 },
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000633 {
Peter Krempa327c8eb2017-05-04 16:00:06 +0200634 .name = CURL_BLOCK_OPT_COOKIE_SECRET,
635 .type = QEMU_OPT_STRING,
636 .help = "ID of secret used as cookie passed with each request"
637 },
638 {
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000639 .name = CURL_BLOCK_OPT_USERNAME,
640 .type = QEMU_OPT_STRING,
641 .help = "Username for HTTP auth"
642 },
643 {
644 .name = CURL_BLOCK_OPT_PASSWORD_SECRET,
645 .type = QEMU_OPT_STRING,
646 .help = "ID of secret used as password for HTTP auth",
647 },
648 {
649 .name = CURL_BLOCK_OPT_PROXY_USERNAME,
650 .type = QEMU_OPT_STRING,
651 .help = "Username for HTTP proxy auth"
652 },
653 {
654 .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
655 .type = QEMU_OPT_STRING,
656 .help = "ID of secret used as password for HTTP proxy auth",
657 },
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200658 { /* end of list */ }
659 },
660};
661
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000662
Max Reitz015a1032013-09-05 14:22:29 +0200663static int curl_open(BlockDriverState *bs, QDict *options, int flags,
664 Error **errp)
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200665{
666 BDRVCURLState *s = bs->opaque;
667 CURLState *state = NULL;
668 QemuOpts *opts;
669 Error *local_err = NULL;
670 const char *file;
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100671 const char *cookie;
Peter Krempa327c8eb2017-05-04 16:00:06 +0200672 const char *cookie_secret;
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200673 double d;
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000674 const char *secretid;
Max Reitz34634ca2017-03-31 14:04:31 +0200675 const char *protocol_delimiter;
Jeff Cody2d259642017-11-07 17:27:22 -0500676 int ret;
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200677
Kevin Wolf6ceef362018-10-08 17:27:18 +0200678 ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes",
679 errp);
680 if (ret < 0) {
681 return ret;
Richard W.M. Jonesa7cea2b2013-06-10 12:38:43 +0100682 }
683
Jeff Cody2d259642017-11-07 17:27:22 -0500684 if (!libcurl_initialized) {
685 ret = curl_global_init(CURL_GLOBAL_ALL);
686 if (ret) {
687 error_setg(errp, "libcurl initialization failed with %d", ret);
688 return -EIO;
689 }
690 libcurl_initialized = true;
691 }
692
Paolo Bonzini456af342017-05-15 12:00:55 +0200693 qemu_mutex_init(&s->mutex);
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800694 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200695 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100696 if (local_err) {
Paolo Bonzini2a94fee2014-02-17 14:43:57 +0100697 error_propagate(errp, local_err);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200698 goto out_noclean;
699 }
700
Matthew Boothe3542c62014-05-14 19:28:41 -0400701 s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
Max Reitz712b64e2019-02-01 20:29:31 +0100702 CURL_BLOCK_OPT_READAHEAD_DEFAULT);
Nolanc76f4952009-07-01 17:16:52 -0700703 if ((s->readahead_size & 0x1ff) != 0) {
Paolo Bonzini2a94fee2014-02-17 14:43:57 +0100704 error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
705 s->readahead_size);
Nolanc76f4952009-07-01 17:16:52 -0700706 goto out_noclean;
707 }
708
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -0300709 s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
Max Reitz712b64e2019-02-01 20:29:31 +0100710 CURL_BLOCK_OPT_TIMEOUT_DEFAULT);
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +0000711 if (s->timeout > CURL_TIMEOUT_MAX) {
712 error_setg(errp, "timeout parameter is too large or negative");
713 goto out_noclean;
714 }
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -0300715
Max Reitz712b64e2019-02-01 20:29:31 +0100716 s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY,
717 CURL_BLOCK_OPT_SSLVERIFY_DEFAULT);
Matthew Booth97a3ea52014-05-14 19:28:42 -0400718
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100719 cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
Peter Krempa327c8eb2017-05-04 16:00:06 +0200720 cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET);
721
722 if (cookie && cookie_secret) {
723 error_setg(errp,
724 "curl driver cannot handle both cookie and cookie secret");
725 goto out_noclean;
726 }
727
728 if (cookie_secret) {
729 s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp);
730 if (!s->cookie) {
731 goto out_noclean;
732 }
733 } else {
734 s->cookie = g_strdup(cookie);
735 }
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100736
Matthew Boothe3542c62014-05-14 19:28:41 -0400737 file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200738 if (file == NULL) {
Paolo Bonzini2a94fee2014-02-17 14:43:57 +0100739 error_setg(errp, "curl block driver requires an 'url' option");
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200740 goto out_noclean;
741 }
742
Max Reitz34634ca2017-03-31 14:04:31 +0200743 if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) ||
744 !strstart(protocol_delimiter, "://", NULL))
745 {
746 error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not "
747 "start with '%s://')", bs->drv->protocol_name, file,
748 bs->drv->protocol_name);
749 goto out_noclean;
750 }
751
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000752 s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME));
753 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET);
754
755 if (secretid) {
756 s->password = qcrypto_secret_lookup_as_utf8(secretid, errp);
757 if (!s->password) {
758 goto out_noclean;
759 }
760 }
761
762 s->proxyusername = g_strdup(
763 qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME));
764 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET);
765 if (secretid) {
766 s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp);
767 if (!s->proxypassword) {
768 goto out_noclean;
769 }
770 }
771
Laurent Viviered2a66d2018-12-13 17:27:25 +0100772 trace_curl_open(file);
Paolo Bonzini709f2132018-02-03 10:39:35 -0500773 qemu_co_queue_init(&s->free_state_waitq);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200774 s->aio_context = bdrv_get_aio_context(bs);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200775 s->url = g_strdup(file);
Paolo Bonzini456af342017-05-15 12:00:55 +0200776 qemu_mutex_lock(&s->mutex);
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200777 state = curl_find_state(s);
Paolo Bonzini456af342017-05-15 12:00:55 +0200778 qemu_mutex_unlock(&s->mutex);
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200779 if (!state) {
Alexander Graf769ce762009-05-11 17:41:42 +0200780 goto out_noclean;
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200781 }
Alexander Graf769ce762009-05-11 17:41:42 +0200782
783 // Get file size
784
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200785 if (curl_init_state(s, state) < 0) {
786 goto out;
787 }
788
Fam Zheng3494d652013-07-02 15:19:21 +0800789 s->accept_range = false;
Alexander Graf769ce762009-05-11 17:41:42 +0200790 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
Fam Zheng3494d652013-07-02 15:19:21 +0800791 curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
792 curl_header_cb);
793 curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
Alexander Graf769ce762009-05-11 17:41:42 +0200794 if (curl_easy_perform(state->curl))
795 goto out;
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200796 if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
Alexander Graf769ce762009-05-11 17:41:42 +0200797 goto out;
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200798 }
799 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
800 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
Stefan Weil50d6a8a2018-07-12 21:51:20 +0200801 * known and zero if it is really zero-length file. */
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200802#if LIBCURL_VERSION_NUM >= 0x071304
803 if (d < 0) {
804 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
805 "Server didn't report file size.");
806 goto out;
807 }
808#else
809 if (d <= 0) {
810 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
811 "Unknown file size or zero-length file.");
812 goto out;
813 }
814#endif
815
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200816 s->len = d;
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200817
Fam Zheng3494d652013-07-02 15:19:21 +0800818 if ((!strncasecmp(s->url, "http://", strlen("http://"))
819 || !strncasecmp(s->url, "https://", strlen("https://")))
820 && !s->accept_range) {
821 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
822 "Server does not support 'range' (byte ranges).");
823 goto out;
824 }
Laurent Viviered2a66d2018-12-13 17:27:25 +0100825 trace_curl_open_size(s->len);
Alexander Graf769ce762009-05-11 17:41:42 +0200826
Paolo Bonzini456af342017-05-15 12:00:55 +0200827 qemu_mutex_lock(&s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200828 curl_clean_state(state);
Paolo Bonzini456af342017-05-15 12:00:55 +0200829 qemu_mutex_unlock(&s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200830 curl_easy_cleanup(state->curl);
831 state->curl = NULL;
832
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200833 curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
Alexander Graf769ce762009-05-11 17:41:42 +0200834
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200835 qemu_opts_del(opts);
Alexander Graf769ce762009-05-11 17:41:42 +0200836 return 0;
837
838out:
Maria Kustovaacd7fdc2014-03-18 09:59:18 +0400839 error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
Alexander Graf769ce762009-05-11 17:41:42 +0200840 curl_easy_cleanup(state->curl);
841 state->curl = NULL;
842out_noclean:
Paolo Bonzini456af342017-05-15 12:00:55 +0200843 qemu_mutex_destroy(&s->mutex);
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100844 g_free(s->cookie);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200845 g_free(s->url);
Jeff Cody996922d2017-11-07 17:27:23 -0500846 g_free(s->username);
847 g_free(s->proxyusername);
848 g_free(s->proxypassword);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200849 qemu_opts_del(opts);
Alexander Graf769ce762009-05-11 17:41:42 +0200850 return -EINVAL;
851}
852
Paolo Bonzini28256d82017-05-15 12:00:58 +0200853static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)
Alexander Graf769ce762009-05-11 17:41:42 +0200854{
Alexander Graf769ce762009-05-11 17:41:42 +0200855 CURLState *state;
Matthew Boothb69cdef2014-04-29 16:03:29 +0100856 int running;
Alexander Graf769ce762009-05-11 17:41:42 +0200857
Paolo Bonzini19196312017-02-13 14:52:31 +0100858 BDRVCURLState *s = bs->opaque;
Alexander Graf769ce762009-05-11 17:41:42 +0200859
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200860 uint64_t start = acb->offset;
861 uint64_t end;
Alexander Graf769ce762009-05-11 17:41:42 +0200862
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100863 qemu_mutex_lock(&s->mutex);
Paolo Bonzini19196312017-02-13 14:52:31 +0100864
Alexander Graf769ce762009-05-11 17:41:42 +0200865 // In case we have the requested data already (e.g. read-ahead),
866 // we can just call the callback and be done.
Paolo Bonzini28256d82017-05-15 12:00:58 +0200867 if (curl_find_buf(s, start, acb->bytes, acb)) {
868 goto out;
Alexander Graf769ce762009-05-11 17:41:42 +0200869 }
870
871 // No cache found, so let's start a new request
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200872 for (;;) {
873 state = curl_find_state(s);
874 if (state) {
875 break;
876 }
Paolo Bonzini709f2132018-02-03 10:39:35 -0500877 qemu_co_queue_wait(&s->free_state_waitq, &s->mutex);
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200878 }
879
880 if (curl_init_state(s, state) < 0) {
881 curl_clean_state(state);
Paolo Bonzini28256d82017-05-15 12:00:58 +0200882 acb->ret = -EIO;
Paolo Bonzini19196312017-02-13 14:52:31 +0100883 goto out;
Nick Thomas363c3c82011-09-21 11:55:50 +0100884 }
Alexander Graf769ce762009-05-11 17:41:42 +0200885
886 acb->start = 0;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200887 acb->end = MIN(acb->bytes, s->len - start);
Alexander Graf769ce762009-05-11 17:41:42 +0200888
889 state->buf_off = 0;
Markus Armbrusterf7047c22014-06-06 18:25:12 +0200890 g_free(state->orig_buf);
Alexander Graf769ce762009-05-11 17:41:42 +0200891 state->buf_start = start;
Max Reitz4e504532016-10-25 04:54:31 +0200892 state->buf_len = MIN(acb->end + s->readahead_size, s->len - start);
893 end = start + state->buf_len - 1;
Kevin Wolf8dc7a772014-05-20 13:26:40 +0200894 state->orig_buf = g_try_malloc(state->buf_len);
895 if (state->buf_len && state->orig_buf == NULL) {
896 curl_clean_state(state);
Paolo Bonzini28256d82017-05-15 12:00:58 +0200897 acb->ret = -ENOMEM;
Paolo Bonzini19196312017-02-13 14:52:31 +0100898 goto out;
Kevin Wolf8dc7a772014-05-20 13:26:40 +0200899 }
Alexander Graf769ce762009-05-11 17:41:42 +0200900 state->acb[0] = acb;
901
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200902 snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end);
Laurent Viviered2a66d2018-12-13 17:27:25 +0100903 trace_curl_setup_preadv(acb->bytes, start, state->range);
Alexander Graf769ce762009-05-11 17:41:42 +0200904 curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
905
906 curl_multi_add_handle(s->multi, state->curl);
Alexander Graf769ce762009-05-11 17:41:42 +0200907
Matthew Boothb69cdef2014-04-29 16:03:29 +0100908 /* Tell curl it needs to kick things off */
909 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
Paolo Bonzini19196312017-02-13 14:52:31 +0100910
911out:
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100912 qemu_mutex_unlock(&s->mutex);
Nick Thomas363c3c82011-09-21 11:55:50 +0100913}
914
Paolo Bonzini28256d82017-05-15 12:00:58 +0200915static int coroutine_fn curl_co_preadv(BlockDriverState *bs,
916 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
Nick Thomas363c3c82011-09-21 11:55:50 +0100917{
Paolo Bonzini28256d82017-05-15 12:00:58 +0200918 CURLAIOCB acb = {
919 .co = qemu_coroutine_self(),
920 .ret = -EINPROGRESS,
921 .qiov = qiov,
922 .offset = offset,
923 .bytes = bytes
924 };
Nick Thomas363c3c82011-09-21 11:55:50 +0100925
Paolo Bonzini28256d82017-05-15 12:00:58 +0200926 curl_setup_preadv(bs, &acb);
927 while (acb.ret == -EINPROGRESS) {
928 qemu_coroutine_yield();
929 }
930 return acb.ret;
Alexander Graf769ce762009-05-11 17:41:42 +0200931}
932
Alexander Graf769ce762009-05-11 17:41:42 +0200933static void curl_close(BlockDriverState *bs)
934{
935 BDRVCURLState *s = bs->opaque;
Alexander Graf769ce762009-05-11 17:41:42 +0200936
Laurent Viviered2a66d2018-12-13 17:27:25 +0100937 trace_curl_close();
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200938 curl_detach_aio_context(bs);
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100939 qemu_mutex_destroy(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100940
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100941 g_free(s->cookie);
Stefan Weil45724d62012-09-01 11:06:45 +0200942 g_free(s->url);
Jeff Cody996922d2017-11-07 17:27:23 -0500943 g_free(s->username);
944 g_free(s->proxyusername);
945 g_free(s->proxypassword);
Alexander Graf769ce762009-05-11 17:41:42 +0200946}
947
948static int64_t curl_getlength(BlockDriverState *bs)
949{
950 BDRVCURLState *s = bs->opaque;
951 return s->len;
952}
953
Max Reitz937c0072019-02-01 20:29:32 +0100954static void curl_refresh_filename(BlockDriverState *bs)
955{
956 BDRVCURLState *s = bs->opaque;
957
958 /* "readahead" and "timeout" do not change the guest-visible data,
959 * so ignore them */
960 if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT ||
961 s->cookie || s->username || s->password || s->proxyusername ||
962 s->proxypassword)
963 {
964 return;
965 }
966
967 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url);
968}
969
970
Max Reitz26542672019-02-01 20:29:25 +0100971static const char *const curl_strong_runtime_opts[] = {
972 CURL_BLOCK_OPT_URL,
973 CURL_BLOCK_OPT_SSLVERIFY,
974 CURL_BLOCK_OPT_COOKIE,
975 CURL_BLOCK_OPT_COOKIE_SECRET,
976 CURL_BLOCK_OPT_USERNAME,
977 CURL_BLOCK_OPT_PASSWORD_SECRET,
978 CURL_BLOCK_OPT_PROXY_USERNAME,
979 CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
980
981 NULL
982};
983
Alexander Graf769ce762009-05-11 17:41:42 +0200984static BlockDriver bdrv_http = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200985 .format_name = "http",
986 .protocol_name = "http",
Alexander Graf769ce762009-05-11 17:41:42 +0200987
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200988 .instance_size = sizeof(BDRVCURLState),
989 .bdrv_parse_filename = curl_parse_filename,
990 .bdrv_file_open = curl_open,
991 .bdrv_close = curl_close,
992 .bdrv_getlength = curl_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +0200993
Paolo Bonzini28256d82017-05-15 12:00:58 +0200994 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200995
996 .bdrv_detach_aio_context = curl_detach_aio_context,
997 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +0100998
Max Reitz937c0072019-02-01 20:29:32 +0100999 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001000 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001001};
1002
1003static BlockDriver bdrv_https = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001004 .format_name = "https",
1005 .protocol_name = "https",
Alexander Graf769ce762009-05-11 17:41:42 +02001006
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001007 .instance_size = sizeof(BDRVCURLState),
1008 .bdrv_parse_filename = curl_parse_filename,
1009 .bdrv_file_open = curl_open,
1010 .bdrv_close = curl_close,
1011 .bdrv_getlength = curl_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +02001012
Paolo Bonzini28256d82017-05-15 12:00:58 +02001013 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001014
1015 .bdrv_detach_aio_context = curl_detach_aio_context,
1016 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01001017
Max Reitz937c0072019-02-01 20:29:32 +01001018 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001019 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001020};
1021
1022static BlockDriver bdrv_ftp = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001023 .format_name = "ftp",
1024 .protocol_name = "ftp",
Alexander Graf769ce762009-05-11 17:41:42 +02001025
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001026 .instance_size = sizeof(BDRVCURLState),
1027 .bdrv_parse_filename = curl_parse_filename,
1028 .bdrv_file_open = curl_open,
1029 .bdrv_close = curl_close,
1030 .bdrv_getlength = curl_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +02001031
Paolo Bonzini28256d82017-05-15 12:00:58 +02001032 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001033
1034 .bdrv_detach_aio_context = curl_detach_aio_context,
1035 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01001036
Max Reitz937c0072019-02-01 20:29:32 +01001037 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001038 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001039};
1040
1041static BlockDriver bdrv_ftps = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001042 .format_name = "ftps",
1043 .protocol_name = "ftps",
Alexander Graf769ce762009-05-11 17:41:42 +02001044
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001045 .instance_size = sizeof(BDRVCURLState),
1046 .bdrv_parse_filename = curl_parse_filename,
1047 .bdrv_file_open = curl_open,
1048 .bdrv_close = curl_close,
1049 .bdrv_getlength = curl_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +02001050
Paolo Bonzini28256d82017-05-15 12:00:58 +02001051 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001052
1053 .bdrv_detach_aio_context = curl_detach_aio_context,
1054 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01001055
Max Reitz937c0072019-02-01 20:29:32 +01001056 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001057 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001058};
1059
Alexander Graf769ce762009-05-11 17:41:42 +02001060static void curl_block_init(void)
1061{
1062 bdrv_register(&bdrv_http);
1063 bdrv_register(&bdrv_https);
1064 bdrv_register(&bdrv_ftp);
1065 bdrv_register(&bdrv_ftps);
Alexander Graf769ce762009-05-11 17:41:42 +02001066}
1067
1068block_init(curl_block_init);