Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1 | /* |
| 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 Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 24 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Richard W.M. Jones | 796a060 | 2015-07-08 14:37:48 +0100 | [diff] [blame] | 27 | #include "qemu/error-report.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 28 | #include "qemu/module.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 29 | #include "qemu/option.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 30 | #include "block/block_int.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 31 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 32 | #include "qapi/qmp/qstring.h" |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 33 | #include "crypto/secret.h" |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 34 | #include <curl/curl.h> |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 35 | #include "qemu/cutils.h" |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 36 | #include "trace.h" |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 37 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 38 | // #define DEBUG_VERBOSE |
| 39 | |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 40 | #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ |
Max Reitz | 23dce38 | 2016-11-02 18:55:37 +0100 | [diff] [blame] | 41 | CURLPROTO_FTP | CURLPROTO_FTPS) |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 42 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 43 | #define CURL_NUM_STATES 8 |
| 44 | #define CURL_NUM_ACB 8 |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 45 | #define CURL_TIMEOUT_MAX 10000 |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 46 | |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 47 | #define CURL_BLOCK_OPT_URL "url" |
| 48 | #define CURL_BLOCK_OPT_READAHEAD "readahead" |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 49 | #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 50 | #define CURL_BLOCK_OPT_TIMEOUT "timeout" |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 51 | #define CURL_BLOCK_OPT_COOKIE "cookie" |
Peter Krempa | 327c8eb | 2017-05-04 16:00:06 +0200 | [diff] [blame] | 52 | #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret" |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 53 | #define CURL_BLOCK_OPT_USERNAME "username" |
| 54 | #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret" |
| 55 | #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username" |
| 56 | #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret" |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 57 | |
Max Reitz | 712b64e | 2019-02-01 20:29:31 +0100 | [diff] [blame] | 58 | #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024) |
| 59 | #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true |
| 60 | #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5 |
| 61 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 62 | struct BDRVCURLState; |
Max Reitz | 0487861 | 2019-09-10 14:41:30 +0200 | [diff] [blame] | 63 | struct CURLState; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 64 | |
Jeff Cody | 2d25964 | 2017-11-07 17:27:22 -0500 | [diff] [blame] | 65 | static bool libcurl_initialized; |
| 66 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 67 | typedef struct CURLAIOCB { |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 68 | Coroutine *co; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 69 | QEMUIOVector *qiov; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 70 | |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 71 | uint64_t offset; |
| 72 | uint64_t bytes; |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 73 | int ret; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 74 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 75 | size_t start; |
| 76 | size_t end; |
| 77 | } CURLAIOCB; |
| 78 | |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 79 | typedef struct CURLSocket { |
| 80 | int fd; |
Max Reitz | 0487861 | 2019-09-10 14:41:30 +0200 | [diff] [blame] | 81 | struct CURLState *state; |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 82 | QLIST_ENTRY(CURLSocket) next; |
| 83 | } CURLSocket; |
| 84 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 85 | typedef struct CURLState |
| 86 | { |
| 87 | struct BDRVCURLState *s; |
| 88 | CURLAIOCB *acb[CURL_NUM_ACB]; |
| 89 | CURL *curl; |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 90 | QLIST_HEAD(, CURLSocket) sockets; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 91 | char *orig_buf; |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 92 | uint64_t buf_start; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 93 | size_t buf_off; |
| 94 | size_t buf_len; |
| 95 | char range[128]; |
| 96 | char errmsg[CURL_ERROR_SIZE]; |
| 97 | char in_use; |
| 98 | } CURLState; |
| 99 | |
| 100 | typedef struct BDRVCURLState { |
| 101 | CURLM *multi; |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 102 | QEMUTimer timer; |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 103 | uint64_t len; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 104 | CURLState states[CURL_NUM_STATES]; |
| 105 | char *url; |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 106 | size_t readahead_size; |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 107 | bool sslverify; |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 108 | uint64_t timeout; |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 109 | char *cookie; |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 110 | bool accept_range; |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 111 | AioContext *aio_context; |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 112 | QemuMutex mutex; |
Paolo Bonzini | 709f213 | 2018-02-03 10:39:35 -0500 | [diff] [blame] | 113 | CoQueue free_state_waitq; |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 114 | char *username; |
| 115 | char *password; |
| 116 | char *proxyusername; |
| 117 | char *proxypassword; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 118 | } BDRVCURLState; |
| 119 | |
| 120 | static void curl_clean_state(CURLState *s); |
| 121 | static void curl_multi_do(void *arg); |
| 122 | |
Paolo Bonzini | 34db05e | 2017-05-15 12:00:54 +0200 | [diff] [blame] | 123 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 124 | static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) |
| 125 | { |
| 126 | BDRVCURLState *s = opaque; |
| 127 | |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 128 | trace_curl_timer_cb(timeout_ms); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 129 | if (timeout_ms == -1) { |
| 130 | timer_del(&s->timer); |
| 131 | } else { |
| 132 | int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; |
| 133 | timer_mod(&s->timer, |
| 134 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); |
| 135 | } |
| 136 | return 0; |
| 137 | } |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 138 | |
Paolo Bonzini | 34db05e | 2017-05-15 12:00:54 +0200 | [diff] [blame] | 139 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 140 | static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 141 | void *userp, void *sp) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 142 | { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 143 | BDRVCURLState *s; |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 144 | CURLState *state = NULL; |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 145 | CURLSocket *socket; |
| 146 | |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 147 | curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 148 | s = state->s; |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 149 | |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 150 | QLIST_FOREACH(socket, &state->sockets, next) { |
| 151 | if (socket->fd == fd) { |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 152 | break; |
| 153 | } |
| 154 | } |
| 155 | if (!socket) { |
| 156 | socket = g_new0(CURLSocket, 1); |
| 157 | socket->fd = fd; |
Max Reitz | 0487861 | 2019-09-10 14:41:30 +0200 | [diff] [blame] | 158 | socket->state = state; |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 159 | QLIST_INSERT_HEAD(&state->sockets, socket, next); |
| 160 | } |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 161 | |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 162 | trace_curl_sock_cb(action, (int)fd); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 163 | switch (action) { |
| 164 | case CURL_POLL_IN: |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 165 | aio_set_fd_handler(s->aio_context, fd, false, |
Max Reitz | 9dbad87 | 2019-09-10 14:41:33 +0200 | [diff] [blame] | 166 | curl_multi_do, NULL, NULL, socket); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 167 | break; |
| 168 | case CURL_POLL_OUT: |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 169 | aio_set_fd_handler(s->aio_context, fd, false, |
Max Reitz | 9dbad87 | 2019-09-10 14:41:33 +0200 | [diff] [blame] | 170 | NULL, curl_multi_do, NULL, socket); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 171 | break; |
| 172 | case CURL_POLL_INOUT: |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 173 | aio_set_fd_handler(s->aio_context, fd, false, |
Max Reitz | 9dbad87 | 2019-09-10 14:41:33 +0200 | [diff] [blame] | 174 | curl_multi_do, curl_multi_do, NULL, socket); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 175 | break; |
| 176 | case CURL_POLL_REMOVE: |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 177 | aio_set_fd_handler(s->aio_context, fd, false, |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 178 | NULL, NULL, NULL, NULL); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | |
Max Reitz | 007f339 | 2019-09-10 14:41:31 +0200 | [diff] [blame] | 182 | if (action == CURL_POLL_REMOVE) { |
| 183 | QLIST_REMOVE(socket, next); |
| 184 | g_free(socket); |
| 185 | } |
| 186 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
Paolo Bonzini | 34db05e | 2017-05-15 12:00:54 +0200 | [diff] [blame] | 190 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 191 | static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 192 | { |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 193 | BDRVCURLState *s = opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 194 | size_t realsize = size * nmemb; |
David Edmondson | 7788a31 | 2020-02-24 10:13:09 +0000 | [diff] [blame] | 195 | const char *header = (char *)ptr; |
| 196 | const char *end = header + realsize; |
David Edmondson | 6903225 | 2020-02-24 10:13:10 +0000 | [diff] [blame] | 197 | const char *accept_ranges = "accept-ranges:"; |
David Edmondson | 7788a31 | 2020-02-24 10:13:09 +0000 | [diff] [blame] | 198 | const char *bytes = "bytes"; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 199 | |
David Edmondson | 7788a31 | 2020-02-24 10:13:09 +0000 | [diff] [blame] | 200 | if (realsize >= strlen(accept_ranges) |
David Edmondson | 6903225 | 2020-02-24 10:13:10 +0000 | [diff] [blame] | 201 | && g_ascii_strncasecmp(header, accept_ranges, |
| 202 | strlen(accept_ranges)) == 0) { |
David Edmondson | 7788a31 | 2020-02-24 10:13:09 +0000 | [diff] [blame] | 203 | |
| 204 | char *p = strchr(header, ':') + 1; |
| 205 | |
| 206 | /* Skip whitespace between the header name and value. */ |
| 207 | while (p < end && *p && g_ascii_isspace(*p)) { |
| 208 | p++; |
| 209 | } |
| 210 | |
| 211 | if (end - p >= strlen(bytes) |
| 212 | && strncmp(p, bytes, strlen(bytes)) == 0) { |
| 213 | |
| 214 | /* Check that there is nothing but whitespace after the value. */ |
| 215 | p += strlen(bytes); |
| 216 | while (p < end && *p && g_ascii_isspace(*p)) { |
| 217 | p++; |
| 218 | } |
| 219 | |
| 220 | if (p == end || !*p) { |
| 221 | s->accept_range = true; |
| 222 | } |
| 223 | } |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 224 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 225 | |
| 226 | return realsize; |
| 227 | } |
| 228 | |
Paolo Bonzini | 34db05e | 2017-05-15 12:00:54 +0200 | [diff] [blame] | 229 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 230 | static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
| 231 | { |
| 232 | CURLState *s = ((CURLState*)opaque); |
| 233 | size_t realsize = size * nmemb; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 234 | |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 235 | trace_curl_read_cb(realsize); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 236 | |
Max Reitz | 4e76765 | 2016-10-25 04:54:29 +0200 | [diff] [blame] | 237 | if (!s || !s->orig_buf) { |
| 238 | goto read_end; |
| 239 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 240 | |
Fam Zheng | 6d4b9e5 | 2014-03-26 13:05:40 +0100 | [diff] [blame] | 241 | if (s->buf_off >= s->buf_len) { |
| 242 | /* buffer full, read nothing */ |
Max Reitz | 4e76765 | 2016-10-25 04:54:29 +0200 | [diff] [blame] | 243 | goto read_end; |
Fam Zheng | 6d4b9e5 | 2014-03-26 13:05:40 +0100 | [diff] [blame] | 244 | } |
| 245 | realsize = MIN(realsize, s->buf_len - s->buf_off); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 246 | memcpy(s->orig_buf + s->buf_off, ptr, realsize); |
| 247 | s->buf_off += realsize; |
| 248 | |
Max Reitz | 4e76765 | 2016-10-25 04:54:29 +0200 | [diff] [blame] | 249 | read_end: |
| 250 | /* curl will error out if we do not return this value */ |
| 251 | return size * nmemb; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 254 | /* Called with s->mutex held. */ |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 255 | static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len, |
| 256 | CURLAIOCB *acb) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 257 | { |
| 258 | int i; |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 259 | uint64_t end = start + len; |
| 260 | uint64_t clamped_end = MIN(end, s->len); |
| 261 | uint64_t clamped_len = clamped_end - start; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 262 | |
| 263 | for (i=0; i<CURL_NUM_STATES; i++) { |
| 264 | CURLState *state = &s->states[i]; |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 265 | uint64_t buf_end = (state->buf_start + state->buf_off); |
| 266 | uint64_t buf_fend = (state->buf_start + state->buf_len); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 267 | |
| 268 | if (!state->orig_buf) |
| 269 | continue; |
| 270 | if (!state->buf_off) |
| 271 | continue; |
| 272 | |
| 273 | // Does the existing buffer cover our section? |
| 274 | if ((start >= state->buf_start) && |
| 275 | (start <= buf_end) && |
Max Reitz | 4e50453 | 2016-10-25 04:54:31 +0200 | [diff] [blame] | 276 | (clamped_end >= state->buf_start) && |
| 277 | (clamped_end <= buf_end)) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 278 | { |
| 279 | char *buf = state->orig_buf + (start - state->buf_start); |
| 280 | |
Max Reitz | 4e50453 | 2016-10-25 04:54:31 +0200 | [diff] [blame] | 281 | qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len); |
| 282 | if (clamped_len < len) { |
| 283 | qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len); |
| 284 | } |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 285 | acb->ret = 0; |
| 286 | return true; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Wait for unfinished chunks |
Matthew Booth | b7079df | 2014-04-29 16:03:32 +0100 | [diff] [blame] | 290 | if (state->in_use && |
| 291 | (start >= state->buf_start) && |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 292 | (start <= buf_fend) && |
Max Reitz | 4e50453 | 2016-10-25 04:54:31 +0200 | [diff] [blame] | 293 | (clamped_end >= state->buf_start) && |
| 294 | (clamped_end <= buf_fend)) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 295 | { |
| 296 | int j; |
| 297 | |
| 298 | acb->start = start - state->buf_start; |
Max Reitz | 4e50453 | 2016-10-25 04:54:31 +0200 | [diff] [blame] | 299 | acb->end = acb->start + clamped_len; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 300 | |
| 301 | for (j=0; j<CURL_NUM_ACB; j++) { |
| 302 | if (!state->acb[j]) { |
| 303 | state->acb[j] = acb; |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 304 | return true; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 310 | return false; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 313 | /* Called with s->mutex held. */ |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 314 | static void curl_multi_check_completion(BDRVCURLState *s) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 315 | { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 316 | int msgs_in_queue; |
| 317 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 318 | /* Try to find done transfers, so we can free the easy |
| 319 | * handle again. */ |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 320 | for (;;) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 321 | CURLMsg *msg; |
| 322 | msg = curl_multi_info_read(s->multi, &msgs_in_queue); |
| 323 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 324 | /* Quit when there are no more completions */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 325 | if (!msg) |
| 326 | break; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 327 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 328 | if (msg->msg == CURLMSG_DONE) { |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 329 | int i; |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 330 | CURLState *state = NULL; |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 331 | bool error = msg->data.result != CURLE_OK; |
| 332 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 333 | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, |
| 334 | (char **)&state); |
Nicholas Thomas | f785a5a | 2011-08-15 10:00:34 +0100 | [diff] [blame] | 335 | |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 336 | if (error) { |
Richard W.M. Jones | 796a060 | 2015-07-08 14:37:48 +0100 | [diff] [blame] | 337 | static int errcount = 100; |
| 338 | |
| 339 | /* Don't lose the original error message from curl, since |
| 340 | * it contains extra data. |
| 341 | */ |
| 342 | if (errcount > 0) { |
| 343 | error_report("curl: %s", state->errmsg); |
| 344 | if (--errcount == 0) { |
| 345 | error_report("curl: further errors suppressed"); |
| 346 | } |
| 347 | } |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 348 | } |
Richard W.M. Jones | 796a060 | 2015-07-08 14:37:48 +0100 | [diff] [blame] | 349 | |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 350 | for (i = 0; i < CURL_NUM_ACB; i++) { |
| 351 | CURLAIOCB *acb = state->acb[i]; |
Nicholas Thomas | f785a5a | 2011-08-15 10:00:34 +0100 | [diff] [blame] | 352 | |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 353 | if (acb == NULL) { |
| 354 | continue; |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 355 | } |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 356 | |
| 357 | if (!error) { |
| 358 | /* Assert that we have read all data */ |
| 359 | assert(state->buf_off >= acb->end); |
| 360 | |
| 361 | qemu_iovec_from_buf(acb->qiov, 0, |
| 362 | state->orig_buf + acb->start, |
| 363 | acb->end - acb->start); |
| 364 | |
| 365 | if (acb->end - acb->start < acb->bytes) { |
| 366 | size_t offset = acb->end - acb->start; |
| 367 | qemu_iovec_memset(acb->qiov, offset, 0, |
| 368 | acb->bytes - offset); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | acb->ret = error ? -EIO : 0; |
| 373 | state->acb[i] = NULL; |
| 374 | qemu_mutex_unlock(&s->mutex); |
| 375 | aio_co_wake(acb->co); |
| 376 | qemu_mutex_lock(&s->mutex); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 377 | } |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 378 | |
| 379 | curl_clean_state(state); |
| 380 | break; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 381 | } |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 382 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 385 | /* Called with s->mutex held. */ |
Max Reitz | 9abaf9f | 2019-09-10 14:41:34 +0200 | [diff] [blame] | 386 | static void curl_multi_do_locked(CURLSocket *socket) |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 387 | { |
Max Reitz | 9abaf9f | 2019-09-10 14:41:34 +0200 | [diff] [blame] | 388 | BDRVCURLState *s = socket->state->s; |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 389 | int running; |
| 390 | int r; |
| 391 | |
Max Reitz | 9abaf9f | 2019-09-10 14:41:34 +0200 | [diff] [blame] | 392 | if (!s->multi) { |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
Max Reitz | 9abaf9f | 2019-09-10 14:41:34 +0200 | [diff] [blame] | 396 | do { |
| 397 | r = curl_multi_socket_action(s->multi, socket->fd, 0, &running); |
| 398 | } while (r == CURLM_CALL_MULTI_PERFORM); |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 401 | static void curl_multi_do(void *arg) |
| 402 | { |
Max Reitz | 9dbad87 | 2019-09-10 14:41:33 +0200 | [diff] [blame] | 403 | CURLSocket *socket = arg; |
| 404 | BDRVCURLState *s = socket->state->s; |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 405 | |
Max Reitz | 9dbad87 | 2019-09-10 14:41:33 +0200 | [diff] [blame] | 406 | qemu_mutex_lock(&s->mutex); |
| 407 | curl_multi_do_locked(socket); |
| 408 | curl_multi_check_completion(s); |
| 409 | qemu_mutex_unlock(&s->mutex); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static void curl_multi_timeout_do(void *arg) |
| 413 | { |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 414 | BDRVCURLState *s = (BDRVCURLState *)arg; |
| 415 | int running; |
| 416 | |
| 417 | if (!s->multi) { |
| 418 | return; |
| 419 | } |
| 420 | |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 421 | qemu_mutex_lock(&s->mutex); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 422 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); |
| 423 | |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 424 | curl_multi_check_completion(s); |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 425 | qemu_mutex_unlock(&s->mutex); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 428 | /* Called with s->mutex held. */ |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 429 | static CURLState *curl_find_state(BDRVCURLState *s) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 430 | { |
| 431 | CURLState *state = NULL; |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 432 | int i; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 433 | |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 434 | for (i = 0; i < CURL_NUM_STATES; i++) { |
| 435 | if (!s->states[i].in_use) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 436 | state = &s->states[i]; |
| 437 | state->in_use = 1; |
| 438 | break; |
| 439 | } |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 440 | } |
| 441 | return state; |
| 442 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 443 | |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 444 | static int curl_init_state(BDRVCURLState *s, CURLState *state) |
| 445 | { |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 446 | if (!state->curl) { |
| 447 | state->curl = curl_easy_init(); |
| 448 | if (!state->curl) { |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 449 | return -EIO; |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 450 | } |
| 451 | curl_easy_setopt(state->curl, CURLOPT_URL, s->url); |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 452 | curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, |
| 453 | (long) s->sslverify); |
Richard W.M. Jones | 637fa44 | 2018-09-14 10:56:22 +0100 | [diff] [blame] | 454 | curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST, |
| 455 | s->sslverify ? 2L : 0L); |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 456 | if (s->cookie) { |
| 457 | curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie); |
| 458 | } |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 459 | curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout); |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 460 | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, |
| 461 | (void *)curl_read_cb); |
| 462 | curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); |
| 463 | curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); |
| 464 | curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); |
| 465 | curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); |
| 466 | curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); |
| 467 | curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); |
| 468 | curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 469 | |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 470 | if (s->username) { |
| 471 | curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username); |
| 472 | } |
| 473 | if (s->password) { |
| 474 | curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password); |
| 475 | } |
| 476 | if (s->proxyusername) { |
| 477 | curl_easy_setopt(state->curl, |
| 478 | CURLOPT_PROXYUSERNAME, s->proxyusername); |
| 479 | } |
| 480 | if (s->proxypassword) { |
| 481 | curl_easy_setopt(state->curl, |
| 482 | CURLOPT_PROXYPASSWORD, s->proxypassword); |
| 483 | } |
| 484 | |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 485 | /* Restrict supported protocols to avoid security issues in the more |
| 486 | * obscure protocols. For example, do not allow POP3/SMTP/IMAP see |
| 487 | * CVE-2013-0249. |
| 488 | * |
| 489 | * Restricting protocols is only supported from 7.19.4 upwards. |
| 490 | */ |
Stefan Hajnoczi | 8a8f584 | 2013-02-13 09:25:34 +0100 | [diff] [blame] | 491 | #if LIBCURL_VERSION_NUM >= 0x071304 |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 492 | curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); |
| 493 | curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); |
Stefan Hajnoczi | 8a8f584 | 2013-02-13 09:25:34 +0100 | [diff] [blame] | 494 | #endif |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 495 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 496 | #ifdef DEBUG_VERBOSE |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 497 | curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 498 | #endif |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 499 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 500 | |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 501 | QLIST_INIT(&state->sockets); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 502 | state->s = s; |
| 503 | |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 504 | return 0; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 507 | /* Called with s->mutex held. */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 508 | static void curl_clean_state(CURLState *s) |
| 509 | { |
Paolo Bonzini | 675a775 | 2017-05-15 12:00:53 +0200 | [diff] [blame] | 510 | int j; |
| 511 | for (j = 0; j < CURL_NUM_ACB; j++) { |
| 512 | assert(!s->acb[j]); |
| 513 | } |
| 514 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 515 | if (s->s->multi) |
| 516 | curl_multi_remove_handle(s->s->multi, s->curl); |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 517 | |
| 518 | while (!QLIST_EMPTY(&s->sockets)) { |
| 519 | CURLSocket *socket = QLIST_FIRST(&s->sockets); |
| 520 | |
| 521 | QLIST_REMOVE(socket, next); |
| 522 | g_free(socket); |
| 523 | } |
| 524 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 525 | s->in_use = 0; |
Paolo Bonzini | 2bb5c93 | 2017-05-15 12:00:59 +0200 | [diff] [blame] | 526 | |
Paolo Bonzini | 709f213 | 2018-02-03 10:39:35 -0500 | [diff] [blame] | 527 | qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 530 | static void curl_parse_filename(const char *filename, QDict *options, |
| 531 | Error **errp) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 532 | { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 533 | qdict_put_str(options, CURL_BLOCK_OPT_URL, filename); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 536 | static void curl_detach_aio_context(BlockDriverState *bs) |
| 537 | { |
| 538 | BDRVCURLState *s = bs->opaque; |
| 539 | int i; |
| 540 | |
Gan Qixin | f5056b7 | 2020-12-03 15:50:53 +0800 | [diff] [blame] | 541 | WITH_QEMU_LOCK_GUARD(&s->mutex) { |
| 542 | for (i = 0; i < CURL_NUM_STATES; i++) { |
| 543 | if (s->states[i].in_use) { |
| 544 | curl_clean_state(&s->states[i]); |
| 545 | } |
| 546 | if (s->states[i].curl) { |
| 547 | curl_easy_cleanup(s->states[i].curl); |
| 548 | s->states[i].curl = NULL; |
| 549 | } |
| 550 | g_free(s->states[i].orig_buf); |
| 551 | s->states[i].orig_buf = NULL; |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 552 | } |
Gan Qixin | f5056b7 | 2020-12-03 15:50:53 +0800 | [diff] [blame] | 553 | if (s->multi) { |
| 554 | curl_multi_cleanup(s->multi); |
| 555 | s->multi = NULL; |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 556 | } |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 557 | } |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 558 | |
| 559 | timer_del(&s->timer); |
| 560 | } |
| 561 | |
| 562 | static void curl_attach_aio_context(BlockDriverState *bs, |
| 563 | AioContext *new_context) |
| 564 | { |
| 565 | BDRVCURLState *s = bs->opaque; |
| 566 | |
| 567 | aio_timer_init(new_context, &s->timer, |
| 568 | QEMU_CLOCK_REALTIME, SCALE_NS, |
| 569 | curl_multi_timeout_do, s); |
| 570 | |
| 571 | assert(!s->multi); |
| 572 | s->multi = curl_multi_init(); |
| 573 | s->aio_context = new_context; |
| 574 | curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 575 | curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); |
| 576 | curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 577 | } |
| 578 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 579 | static QemuOptsList runtime_opts = { |
| 580 | .name = "curl", |
| 581 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
| 582 | .desc = { |
| 583 | { |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 584 | .name = CURL_BLOCK_OPT_URL, |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 585 | .type = QEMU_OPT_STRING, |
| 586 | .help = "URL to open", |
| 587 | }, |
| 588 | { |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 589 | .name = CURL_BLOCK_OPT_READAHEAD, |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 590 | .type = QEMU_OPT_SIZE, |
| 591 | .help = "Readahead size", |
| 592 | }, |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 593 | { |
| 594 | .name = CURL_BLOCK_OPT_SSLVERIFY, |
| 595 | .type = QEMU_OPT_BOOL, |
| 596 | .help = "Verify SSL certificate" |
| 597 | }, |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 598 | { |
| 599 | .name = CURL_BLOCK_OPT_TIMEOUT, |
| 600 | .type = QEMU_OPT_NUMBER, |
| 601 | .help = "Curl timeout" |
| 602 | }, |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 603 | { |
| 604 | .name = CURL_BLOCK_OPT_COOKIE, |
| 605 | .type = QEMU_OPT_STRING, |
| 606 | .help = "Pass the cookie or list of cookies with each request" |
| 607 | }, |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 608 | { |
Peter Krempa | 327c8eb | 2017-05-04 16:00:06 +0200 | [diff] [blame] | 609 | .name = CURL_BLOCK_OPT_COOKIE_SECRET, |
| 610 | .type = QEMU_OPT_STRING, |
| 611 | .help = "ID of secret used as cookie passed with each request" |
| 612 | }, |
| 613 | { |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 614 | .name = CURL_BLOCK_OPT_USERNAME, |
| 615 | .type = QEMU_OPT_STRING, |
| 616 | .help = "Username for HTTP auth" |
| 617 | }, |
| 618 | { |
| 619 | .name = CURL_BLOCK_OPT_PASSWORD_SECRET, |
| 620 | .type = QEMU_OPT_STRING, |
| 621 | .help = "ID of secret used as password for HTTP auth", |
| 622 | }, |
| 623 | { |
| 624 | .name = CURL_BLOCK_OPT_PROXY_USERNAME, |
| 625 | .type = QEMU_OPT_STRING, |
| 626 | .help = "Username for HTTP proxy auth" |
| 627 | }, |
| 628 | { |
| 629 | .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, |
| 630 | .type = QEMU_OPT_STRING, |
| 631 | .help = "ID of secret used as password for HTTP proxy auth", |
| 632 | }, |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 633 | { /* end of list */ } |
| 634 | }, |
| 635 | }; |
| 636 | |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 637 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 638 | static int curl_open(BlockDriverState *bs, QDict *options, int flags, |
| 639 | Error **errp) |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 640 | { |
| 641 | BDRVCURLState *s = bs->opaque; |
| 642 | CURLState *state = NULL; |
| 643 | QemuOpts *opts; |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 644 | const char *file; |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 645 | const char *cookie; |
Peter Krempa | 327c8eb | 2017-05-04 16:00:06 +0200 | [diff] [blame] | 646 | const char *cookie_secret; |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 647 | double d; |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 648 | const char *secretid; |
Max Reitz | 34634ca | 2017-03-31 14:04:31 +0200 | [diff] [blame] | 649 | const char *protocol_delimiter; |
Jeff Cody | 2d25964 | 2017-11-07 17:27:22 -0500 | [diff] [blame] | 650 | int ret; |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 651 | |
Kevin Wolf | 6ceef36 | 2018-10-08 17:27:18 +0200 | [diff] [blame] | 652 | ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes", |
| 653 | errp); |
| 654 | if (ret < 0) { |
| 655 | return ret; |
Richard W.M. Jones | a7cea2b | 2013-06-10 12:38:43 +0100 | [diff] [blame] | 656 | } |
| 657 | |
Jeff Cody | 2d25964 | 2017-11-07 17:27:22 -0500 | [diff] [blame] | 658 | if (!libcurl_initialized) { |
| 659 | ret = curl_global_init(CURL_GLOBAL_ALL); |
| 660 | if (ret) { |
| 661 | error_setg(errp, "libcurl initialization failed with %d", ret); |
| 662 | return -EIO; |
| 663 | } |
| 664 | libcurl_initialized = true; |
| 665 | } |
| 666 | |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 667 | qemu_mutex_init(&s->mutex); |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 668 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
Markus Armbruster | 668f62e | 2020-07-07 18:06:02 +0200 | [diff] [blame] | 669 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 670 | goto out_noclean; |
| 671 | } |
| 672 | |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 673 | s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, |
Max Reitz | 712b64e | 2019-02-01 20:29:31 +0100 | [diff] [blame] | 674 | CURL_BLOCK_OPT_READAHEAD_DEFAULT); |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 675 | if ((s->readahead_size & 0x1ff) != 0) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 676 | error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", |
| 677 | s->readahead_size); |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 678 | goto out_noclean; |
| 679 | } |
| 680 | |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 681 | s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, |
Max Reitz | 712b64e | 2019-02-01 20:29:31 +0100 | [diff] [blame] | 682 | CURL_BLOCK_OPT_TIMEOUT_DEFAULT); |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 683 | if (s->timeout > CURL_TIMEOUT_MAX) { |
| 684 | error_setg(errp, "timeout parameter is too large or negative"); |
| 685 | goto out_noclean; |
| 686 | } |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 687 | |
Max Reitz | 712b64e | 2019-02-01 20:29:31 +0100 | [diff] [blame] | 688 | s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, |
| 689 | CURL_BLOCK_OPT_SSLVERIFY_DEFAULT); |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 690 | |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 691 | cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); |
Peter Krempa | 327c8eb | 2017-05-04 16:00:06 +0200 | [diff] [blame] | 692 | cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET); |
| 693 | |
| 694 | if (cookie && cookie_secret) { |
| 695 | error_setg(errp, |
| 696 | "curl driver cannot handle both cookie and cookie secret"); |
| 697 | goto out_noclean; |
| 698 | } |
| 699 | |
| 700 | if (cookie_secret) { |
| 701 | s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp); |
| 702 | if (!s->cookie) { |
| 703 | goto out_noclean; |
| 704 | } |
| 705 | } else { |
| 706 | s->cookie = g_strdup(cookie); |
| 707 | } |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 708 | |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 709 | file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 710 | if (file == NULL) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 711 | error_setg(errp, "curl block driver requires an 'url' option"); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 712 | goto out_noclean; |
| 713 | } |
| 714 | |
Max Reitz | 34634ca | 2017-03-31 14:04:31 +0200 | [diff] [blame] | 715 | if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) || |
| 716 | !strstart(protocol_delimiter, "://", NULL)) |
| 717 | { |
| 718 | error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not " |
| 719 | "start with '%s://')", bs->drv->protocol_name, file, |
| 720 | bs->drv->protocol_name); |
| 721 | goto out_noclean; |
| 722 | } |
| 723 | |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 724 | s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME)); |
| 725 | secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET); |
| 726 | |
| 727 | if (secretid) { |
| 728 | s->password = qcrypto_secret_lookup_as_utf8(secretid, errp); |
| 729 | if (!s->password) { |
| 730 | goto out_noclean; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | s->proxyusername = g_strdup( |
| 735 | qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME)); |
| 736 | secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET); |
| 737 | if (secretid) { |
| 738 | s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp); |
| 739 | if (!s->proxypassword) { |
| 740 | goto out_noclean; |
| 741 | } |
| 742 | } |
| 743 | |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 744 | trace_curl_open(file); |
Paolo Bonzini | 709f213 | 2018-02-03 10:39:35 -0500 | [diff] [blame] | 745 | qemu_co_queue_init(&s->free_state_waitq); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 746 | s->aio_context = bdrv_get_aio_context(bs); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 747 | s->url = g_strdup(file); |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 748 | qemu_mutex_lock(&s->mutex); |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 749 | state = curl_find_state(s); |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 750 | qemu_mutex_unlock(&s->mutex); |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 751 | if (!state) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 752 | goto out_noclean; |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 753 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 754 | |
| 755 | // Get file size |
| 756 | |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 757 | if (curl_init_state(s, state) < 0) { |
| 758 | goto out; |
| 759 | } |
| 760 | |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 761 | s->accept_range = false; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 762 | curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 763 | curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, |
| 764 | curl_header_cb); |
| 765 | curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 766 | if (curl_easy_perform(state->curl)) |
| 767 | goto out; |
Tomáš Golembiovský | a41c457 | 2016-07-08 13:18:09 +0200 | [diff] [blame] | 768 | if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 769 | goto out; |
Tomáš Golembiovský | a41c457 | 2016-07-08 13:18:09 +0200 | [diff] [blame] | 770 | } |
| 771 | /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not |
| 772 | * know or the size is zero. From 7.19.4 CURL returns -1 if size is not |
Stefan Weil | 50d6a8a | 2018-07-12 21:51:20 +0200 | [diff] [blame] | 773 | * known and zero if it is really zero-length file. */ |
Tomáš Golembiovský | a41c457 | 2016-07-08 13:18:09 +0200 | [diff] [blame] | 774 | #if LIBCURL_VERSION_NUM >= 0x071304 |
| 775 | if (d < 0) { |
| 776 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
| 777 | "Server didn't report file size."); |
| 778 | goto out; |
| 779 | } |
| 780 | #else |
| 781 | if (d <= 0) { |
| 782 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
| 783 | "Unknown file size or zero-length file."); |
| 784 | goto out; |
| 785 | } |
| 786 | #endif |
| 787 | |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 788 | s->len = d; |
Tomáš Golembiovský | a41c457 | 2016-07-08 13:18:09 +0200 | [diff] [blame] | 789 | |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 790 | if ((!strncasecmp(s->url, "http://", strlen("http://")) |
| 791 | || !strncasecmp(s->url, "https://", strlen("https://"))) |
| 792 | && !s->accept_range) { |
| 793 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
| 794 | "Server does not support 'range' (byte ranges)."); |
| 795 | goto out; |
| 796 | } |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 797 | trace_curl_open_size(s->len); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 798 | |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 799 | qemu_mutex_lock(&s->mutex); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 800 | curl_clean_state(state); |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 801 | qemu_mutex_unlock(&s->mutex); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 802 | curl_easy_cleanup(state->curl); |
| 803 | state->curl = NULL; |
| 804 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 805 | curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 806 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 807 | qemu_opts_del(opts); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 808 | return 0; |
| 809 | |
| 810 | out: |
Maria Kustova | acd7fdc | 2014-03-18 09:59:18 +0400 | [diff] [blame] | 811 | error_setg(errp, "CURL: Error opening file: %s", state->errmsg); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 812 | curl_easy_cleanup(state->curl); |
| 813 | state->curl = NULL; |
| 814 | out_noclean: |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 815 | qemu_mutex_destroy(&s->mutex); |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 816 | g_free(s->cookie); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 817 | g_free(s->url); |
Jeff Cody | 996922d | 2017-11-07 17:27:23 -0500 | [diff] [blame] | 818 | g_free(s->username); |
| 819 | g_free(s->proxyusername); |
| 820 | g_free(s->proxypassword); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 821 | qemu_opts_del(opts); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 822 | return -EINVAL; |
| 823 | } |
| 824 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 825 | static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 826 | { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 827 | CURLState *state; |
Matthew Booth | b69cdef | 2014-04-29 16:03:29 +0100 | [diff] [blame] | 828 | int running; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 829 | |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 830 | BDRVCURLState *s = bs->opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 831 | |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 832 | uint64_t start = acb->offset; |
| 833 | uint64_t end; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 834 | |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 835 | qemu_mutex_lock(&s->mutex); |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 836 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 837 | // In case we have the requested data already (e.g. read-ahead), |
| 838 | // we can just call the callback and be done. |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 839 | if (curl_find_buf(s, start, acb->bytes, acb)) { |
| 840 | goto out; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | // No cache found, so let's start a new request |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 844 | for (;;) { |
| 845 | state = curl_find_state(s); |
| 846 | if (state) { |
| 847 | break; |
| 848 | } |
Paolo Bonzini | 709f213 | 2018-02-03 10:39:35 -0500 | [diff] [blame] | 849 | qemu_co_queue_wait(&s->free_state_waitq, &s->mutex); |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | if (curl_init_state(s, state) < 0) { |
| 853 | curl_clean_state(state); |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 854 | acb->ret = -EIO; |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 855 | goto out; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 856 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 857 | |
| 858 | acb->start = 0; |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 859 | acb->end = MIN(acb->bytes, s->len - start); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 860 | |
| 861 | state->buf_off = 0; |
Markus Armbruster | f7047c2 | 2014-06-06 18:25:12 +0200 | [diff] [blame] | 862 | g_free(state->orig_buf); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 863 | state->buf_start = start; |
Max Reitz | 4e50453 | 2016-10-25 04:54:31 +0200 | [diff] [blame] | 864 | state->buf_len = MIN(acb->end + s->readahead_size, s->len - start); |
| 865 | end = start + state->buf_len - 1; |
Kevin Wolf | 8dc7a77 | 2014-05-20 13:26:40 +0200 | [diff] [blame] | 866 | state->orig_buf = g_try_malloc(state->buf_len); |
| 867 | if (state->buf_len && state->orig_buf == NULL) { |
| 868 | curl_clean_state(state); |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 869 | acb->ret = -ENOMEM; |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 870 | goto out; |
Kevin Wolf | 8dc7a77 | 2014-05-20 13:26:40 +0200 | [diff] [blame] | 871 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 872 | state->acb[0] = acb; |
| 873 | |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 874 | snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end); |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 875 | trace_curl_setup_preadv(acb->bytes, start, state->range); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 876 | curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); |
| 877 | |
Max Reitz | c34dc07 | 2019-09-10 14:41:36 +0200 | [diff] [blame] | 878 | if (curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) { |
| 879 | state->acb[0] = NULL; |
| 880 | acb->ret = -EIO; |
| 881 | |
| 882 | curl_clean_state(state); |
| 883 | goto out; |
| 884 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 885 | |
Matthew Booth | b69cdef | 2014-04-29 16:03:29 +0100 | [diff] [blame] | 886 | /* Tell curl it needs to kick things off */ |
| 887 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 888 | |
| 889 | out: |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 890 | qemu_mutex_unlock(&s->mutex); |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 893 | static int coroutine_fn curl_co_preadv(BlockDriverState *bs, |
| 894 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 895 | { |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 896 | CURLAIOCB acb = { |
| 897 | .co = qemu_coroutine_self(), |
| 898 | .ret = -EINPROGRESS, |
| 899 | .qiov = qiov, |
| 900 | .offset = offset, |
| 901 | .bytes = bytes |
| 902 | }; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 903 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 904 | curl_setup_preadv(bs, &acb); |
| 905 | while (acb.ret == -EINPROGRESS) { |
| 906 | qemu_coroutine_yield(); |
| 907 | } |
| 908 | return acb.ret; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 909 | } |
| 910 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 911 | static void curl_close(BlockDriverState *bs) |
| 912 | { |
| 913 | BDRVCURLState *s = bs->opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 914 | |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 915 | trace_curl_close(); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 916 | curl_detach_aio_context(bs); |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 917 | qemu_mutex_destroy(&s->mutex); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 918 | |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 919 | g_free(s->cookie); |
Stefan Weil | 45724d6 | 2012-09-01 11:06:45 +0200 | [diff] [blame] | 920 | g_free(s->url); |
Jeff Cody | 996922d | 2017-11-07 17:27:23 -0500 | [diff] [blame] | 921 | g_free(s->username); |
| 922 | g_free(s->proxyusername); |
| 923 | g_free(s->proxypassword); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static int64_t curl_getlength(BlockDriverState *bs) |
| 927 | { |
| 928 | BDRVCURLState *s = bs->opaque; |
| 929 | return s->len; |
| 930 | } |
| 931 | |
Max Reitz | 937c007 | 2019-02-01 20:29:32 +0100 | [diff] [blame] | 932 | static void curl_refresh_filename(BlockDriverState *bs) |
| 933 | { |
| 934 | BDRVCURLState *s = bs->opaque; |
| 935 | |
| 936 | /* "readahead" and "timeout" do not change the guest-visible data, |
| 937 | * so ignore them */ |
| 938 | if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT || |
| 939 | s->cookie || s->username || s->password || s->proxyusername || |
| 940 | s->proxypassword) |
| 941 | { |
| 942 | return; |
| 943 | } |
| 944 | |
| 945 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url); |
| 946 | } |
| 947 | |
| 948 | |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 949 | static const char *const curl_strong_runtime_opts[] = { |
| 950 | CURL_BLOCK_OPT_URL, |
| 951 | CURL_BLOCK_OPT_SSLVERIFY, |
| 952 | CURL_BLOCK_OPT_COOKIE, |
| 953 | CURL_BLOCK_OPT_COOKIE_SECRET, |
| 954 | CURL_BLOCK_OPT_USERNAME, |
| 955 | CURL_BLOCK_OPT_PASSWORD_SECRET, |
| 956 | CURL_BLOCK_OPT_PROXY_USERNAME, |
| 957 | CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, |
| 958 | |
| 959 | NULL |
| 960 | }; |
| 961 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 962 | static BlockDriver bdrv_http = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 963 | .format_name = "http", |
| 964 | .protocol_name = "http", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 965 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 966 | .instance_size = sizeof(BDRVCURLState), |
| 967 | .bdrv_parse_filename = curl_parse_filename, |
| 968 | .bdrv_file_open = curl_open, |
| 969 | .bdrv_close = curl_close, |
| 970 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 971 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 972 | .bdrv_co_preadv = curl_co_preadv, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 973 | |
| 974 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 975 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 976 | |
Max Reitz | 937c007 | 2019-02-01 20:29:32 +0100 | [diff] [blame] | 977 | .bdrv_refresh_filename = curl_refresh_filename, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 978 | .strong_runtime_opts = curl_strong_runtime_opts, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 979 | }; |
| 980 | |
| 981 | static BlockDriver bdrv_https = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 982 | .format_name = "https", |
| 983 | .protocol_name = "https", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 984 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 985 | .instance_size = sizeof(BDRVCURLState), |
| 986 | .bdrv_parse_filename = curl_parse_filename, |
| 987 | .bdrv_file_open = curl_open, |
| 988 | .bdrv_close = curl_close, |
| 989 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 990 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 991 | .bdrv_co_preadv = curl_co_preadv, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 992 | |
| 993 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 994 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 995 | |
Max Reitz | 937c007 | 2019-02-01 20:29:32 +0100 | [diff] [blame] | 996 | .bdrv_refresh_filename = curl_refresh_filename, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 997 | .strong_runtime_opts = curl_strong_runtime_opts, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 998 | }; |
| 999 | |
| 1000 | static BlockDriver bdrv_ftp = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1001 | .format_name = "ftp", |
| 1002 | .protocol_name = "ftp", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1003 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1004 | .instance_size = sizeof(BDRVCURLState), |
| 1005 | .bdrv_parse_filename = curl_parse_filename, |
| 1006 | .bdrv_file_open = curl_open, |
| 1007 | .bdrv_close = curl_close, |
| 1008 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1009 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 1010 | .bdrv_co_preadv = curl_co_preadv, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1011 | |
| 1012 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 1013 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1014 | |
Max Reitz | 937c007 | 2019-02-01 20:29:32 +0100 | [diff] [blame] | 1015 | .bdrv_refresh_filename = curl_refresh_filename, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1016 | .strong_runtime_opts = curl_strong_runtime_opts, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1017 | }; |
| 1018 | |
| 1019 | static BlockDriver bdrv_ftps = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1020 | .format_name = "ftps", |
| 1021 | .protocol_name = "ftps", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1022 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1023 | .instance_size = sizeof(BDRVCURLState), |
| 1024 | .bdrv_parse_filename = curl_parse_filename, |
| 1025 | .bdrv_file_open = curl_open, |
| 1026 | .bdrv_close = curl_close, |
| 1027 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1028 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 1029 | .bdrv_co_preadv = curl_co_preadv, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1030 | |
| 1031 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 1032 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1033 | |
Max Reitz | 937c007 | 2019-02-01 20:29:32 +0100 | [diff] [blame] | 1034 | .bdrv_refresh_filename = curl_refresh_filename, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1035 | .strong_runtime_opts = curl_strong_runtime_opts, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1036 | }; |
| 1037 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1038 | static void curl_block_init(void) |
| 1039 | { |
| 1040 | bdrv_register(&bdrv_http); |
| 1041 | bdrv_register(&bdrv_https); |
| 1042 | bdrv_register(&bdrv_ftp); |
| 1043 | bdrv_register(&bdrv_ftps); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | block_init(curl_block_init); |