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