curl: Report only ready sockets
[qemu/ar7.git] / block / curl.c
blobfd70f1ebc458f22f6d1a4bc01e1e3428b47fea04
1 /*
2 * QEMU Block driver for CURL images
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/error-report.h"
28 #include "qemu/module.h"
29 #include "qemu/option.h"
30 #include "block/block_int.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/qmp/qstring.h"
33 #include "crypto/secret.h"
34 #include <curl/curl.h>
35 #include "qemu/cutils.h"
36 #include "trace.h"
38 // #define DEBUG_VERBOSE
40 #if LIBCURL_VERSION_NUM >= 0x071000
41 /* The multi interface timer callback was introduced in 7.16.0 */
42 #define NEED_CURL_TIMER_CALLBACK
43 #define HAVE_SOCKET_ACTION
44 #endif
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. */
50 static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
51 curl_socket_t sockfd,
52 int ev_bitmask,
53 int *running_handles)
55 return curl_multi_socket(multi_handle, sockfd, running_handles);
57 #define curl_multi_socket_action __curl_multi_socket_action
58 #endif
60 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
61 CURLPROTO_FTP | CURLPROTO_FTPS)
63 #define CURL_NUM_STATES 8
64 #define CURL_NUM_ACB 8
65 #define CURL_TIMEOUT_MAX 10000
67 #define CURL_BLOCK_OPT_URL "url"
68 #define CURL_BLOCK_OPT_READAHEAD "readahead"
69 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
70 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
71 #define CURL_BLOCK_OPT_COOKIE "cookie"
72 #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
73 #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"
78 #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024)
79 #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true
80 #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5
82 struct BDRVCURLState;
83 struct CURLState;
85 static bool libcurl_initialized;
87 typedef struct CURLAIOCB {
88 Coroutine *co;
89 QEMUIOVector *qiov;
91 uint64_t offset;
92 uint64_t bytes;
93 int ret;
95 size_t start;
96 size_t end;
97 } CURLAIOCB;
99 typedef struct CURLSocket {
100 int fd;
101 struct CURLState *state;
102 QLIST_ENTRY(CURLSocket) next;
103 } CURLSocket;
105 typedef struct CURLState
107 struct BDRVCURLState *s;
108 CURLAIOCB *acb[CURL_NUM_ACB];
109 CURL *curl;
110 QLIST_HEAD(, CURLSocket) sockets;
111 char *orig_buf;
112 uint64_t buf_start;
113 size_t buf_off;
114 size_t buf_len;
115 char range[128];
116 char errmsg[CURL_ERROR_SIZE];
117 char in_use;
118 } CURLState;
120 typedef struct BDRVCURLState {
121 CURLM *multi;
122 QEMUTimer timer;
123 uint64_t len;
124 CURLState states[CURL_NUM_STATES];
125 char *url;
126 size_t readahead_size;
127 bool sslverify;
128 uint64_t timeout;
129 char *cookie;
130 bool accept_range;
131 AioContext *aio_context;
132 QemuMutex mutex;
133 CoQueue free_state_waitq;
134 char *username;
135 char *password;
136 char *proxyusername;
137 char *proxypassword;
138 } BDRVCURLState;
140 static void curl_clean_state(CURLState *s);
141 static void curl_multi_do(void *arg);
143 #ifdef NEED_CURL_TIMER_CALLBACK
144 /* Called from curl_multi_do_locked, with s->mutex held. */
145 static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
147 BDRVCURLState *s = opaque;
149 trace_curl_timer_cb(timeout_ms);
150 if (timeout_ms == -1) {
151 timer_del(&s->timer);
152 } else {
153 int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
154 timer_mod(&s->timer,
155 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
157 return 0;
159 #endif
161 /* Called from curl_multi_do_locked, with s->mutex held. */
162 static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
163 void *userp, void *sp)
165 BDRVCURLState *s;
166 CURLState *state = NULL;
167 CURLSocket *socket;
169 curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
170 s = state->s;
172 QLIST_FOREACH(socket, &state->sockets, next) {
173 if (socket->fd == fd) {
174 break;
177 if (!socket) {
178 socket = g_new0(CURLSocket, 1);
179 socket->fd = fd;
180 socket->state = state;
181 QLIST_INSERT_HEAD(&state->sockets, socket, next);
184 trace_curl_sock_cb(action, (int)fd);
185 switch (action) {
186 case CURL_POLL_IN:
187 aio_set_fd_handler(s->aio_context, fd, false,
188 curl_multi_do, NULL, NULL, socket);
189 break;
190 case CURL_POLL_OUT:
191 aio_set_fd_handler(s->aio_context, fd, false,
192 NULL, curl_multi_do, NULL, socket);
193 break;
194 case CURL_POLL_INOUT:
195 aio_set_fd_handler(s->aio_context, fd, false,
196 curl_multi_do, curl_multi_do, NULL, socket);
197 break;
198 case CURL_POLL_REMOVE:
199 aio_set_fd_handler(s->aio_context, fd, false,
200 NULL, NULL, NULL, NULL);
201 break;
204 if (action == CURL_POLL_REMOVE) {
205 QLIST_REMOVE(socket, next);
206 g_free(socket);
209 return 0;
212 /* Called from curl_multi_do_locked, with s->mutex held. */
213 static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
215 BDRVCURLState *s = opaque;
216 size_t realsize = size * nmemb;
217 const char *accept_line = "Accept-Ranges: bytes";
219 if (realsize >= strlen(accept_line)
220 && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
221 s->accept_range = true;
224 return realsize;
227 /* Called from curl_multi_do_locked, with s->mutex held. */
228 static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
230 CURLState *s = ((CURLState*)opaque);
231 size_t realsize = size * nmemb;
232 int i;
234 trace_curl_read_cb(realsize);
236 if (!s || !s->orig_buf) {
237 goto read_end;
240 if (s->buf_off >= s->buf_len) {
241 /* buffer full, read nothing */
242 goto read_end;
244 realsize = MIN(realsize, s->buf_len - s->buf_off);
245 memcpy(s->orig_buf + s->buf_off, ptr, realsize);
246 s->buf_off += realsize;
248 for(i=0; i<CURL_NUM_ACB; i++) {
249 CURLAIOCB *acb = s->acb[i];
251 if (!acb)
252 continue;
254 if ((s->buf_off >= acb->end)) {
255 size_t request_length = acb->bytes;
257 qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
258 acb->end - acb->start);
260 if (acb->end - acb->start < request_length) {
261 size_t offset = acb->end - acb->start;
262 qemu_iovec_memset(acb->qiov, offset, 0,
263 request_length - offset);
266 acb->ret = 0;
267 s->acb[i] = NULL;
268 qemu_mutex_unlock(&s->s->mutex);
269 aio_co_wake(acb->co);
270 qemu_mutex_lock(&s->s->mutex);
274 read_end:
275 /* curl will error out if we do not return this value */
276 return size * nmemb;
279 /* Called with s->mutex held. */
280 static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
281 CURLAIOCB *acb)
283 int i;
284 uint64_t end = start + len;
285 uint64_t clamped_end = MIN(end, s->len);
286 uint64_t clamped_len = clamped_end - start;
288 for (i=0; i<CURL_NUM_STATES; i++) {
289 CURLState *state = &s->states[i];
290 uint64_t buf_end = (state->buf_start + state->buf_off);
291 uint64_t buf_fend = (state->buf_start + state->buf_len);
293 if (!state->orig_buf)
294 continue;
295 if (!state->buf_off)
296 continue;
298 // Does the existing buffer cover our section?
299 if ((start >= state->buf_start) &&
300 (start <= buf_end) &&
301 (clamped_end >= state->buf_start) &&
302 (clamped_end <= buf_end))
304 char *buf = state->orig_buf + (start - state->buf_start);
306 qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len);
307 if (clamped_len < len) {
308 qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len);
310 acb->ret = 0;
311 return true;
314 // Wait for unfinished chunks
315 if (state->in_use &&
316 (start >= state->buf_start) &&
317 (start <= buf_fend) &&
318 (clamped_end >= state->buf_start) &&
319 (clamped_end <= buf_fend))
321 int j;
323 acb->start = start - state->buf_start;
324 acb->end = acb->start + clamped_len;
326 for (j=0; j<CURL_NUM_ACB; j++) {
327 if (!state->acb[j]) {
328 state->acb[j] = acb;
329 return true;
335 return false;
338 /* Called with s->mutex held. */
339 static void curl_multi_check_completion(BDRVCURLState *s)
341 int msgs_in_queue;
343 /* Try to find done transfers, so we can free the easy
344 * handle again. */
345 for (;;) {
346 CURLMsg *msg;
347 msg = curl_multi_info_read(s->multi, &msgs_in_queue);
349 /* Quit when there are no more completions */
350 if (!msg)
351 break;
353 if (msg->msg == CURLMSG_DONE) {
354 CURLState *state = NULL;
355 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
356 (char **)&state);
358 /* ACBs for successful messages get completed in curl_read_cb */
359 if (msg->data.result != CURLE_OK) {
360 int i;
361 static int errcount = 100;
363 /* Don't lose the original error message from curl, since
364 * it contains extra data.
366 if (errcount > 0) {
367 error_report("curl: %s", state->errmsg);
368 if (--errcount == 0) {
369 error_report("curl: further errors suppressed");
373 for (i = 0; i < CURL_NUM_ACB; i++) {
374 CURLAIOCB *acb = state->acb[i];
376 if (acb == NULL) {
377 continue;
380 acb->ret = -EIO;
381 state->acb[i] = NULL;
382 qemu_mutex_unlock(&s->mutex);
383 aio_co_wake(acb->co);
384 qemu_mutex_lock(&s->mutex);
388 curl_clean_state(state);
389 break;
394 /* Called with s->mutex held. */
395 static void curl_multi_do_locked(CURLSocket *socket)
397 BDRVCURLState *s = socket->state->s;
398 int running;
399 int r;
401 if (!s->multi) {
402 return;
405 do {
406 r = curl_multi_socket_action(s->multi, socket->fd, 0, &running);
407 } while (r == CURLM_CALL_MULTI_PERFORM);
410 static void curl_multi_do(void *arg)
412 CURLSocket *socket = arg;
413 BDRVCURLState *s = socket->state->s;
415 qemu_mutex_lock(&s->mutex);
416 curl_multi_do_locked(socket);
417 curl_multi_check_completion(s);
418 qemu_mutex_unlock(&s->mutex);
421 static void curl_multi_timeout_do(void *arg)
423 #ifdef NEED_CURL_TIMER_CALLBACK
424 BDRVCURLState *s = (BDRVCURLState *)arg;
425 int running;
427 if (!s->multi) {
428 return;
431 qemu_mutex_lock(&s->mutex);
432 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
434 curl_multi_check_completion(s);
435 qemu_mutex_unlock(&s->mutex);
436 #else
437 abort();
438 #endif
441 /* Called with s->mutex held. */
442 static CURLState *curl_find_state(BDRVCURLState *s)
444 CURLState *state = NULL;
445 int i;
447 for (i = 0; i < CURL_NUM_STATES; i++) {
448 if (!s->states[i].in_use) {
449 state = &s->states[i];
450 state->in_use = 1;
451 break;
454 return state;
457 static int curl_init_state(BDRVCURLState *s, CURLState *state)
459 if (!state->curl) {
460 state->curl = curl_easy_init();
461 if (!state->curl) {
462 return -EIO;
464 curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
465 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
466 (long) s->sslverify);
467 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST,
468 s->sslverify ? 2L : 0L);
469 if (s->cookie) {
470 curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
472 curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
473 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
474 (void *)curl_read_cb);
475 curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
476 curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
477 curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
478 curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
479 curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
480 curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
481 curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
483 if (s->username) {
484 curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username);
486 if (s->password) {
487 curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password);
489 if (s->proxyusername) {
490 curl_easy_setopt(state->curl,
491 CURLOPT_PROXYUSERNAME, s->proxyusername);
493 if (s->proxypassword) {
494 curl_easy_setopt(state->curl,
495 CURLOPT_PROXYPASSWORD, s->proxypassword);
498 /* Restrict supported protocols to avoid security issues in the more
499 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
500 * CVE-2013-0249.
502 * Restricting protocols is only supported from 7.19.4 upwards.
504 #if LIBCURL_VERSION_NUM >= 0x071304
505 curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
506 curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
507 #endif
509 #ifdef DEBUG_VERBOSE
510 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
511 #endif
514 QLIST_INIT(&state->sockets);
515 state->s = s;
517 return 0;
520 /* Called with s->mutex held. */
521 static void curl_clean_state(CURLState *s)
523 int j;
524 for (j = 0; j < CURL_NUM_ACB; j++) {
525 assert(!s->acb[j]);
528 if (s->s->multi)
529 curl_multi_remove_handle(s->s->multi, s->curl);
531 while (!QLIST_EMPTY(&s->sockets)) {
532 CURLSocket *socket = QLIST_FIRST(&s->sockets);
534 QLIST_REMOVE(socket, next);
535 g_free(socket);
538 s->in_use = 0;
540 qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex);
543 static void curl_parse_filename(const char *filename, QDict *options,
544 Error **errp)
546 qdict_put_str(options, CURL_BLOCK_OPT_URL, filename);
549 static void curl_detach_aio_context(BlockDriverState *bs)
551 BDRVCURLState *s = bs->opaque;
552 int i;
554 qemu_mutex_lock(&s->mutex);
555 for (i = 0; i < CURL_NUM_STATES; i++) {
556 if (s->states[i].in_use) {
557 curl_clean_state(&s->states[i]);
559 if (s->states[i].curl) {
560 curl_easy_cleanup(s->states[i].curl);
561 s->states[i].curl = NULL;
563 g_free(s->states[i].orig_buf);
564 s->states[i].orig_buf = NULL;
566 if (s->multi) {
567 curl_multi_cleanup(s->multi);
568 s->multi = NULL;
570 qemu_mutex_unlock(&s->mutex);
572 timer_del(&s->timer);
575 static void curl_attach_aio_context(BlockDriverState *bs,
576 AioContext *new_context)
578 BDRVCURLState *s = bs->opaque;
580 aio_timer_init(new_context, &s->timer,
581 QEMU_CLOCK_REALTIME, SCALE_NS,
582 curl_multi_timeout_do, s);
584 assert(!s->multi);
585 s->multi = curl_multi_init();
586 s->aio_context = new_context;
587 curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
588 #ifdef NEED_CURL_TIMER_CALLBACK
589 curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
590 curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
591 #endif
594 static QemuOptsList runtime_opts = {
595 .name = "curl",
596 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
597 .desc = {
599 .name = CURL_BLOCK_OPT_URL,
600 .type = QEMU_OPT_STRING,
601 .help = "URL to open",
604 .name = CURL_BLOCK_OPT_READAHEAD,
605 .type = QEMU_OPT_SIZE,
606 .help = "Readahead size",
609 .name = CURL_BLOCK_OPT_SSLVERIFY,
610 .type = QEMU_OPT_BOOL,
611 .help = "Verify SSL certificate"
614 .name = CURL_BLOCK_OPT_TIMEOUT,
615 .type = QEMU_OPT_NUMBER,
616 .help = "Curl timeout"
619 .name = CURL_BLOCK_OPT_COOKIE,
620 .type = QEMU_OPT_STRING,
621 .help = "Pass the cookie or list of cookies with each request"
624 .name = CURL_BLOCK_OPT_COOKIE_SECRET,
625 .type = QEMU_OPT_STRING,
626 .help = "ID of secret used as cookie passed with each request"
629 .name = CURL_BLOCK_OPT_USERNAME,
630 .type = QEMU_OPT_STRING,
631 .help = "Username for HTTP auth"
634 .name = CURL_BLOCK_OPT_PASSWORD_SECRET,
635 .type = QEMU_OPT_STRING,
636 .help = "ID of secret used as password for HTTP auth",
639 .name = CURL_BLOCK_OPT_PROXY_USERNAME,
640 .type = QEMU_OPT_STRING,
641 .help = "Username for HTTP proxy auth"
644 .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
645 .type = QEMU_OPT_STRING,
646 .help = "ID of secret used as password for HTTP proxy auth",
648 { /* end of list */ }
653 static int curl_open(BlockDriverState *bs, QDict *options, int flags,
654 Error **errp)
656 BDRVCURLState *s = bs->opaque;
657 CURLState *state = NULL;
658 QemuOpts *opts;
659 Error *local_err = NULL;
660 const char *file;
661 const char *cookie;
662 const char *cookie_secret;
663 double d;
664 const char *secretid;
665 const char *protocol_delimiter;
666 int ret;
668 ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes",
669 errp);
670 if (ret < 0) {
671 return ret;
674 if (!libcurl_initialized) {
675 ret = curl_global_init(CURL_GLOBAL_ALL);
676 if (ret) {
677 error_setg(errp, "libcurl initialization failed with %d", ret);
678 return -EIO;
680 libcurl_initialized = true;
683 qemu_mutex_init(&s->mutex);
684 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
685 qemu_opts_absorb_qdict(opts, options, &local_err);
686 if (local_err) {
687 error_propagate(errp, local_err);
688 goto out_noclean;
691 s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
692 CURL_BLOCK_OPT_READAHEAD_DEFAULT);
693 if ((s->readahead_size & 0x1ff) != 0) {
694 error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
695 s->readahead_size);
696 goto out_noclean;
699 s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
700 CURL_BLOCK_OPT_TIMEOUT_DEFAULT);
701 if (s->timeout > CURL_TIMEOUT_MAX) {
702 error_setg(errp, "timeout parameter is too large or negative");
703 goto out_noclean;
706 s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY,
707 CURL_BLOCK_OPT_SSLVERIFY_DEFAULT);
709 cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
710 cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET);
712 if (cookie && cookie_secret) {
713 error_setg(errp,
714 "curl driver cannot handle both cookie and cookie secret");
715 goto out_noclean;
718 if (cookie_secret) {
719 s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp);
720 if (!s->cookie) {
721 goto out_noclean;
723 } else {
724 s->cookie = g_strdup(cookie);
727 file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
728 if (file == NULL) {
729 error_setg(errp, "curl block driver requires an 'url' option");
730 goto out_noclean;
733 if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) ||
734 !strstart(protocol_delimiter, "://", NULL))
736 error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not "
737 "start with '%s://')", bs->drv->protocol_name, file,
738 bs->drv->protocol_name);
739 goto out_noclean;
742 s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME));
743 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET);
745 if (secretid) {
746 s->password = qcrypto_secret_lookup_as_utf8(secretid, errp);
747 if (!s->password) {
748 goto out_noclean;
752 s->proxyusername = g_strdup(
753 qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME));
754 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET);
755 if (secretid) {
756 s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp);
757 if (!s->proxypassword) {
758 goto out_noclean;
762 trace_curl_open(file);
763 qemu_co_queue_init(&s->free_state_waitq);
764 s->aio_context = bdrv_get_aio_context(bs);
765 s->url = g_strdup(file);
766 qemu_mutex_lock(&s->mutex);
767 state = curl_find_state(s);
768 qemu_mutex_unlock(&s->mutex);
769 if (!state) {
770 goto out_noclean;
773 // Get file size
775 if (curl_init_state(s, state) < 0) {
776 goto out;
779 s->accept_range = false;
780 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
781 curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
782 curl_header_cb);
783 curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
784 if (curl_easy_perform(state->curl))
785 goto out;
786 if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
787 goto out;
789 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
790 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
791 * known and zero if it is really zero-length file. */
792 #if LIBCURL_VERSION_NUM >= 0x071304
793 if (d < 0) {
794 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
795 "Server didn't report file size.");
796 goto out;
798 #else
799 if (d <= 0) {
800 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
801 "Unknown file size or zero-length file.");
802 goto out;
804 #endif
806 s->len = d;
808 if ((!strncasecmp(s->url, "http://", strlen("http://"))
809 || !strncasecmp(s->url, "https://", strlen("https://")))
810 && !s->accept_range) {
811 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
812 "Server does not support 'range' (byte ranges).");
813 goto out;
815 trace_curl_open_size(s->len);
817 qemu_mutex_lock(&s->mutex);
818 curl_clean_state(state);
819 qemu_mutex_unlock(&s->mutex);
820 curl_easy_cleanup(state->curl);
821 state->curl = NULL;
823 curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
825 qemu_opts_del(opts);
826 return 0;
828 out:
829 error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
830 curl_easy_cleanup(state->curl);
831 state->curl = NULL;
832 out_noclean:
833 qemu_mutex_destroy(&s->mutex);
834 g_free(s->cookie);
835 g_free(s->url);
836 g_free(s->username);
837 g_free(s->proxyusername);
838 g_free(s->proxypassword);
839 qemu_opts_del(opts);
840 return -EINVAL;
843 static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)
845 CURLState *state;
846 int running;
848 BDRVCURLState *s = bs->opaque;
850 uint64_t start = acb->offset;
851 uint64_t end;
853 qemu_mutex_lock(&s->mutex);
855 // In case we have the requested data already (e.g. read-ahead),
856 // we can just call the callback and be done.
857 if (curl_find_buf(s, start, acb->bytes, acb)) {
858 goto out;
861 // No cache found, so let's start a new request
862 for (;;) {
863 state = curl_find_state(s);
864 if (state) {
865 break;
867 qemu_co_queue_wait(&s->free_state_waitq, &s->mutex);
870 if (curl_init_state(s, state) < 0) {
871 curl_clean_state(state);
872 acb->ret = -EIO;
873 goto out;
876 acb->start = 0;
877 acb->end = MIN(acb->bytes, s->len - start);
879 state->buf_off = 0;
880 g_free(state->orig_buf);
881 state->buf_start = start;
882 state->buf_len = MIN(acb->end + s->readahead_size, s->len - start);
883 end = start + state->buf_len - 1;
884 state->orig_buf = g_try_malloc(state->buf_len);
885 if (state->buf_len && state->orig_buf == NULL) {
886 curl_clean_state(state);
887 acb->ret = -ENOMEM;
888 goto out;
890 state->acb[0] = acb;
892 snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end);
893 trace_curl_setup_preadv(acb->bytes, start, state->range);
894 curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
896 curl_multi_add_handle(s->multi, state->curl);
898 /* Tell curl it needs to kick things off */
899 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
901 out:
902 qemu_mutex_unlock(&s->mutex);
905 static int coroutine_fn curl_co_preadv(BlockDriverState *bs,
906 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
908 CURLAIOCB acb = {
909 .co = qemu_coroutine_self(),
910 .ret = -EINPROGRESS,
911 .qiov = qiov,
912 .offset = offset,
913 .bytes = bytes
916 curl_setup_preadv(bs, &acb);
917 while (acb.ret == -EINPROGRESS) {
918 qemu_coroutine_yield();
920 return acb.ret;
923 static void curl_close(BlockDriverState *bs)
925 BDRVCURLState *s = bs->opaque;
927 trace_curl_close();
928 curl_detach_aio_context(bs);
929 qemu_mutex_destroy(&s->mutex);
931 g_free(s->cookie);
932 g_free(s->url);
933 g_free(s->username);
934 g_free(s->proxyusername);
935 g_free(s->proxypassword);
938 static int64_t curl_getlength(BlockDriverState *bs)
940 BDRVCURLState *s = bs->opaque;
941 return s->len;
944 static void curl_refresh_filename(BlockDriverState *bs)
946 BDRVCURLState *s = bs->opaque;
948 /* "readahead" and "timeout" do not change the guest-visible data,
949 * so ignore them */
950 if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT ||
951 s->cookie || s->username || s->password || s->proxyusername ||
952 s->proxypassword)
954 return;
957 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url);
961 static const char *const curl_strong_runtime_opts[] = {
962 CURL_BLOCK_OPT_URL,
963 CURL_BLOCK_OPT_SSLVERIFY,
964 CURL_BLOCK_OPT_COOKIE,
965 CURL_BLOCK_OPT_COOKIE_SECRET,
966 CURL_BLOCK_OPT_USERNAME,
967 CURL_BLOCK_OPT_PASSWORD_SECRET,
968 CURL_BLOCK_OPT_PROXY_USERNAME,
969 CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
971 NULL
974 static BlockDriver bdrv_http = {
975 .format_name = "http",
976 .protocol_name = "http",
978 .instance_size = sizeof(BDRVCURLState),
979 .bdrv_parse_filename = curl_parse_filename,
980 .bdrv_file_open = curl_open,
981 .bdrv_close = curl_close,
982 .bdrv_getlength = curl_getlength,
984 .bdrv_co_preadv = curl_co_preadv,
986 .bdrv_detach_aio_context = curl_detach_aio_context,
987 .bdrv_attach_aio_context = curl_attach_aio_context,
989 .bdrv_refresh_filename = curl_refresh_filename,
990 .strong_runtime_opts = curl_strong_runtime_opts,
993 static BlockDriver bdrv_https = {
994 .format_name = "https",
995 .protocol_name = "https",
997 .instance_size = sizeof(BDRVCURLState),
998 .bdrv_parse_filename = curl_parse_filename,
999 .bdrv_file_open = curl_open,
1000 .bdrv_close = curl_close,
1001 .bdrv_getlength = curl_getlength,
1003 .bdrv_co_preadv = curl_co_preadv,
1005 .bdrv_detach_aio_context = curl_detach_aio_context,
1006 .bdrv_attach_aio_context = curl_attach_aio_context,
1008 .bdrv_refresh_filename = curl_refresh_filename,
1009 .strong_runtime_opts = curl_strong_runtime_opts,
1012 static BlockDriver bdrv_ftp = {
1013 .format_name = "ftp",
1014 .protocol_name = "ftp",
1016 .instance_size = sizeof(BDRVCURLState),
1017 .bdrv_parse_filename = curl_parse_filename,
1018 .bdrv_file_open = curl_open,
1019 .bdrv_close = curl_close,
1020 .bdrv_getlength = curl_getlength,
1022 .bdrv_co_preadv = curl_co_preadv,
1024 .bdrv_detach_aio_context = curl_detach_aio_context,
1025 .bdrv_attach_aio_context = curl_attach_aio_context,
1027 .bdrv_refresh_filename = curl_refresh_filename,
1028 .strong_runtime_opts = curl_strong_runtime_opts,
1031 static BlockDriver bdrv_ftps = {
1032 .format_name = "ftps",
1033 .protocol_name = "ftps",
1035 .instance_size = sizeof(BDRVCURLState),
1036 .bdrv_parse_filename = curl_parse_filename,
1037 .bdrv_file_open = curl_open,
1038 .bdrv_close = curl_close,
1039 .bdrv_getlength = curl_getlength,
1041 .bdrv_co_preadv = curl_co_preadv,
1043 .bdrv_detach_aio_context = curl_detach_aio_context,
1044 .bdrv_attach_aio_context = curl_attach_aio_context,
1046 .bdrv_refresh_filename = curl_refresh_filename,
1047 .strong_runtime_opts = curl_strong_runtime_opts,
1050 static void curl_block_init(void)
1052 bdrv_register(&bdrv_http);
1053 bdrv_register(&bdrv_https);
1054 bdrv_register(&bdrv_ftp);
1055 bdrv_register(&bdrv_ftps);
1058 block_init(curl_block_init);