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 | */ |
| 24 | #include "qemu-common.h" |
Richard W.M. Jones | 796a060 | 2015-07-08 14:37:48 +0100 | [diff] [blame] | 25 | #include "qemu/error-report.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 26 | #include "block/block_int.h" |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 27 | #include "qapi/qmp/qbool.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 28 | #include "qapi/qmp/qstring.h" |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 29 | #include <curl/curl.h> |
| 30 | |
Richard W.M. Jones | 41c2346 | 2014-08-26 21:31:08 +0100 | [diff] [blame] | 31 | // #define DEBUG_CURL |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 32 | // #define DEBUG_VERBOSE |
| 33 | |
| 34 | #ifdef DEBUG_CURL |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 35 | #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 36 | #else |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 37 | #define DPRINTF(fmt, ...) do { } while (0) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 38 | #endif |
| 39 | |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 40 | #if LIBCURL_VERSION_NUM >= 0x071000 |
| 41 | /* The multi interface timer callback was introduced in 7.16.0 */ |
| 42 | #define NEED_CURL_TIMER_CALLBACK |
Matthew Booth | 9aedd5a | 2014-05-14 19:28:40 -0400 | [diff] [blame] | 43 | #define HAVE_SOCKET_ACTION |
| 44 | #endif |
| 45 | |
| 46 | #ifndef HAVE_SOCKET_ACTION |
| 47 | /* If curl_multi_socket_action isn't available, define it statically here in |
| 48 | * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is |
| 49 | * less efficient but still safe. */ |
| 50 | static CURLMcode __curl_multi_socket_action(CURLM *multi_handle, |
| 51 | curl_socket_t sockfd, |
| 52 | int ev_bitmask, |
| 53 | int *running_handles) |
| 54 | { |
| 55 | return curl_multi_socket(multi_handle, sockfd, running_handles); |
| 56 | } |
| 57 | #define curl_multi_socket_action __curl_multi_socket_action |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 58 | #endif |
| 59 | |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 60 | #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ |
| 61 | CURLPROTO_FTP | CURLPROTO_FTPS | \ |
| 62 | CURLPROTO_TFTP) |
| 63 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 64 | #define CURL_NUM_STATES 8 |
| 65 | #define CURL_NUM_ACB 8 |
| 66 | #define SECTOR_SIZE 512 |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 67 | #define READ_AHEAD_DEFAULT (256 * 1024) |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 68 | #define CURL_TIMEOUT_DEFAULT 5 |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 69 | #define CURL_TIMEOUT_MAX 10000 |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 70 | |
| 71 | #define FIND_RET_NONE 0 |
| 72 | #define FIND_RET_OK 1 |
| 73 | #define FIND_RET_WAIT 2 |
| 74 | |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 75 | #define CURL_BLOCK_OPT_URL "url" |
| 76 | #define CURL_BLOCK_OPT_READAHEAD "readahead" |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 77 | #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 78 | #define CURL_BLOCK_OPT_TIMEOUT "timeout" |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 79 | #define CURL_BLOCK_OPT_COOKIE "cookie" |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 80 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 81 | struct BDRVCURLState; |
| 82 | |
| 83 | typedef struct CURLAIOCB { |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 84 | BlockAIOCB common; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 85 | QEMUBH *bh; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 86 | QEMUIOVector *qiov; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 87 | |
| 88 | int64_t sector_num; |
| 89 | int nb_sectors; |
| 90 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 91 | size_t start; |
| 92 | size_t end; |
| 93 | } CURLAIOCB; |
| 94 | |
| 95 | typedef struct CURLState |
| 96 | { |
| 97 | struct BDRVCURLState *s; |
| 98 | CURLAIOCB *acb[CURL_NUM_ACB]; |
| 99 | CURL *curl; |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 100 | curl_socket_t sock_fd; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 101 | char *orig_buf; |
| 102 | size_t buf_start; |
| 103 | size_t buf_off; |
| 104 | size_t buf_len; |
| 105 | char range[128]; |
| 106 | char errmsg[CURL_ERROR_SIZE]; |
| 107 | char in_use; |
| 108 | } CURLState; |
| 109 | |
| 110 | typedef struct BDRVCURLState { |
| 111 | CURLM *multi; |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 112 | QEMUTimer timer; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 113 | size_t len; |
| 114 | CURLState states[CURL_NUM_STATES]; |
| 115 | char *url; |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 116 | size_t readahead_size; |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 117 | bool sslverify; |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 118 | uint64_t timeout; |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 119 | char *cookie; |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 120 | bool accept_range; |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 121 | AioContext *aio_context; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 122 | } BDRVCURLState; |
| 123 | |
| 124 | static void curl_clean_state(CURLState *s); |
| 125 | static void curl_multi_do(void *arg); |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 126 | static void curl_multi_read(void *arg); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 127 | |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 128 | #ifdef NEED_CURL_TIMER_CALLBACK |
| 129 | static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) |
| 130 | { |
| 131 | BDRVCURLState *s = opaque; |
| 132 | |
| 133 | DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms); |
| 134 | if (timeout_ms == -1) { |
| 135 | timer_del(&s->timer); |
| 136 | } else { |
| 137 | int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; |
| 138 | timer_mod(&s->timer, |
| 139 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | #endif |
| 144 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 145 | 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] | 146 | void *userp, void *sp) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 147 | { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 148 | BDRVCURLState *s; |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 149 | CURLState *state = NULL; |
| 150 | curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); |
| 151 | state->sock_fd = fd; |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 152 | s = state->s; |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 153 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 154 | DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 155 | switch (action) { |
| 156 | case CURL_POLL_IN: |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 157 | aio_set_fd_handler(s->aio_context, fd, false, |
| 158 | curl_multi_read, NULL, state); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 159 | break; |
| 160 | case CURL_POLL_OUT: |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 161 | aio_set_fd_handler(s->aio_context, fd, false, |
| 162 | NULL, curl_multi_do, state); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 163 | break; |
| 164 | case CURL_POLL_INOUT: |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 165 | aio_set_fd_handler(s->aio_context, fd, false, |
| 166 | curl_multi_read, curl_multi_do, state); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 167 | break; |
| 168 | case CURL_POLL_REMOVE: |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 169 | aio_set_fd_handler(s->aio_context, fd, false, |
| 170 | NULL, NULL, NULL); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 171 | break; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 177 | 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] | 178 | { |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 179 | BDRVCURLState *s = opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 180 | size_t realsize = size * nmemb; |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 181 | const char *accept_line = "Accept-Ranges: bytes"; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 182 | |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 183 | if (realsize >= strlen(accept_line) |
| 184 | && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) { |
| 185 | s->accept_range = true; |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 186 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 187 | |
| 188 | return realsize; |
| 189 | } |
| 190 | |
| 191 | static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
| 192 | { |
| 193 | CURLState *s = ((CURLState*)opaque); |
| 194 | size_t realsize = size * nmemb; |
| 195 | int i; |
| 196 | |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 197 | DPRINTF("CURL: Just reading %zd bytes\n", realsize); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 198 | |
| 199 | if (!s || !s->orig_buf) |
Matthew Booth | 38bbc0a | 2014-04-29 16:03:27 +0100 | [diff] [blame] | 200 | return 0; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 201 | |
Fam Zheng | 6d4b9e5 | 2014-03-26 13:05:40 +0100 | [diff] [blame] | 202 | if (s->buf_off >= s->buf_len) { |
| 203 | /* buffer full, read nothing */ |
| 204 | return 0; |
| 205 | } |
| 206 | realsize = MIN(realsize, s->buf_len - s->buf_off); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 207 | memcpy(s->orig_buf + s->buf_off, ptr, realsize); |
| 208 | s->buf_off += realsize; |
| 209 | |
| 210 | for(i=0; i<CURL_NUM_ACB; i++) { |
| 211 | CURLAIOCB *acb = s->acb[i]; |
| 212 | |
| 213 | if (!acb) |
| 214 | continue; |
| 215 | |
| 216 | if ((s->buf_off >= acb->end)) { |
Michael Tokarev | 0339614 | 2012-06-07 20:17:55 +0400 | [diff] [blame] | 217 | qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start, |
| 218 | acb->end - acb->start); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 219 | acb->common.cb(acb->common.opaque, 0); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 220 | qemu_aio_unref(acb); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 221 | s->acb[i] = NULL; |
| 222 | } |
| 223 | } |
| 224 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 225 | return realsize; |
| 226 | } |
| 227 | |
| 228 | static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len, |
| 229 | CURLAIOCB *acb) |
| 230 | { |
| 231 | int i; |
| 232 | size_t end = start + len; |
| 233 | |
| 234 | for (i=0; i<CURL_NUM_STATES; i++) { |
| 235 | CURLState *state = &s->states[i]; |
| 236 | size_t buf_end = (state->buf_start + state->buf_off); |
| 237 | size_t buf_fend = (state->buf_start + state->buf_len); |
| 238 | |
| 239 | if (!state->orig_buf) |
| 240 | continue; |
| 241 | if (!state->buf_off) |
| 242 | continue; |
| 243 | |
| 244 | // Does the existing buffer cover our section? |
| 245 | if ((start >= state->buf_start) && |
| 246 | (start <= buf_end) && |
| 247 | (end >= state->buf_start) && |
| 248 | (end <= buf_end)) |
| 249 | { |
| 250 | char *buf = state->orig_buf + (start - state->buf_start); |
| 251 | |
Michael Tokarev | 0339614 | 2012-06-07 20:17:55 +0400 | [diff] [blame] | 252 | qemu_iovec_from_buf(acb->qiov, 0, buf, len); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 253 | acb->common.cb(acb->common.opaque, 0); |
| 254 | |
| 255 | return FIND_RET_OK; |
| 256 | } |
| 257 | |
| 258 | // Wait for unfinished chunks |
Matthew Booth | b7079df | 2014-04-29 16:03:32 +0100 | [diff] [blame] | 259 | if (state->in_use && |
| 260 | (start >= state->buf_start) && |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 261 | (start <= buf_fend) && |
| 262 | (end >= state->buf_start) && |
| 263 | (end <= buf_fend)) |
| 264 | { |
| 265 | int j; |
| 266 | |
| 267 | acb->start = start - state->buf_start; |
| 268 | acb->end = acb->start + len; |
| 269 | |
| 270 | for (j=0; j<CURL_NUM_ACB; j++) { |
| 271 | if (!state->acb[j]) { |
| 272 | state->acb[j] = acb; |
| 273 | return FIND_RET_WAIT; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return FIND_RET_NONE; |
| 280 | } |
| 281 | |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 282 | static void curl_multi_check_completion(BDRVCURLState *s) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 283 | { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 284 | int msgs_in_queue; |
| 285 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 286 | /* Try to find done transfers, so we can free the easy |
| 287 | * handle again. */ |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 288 | for (;;) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 289 | CURLMsg *msg; |
| 290 | msg = curl_multi_info_read(s->multi, &msgs_in_queue); |
| 291 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 292 | /* Quit when there are no more completions */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 293 | if (!msg) |
| 294 | break; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 295 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 296 | if (msg->msg == CURLMSG_DONE) { |
| 297 | CURLState *state = NULL; |
| 298 | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, |
| 299 | (char **)&state); |
Nicholas Thomas | f785a5a | 2011-08-15 10:00:34 +0100 | [diff] [blame] | 300 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 301 | /* ACBs for successful messages get completed in curl_read_cb */ |
| 302 | if (msg->data.result != CURLE_OK) { |
| 303 | int i; |
Richard W.M. Jones | 796a060 | 2015-07-08 14:37:48 +0100 | [diff] [blame] | 304 | static int errcount = 100; |
| 305 | |
| 306 | /* Don't lose the original error message from curl, since |
| 307 | * it contains extra data. |
| 308 | */ |
| 309 | if (errcount > 0) { |
| 310 | error_report("curl: %s", state->errmsg); |
| 311 | if (--errcount == 0) { |
| 312 | error_report("curl: further errors suppressed"); |
| 313 | } |
| 314 | } |
| 315 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 316 | for (i = 0; i < CURL_NUM_ACB; i++) { |
| 317 | CURLAIOCB *acb = state->acb[i]; |
Nicholas Thomas | f785a5a | 2011-08-15 10:00:34 +0100 | [diff] [blame] | 318 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 319 | if (acb == NULL) { |
| 320 | continue; |
Nicholas Thomas | f785a5a | 2011-08-15 10:00:34 +0100 | [diff] [blame] | 321 | } |
Nicholas Thomas | f785a5a | 2011-08-15 10:00:34 +0100 | [diff] [blame] | 322 | |
Richard W.M. Jones | 796a060 | 2015-07-08 14:37:48 +0100 | [diff] [blame] | 323 | acb->common.cb(acb->common.opaque, -EPROTO); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 324 | qemu_aio_unref(acb); |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 325 | state->acb[i] = NULL; |
| 326 | } |
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 | |
| 329 | curl_clean_state(state); |
| 330 | break; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 331 | } |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 332 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 335 | static void curl_multi_do(void *arg) |
| 336 | { |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 337 | CURLState *s = (CURLState *)arg; |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 338 | int running; |
| 339 | int r; |
| 340 | |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 341 | if (!s->s->multi) { |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
| 345 | do { |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 346 | r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 347 | } while(r == CURLM_CALL_MULTI_PERFORM); |
| 348 | |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static void curl_multi_read(void *arg) |
| 352 | { |
| 353 | CURLState *s = (CURLState *)arg; |
| 354 | |
| 355 | curl_multi_do(arg); |
| 356 | curl_multi_check_completion(s->s); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static void curl_multi_timeout_do(void *arg) |
| 360 | { |
| 361 | #ifdef NEED_CURL_TIMER_CALLBACK |
| 362 | BDRVCURLState *s = (BDRVCURLState *)arg; |
| 363 | int running; |
| 364 | |
| 365 | if (!s->multi) { |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); |
| 370 | |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 371 | curl_multi_check_completion(s); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 372 | #else |
| 373 | abort(); |
| 374 | #endif |
| 375 | } |
| 376 | |
Richard W.M. Jones | a2f468e | 2014-08-28 09:04:21 +0100 | [diff] [blame] | 377 | static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 378 | { |
| 379 | CURLState *state = NULL; |
| 380 | int i, j; |
| 381 | |
| 382 | do { |
| 383 | for (i=0; i<CURL_NUM_STATES; i++) { |
| 384 | for (j=0; j<CURL_NUM_ACB; j++) |
| 385 | if (s->states[i].acb[j]) |
| 386 | continue; |
| 387 | if (s->states[i].in_use) |
| 388 | continue; |
| 389 | |
| 390 | state = &s->states[i]; |
| 391 | state->in_use = 1; |
| 392 | break; |
| 393 | } |
| 394 | if (!state) { |
Richard W.M. Jones | a2f468e | 2014-08-28 09:04:21 +0100 | [diff] [blame] | 395 | aio_poll(bdrv_get_aio_context(bs), true); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 396 | } |
| 397 | } while(!state); |
| 398 | |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 399 | if (!state->curl) { |
| 400 | state->curl = curl_easy_init(); |
| 401 | if (!state->curl) { |
| 402 | return NULL; |
| 403 | } |
| 404 | curl_easy_setopt(state->curl, CURLOPT_URL, s->url); |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 405 | curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, |
| 406 | (long) s->sslverify); |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 407 | if (s->cookie) { |
| 408 | curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie); |
| 409 | } |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 410 | curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout); |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 411 | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, |
| 412 | (void *)curl_read_cb); |
| 413 | curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); |
| 414 | curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); |
| 415 | curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); |
| 416 | curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); |
| 417 | curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); |
| 418 | curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); |
| 419 | curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 420 | |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 421 | /* Restrict supported protocols to avoid security issues in the more |
| 422 | * obscure protocols. For example, do not allow POP3/SMTP/IMAP see |
| 423 | * CVE-2013-0249. |
| 424 | * |
| 425 | * Restricting protocols is only supported from 7.19.4 upwards. |
| 426 | */ |
Stefan Hajnoczi | 8a8f584 | 2013-02-13 09:25:34 +0100 | [diff] [blame] | 427 | #if LIBCURL_VERSION_NUM >= 0x071304 |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 428 | curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); |
| 429 | curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); |
Stefan Hajnoczi | 8a8f584 | 2013-02-13 09:25:34 +0100 | [diff] [blame] | 430 | #endif |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 431 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 432 | #ifdef DEBUG_VERBOSE |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 433 | curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 434 | #endif |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 435 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 436 | |
| 437 | state->s = s; |
| 438 | |
| 439 | return state; |
| 440 | } |
| 441 | |
| 442 | static void curl_clean_state(CURLState *s) |
| 443 | { |
| 444 | if (s->s->multi) |
| 445 | curl_multi_remove_handle(s->s->multi, s->curl); |
| 446 | s->in_use = 0; |
| 447 | } |
| 448 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 449 | static void curl_parse_filename(const char *filename, QDict *options, |
| 450 | Error **errp) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 451 | { |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 452 | qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename)); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 455 | static void curl_detach_aio_context(BlockDriverState *bs) |
| 456 | { |
| 457 | BDRVCURLState *s = bs->opaque; |
| 458 | int i; |
| 459 | |
| 460 | for (i = 0; i < CURL_NUM_STATES; i++) { |
| 461 | if (s->states[i].in_use) { |
| 462 | curl_clean_state(&s->states[i]); |
| 463 | } |
| 464 | if (s->states[i].curl) { |
| 465 | curl_easy_cleanup(s->states[i].curl); |
| 466 | s->states[i].curl = NULL; |
| 467 | } |
Markus Armbruster | f7047c2 | 2014-06-06 18:25:12 +0200 | [diff] [blame] | 468 | g_free(s->states[i].orig_buf); |
| 469 | s->states[i].orig_buf = NULL; |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 470 | } |
| 471 | if (s->multi) { |
| 472 | curl_multi_cleanup(s->multi); |
| 473 | s->multi = NULL; |
| 474 | } |
| 475 | |
| 476 | timer_del(&s->timer); |
| 477 | } |
| 478 | |
| 479 | static void curl_attach_aio_context(BlockDriverState *bs, |
| 480 | AioContext *new_context) |
| 481 | { |
| 482 | BDRVCURLState *s = bs->opaque; |
| 483 | |
| 484 | aio_timer_init(new_context, &s->timer, |
| 485 | QEMU_CLOCK_REALTIME, SCALE_NS, |
| 486 | curl_multi_timeout_do, s); |
| 487 | |
| 488 | assert(!s->multi); |
| 489 | s->multi = curl_multi_init(); |
| 490 | s->aio_context = new_context; |
| 491 | curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); |
| 492 | #ifdef NEED_CURL_TIMER_CALLBACK |
| 493 | curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); |
| 494 | curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); |
| 495 | #endif |
| 496 | } |
| 497 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 498 | static QemuOptsList runtime_opts = { |
| 499 | .name = "curl", |
| 500 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
| 501 | .desc = { |
| 502 | { |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 503 | .name = CURL_BLOCK_OPT_URL, |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 504 | .type = QEMU_OPT_STRING, |
| 505 | .help = "URL to open", |
| 506 | }, |
| 507 | { |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 508 | .name = CURL_BLOCK_OPT_READAHEAD, |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 509 | .type = QEMU_OPT_SIZE, |
| 510 | .help = "Readahead size", |
| 511 | }, |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 512 | { |
| 513 | .name = CURL_BLOCK_OPT_SSLVERIFY, |
| 514 | .type = QEMU_OPT_BOOL, |
| 515 | .help = "Verify SSL certificate" |
| 516 | }, |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 517 | { |
| 518 | .name = CURL_BLOCK_OPT_TIMEOUT, |
| 519 | .type = QEMU_OPT_NUMBER, |
| 520 | .help = "Curl timeout" |
| 521 | }, |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 522 | { |
| 523 | .name = CURL_BLOCK_OPT_COOKIE, |
| 524 | .type = QEMU_OPT_STRING, |
| 525 | .help = "Pass the cookie or list of cookies with each request" |
| 526 | }, |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 527 | { /* end of list */ } |
| 528 | }, |
| 529 | }; |
| 530 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 531 | static int curl_open(BlockDriverState *bs, QDict *options, int flags, |
| 532 | Error **errp) |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 533 | { |
| 534 | BDRVCURLState *s = bs->opaque; |
| 535 | CURLState *state = NULL; |
| 536 | QemuOpts *opts; |
| 537 | Error *local_err = NULL; |
| 538 | const char *file; |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 539 | const char *cookie; |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 540 | double d; |
| 541 | |
| 542 | static int inited = 0; |
| 543 | |
Richard W.M. Jones | a7cea2b | 2013-06-10 12:38:43 +0100 | [diff] [blame] | 544 | if (flags & BDRV_O_RDWR) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 545 | error_setg(errp, "curl block device does not support writes"); |
Richard W.M. Jones | a7cea2b | 2013-06-10 12:38:43 +0100 | [diff] [blame] | 546 | return -EROFS; |
| 547 | } |
| 548 | |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 549 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 550 | qemu_opts_absorb_qdict(opts, options, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 551 | if (local_err) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 552 | error_propagate(errp, local_err); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 553 | goto out_noclean; |
| 554 | } |
| 555 | |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 556 | s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, |
| 557 | READ_AHEAD_DEFAULT); |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 558 | if ((s->readahead_size & 0x1ff) != 0) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 559 | error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", |
| 560 | s->readahead_size); |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 561 | goto out_noclean; |
| 562 | } |
| 563 | |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 564 | s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, |
| 565 | CURL_TIMEOUT_DEFAULT); |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 566 | if (s->timeout > CURL_TIMEOUT_MAX) { |
| 567 | error_setg(errp, "timeout parameter is too large or negative"); |
| 568 | goto out_noclean; |
| 569 | } |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 570 | |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 571 | s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true); |
| 572 | |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 573 | cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); |
| 574 | s->cookie = g_strdup(cookie); |
| 575 | |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 576 | file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 577 | if (file == NULL) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 578 | error_setg(errp, "curl block driver requires an 'url' option"); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 579 | goto out_noclean; |
| 580 | } |
| 581 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 582 | if (!inited) { |
| 583 | curl_global_init(CURL_GLOBAL_ALL); |
| 584 | inited = 1; |
| 585 | } |
| 586 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 587 | DPRINTF("CURL: Opening %s\n", file); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 588 | s->aio_context = bdrv_get_aio_context(bs); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 589 | s->url = g_strdup(file); |
Richard W.M. Jones | a2f468e | 2014-08-28 09:04:21 +0100 | [diff] [blame] | 590 | state = curl_init_state(bs, s); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 591 | if (!state) |
| 592 | goto out_noclean; |
| 593 | |
| 594 | // Get file size |
| 595 | |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 596 | s->accept_range = false; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 597 | curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 598 | curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, |
| 599 | curl_header_cb); |
| 600 | curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 601 | if (curl_easy_perform(state->curl)) |
| 602 | goto out; |
| 603 | curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 604 | if (d) |
| 605 | s->len = (size_t)d; |
| 606 | else if(!s->len) |
| 607 | goto out; |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 608 | if ((!strncasecmp(s->url, "http://", strlen("http://")) |
| 609 | || !strncasecmp(s->url, "https://", strlen("https://"))) |
| 610 | && !s->accept_range) { |
| 611 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
| 612 | "Server does not support 'range' (byte ranges)."); |
| 613 | goto out; |
| 614 | } |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 615 | DPRINTF("CURL: Size = %zd\n", s->len); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 616 | |
| 617 | curl_clean_state(state); |
| 618 | curl_easy_cleanup(state->curl); |
| 619 | state->curl = NULL; |
| 620 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 621 | curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 622 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 623 | qemu_opts_del(opts); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 624 | return 0; |
| 625 | |
| 626 | out: |
Maria Kustova | acd7fdc | 2014-03-18 09:59:18 +0400 | [diff] [blame] | 627 | error_setg(errp, "CURL: Error opening file: %s", state->errmsg); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 628 | curl_easy_cleanup(state->curl); |
| 629 | state->curl = NULL; |
| 630 | out_noclean: |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 631 | g_free(s->cookie); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 632 | g_free(s->url); |
| 633 | qemu_opts_del(opts); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 634 | return -EINVAL; |
| 635 | } |
| 636 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 637 | static const AIOCBInfo curl_aiocb_info = { |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 638 | .aiocb_size = sizeof(CURLAIOCB), |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 639 | }; |
| 640 | |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 641 | |
| 642 | static void curl_readv_bh_cb(void *p) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 643 | { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 644 | CURLState *state; |
Matthew Booth | b69cdef | 2014-04-29 16:03:29 +0100 | [diff] [blame] | 645 | int running; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 646 | |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 647 | CURLAIOCB *acb = p; |
| 648 | BDRVCURLState *s = acb->common.bs->opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 649 | |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 650 | qemu_bh_delete(acb->bh); |
| 651 | acb->bh = NULL; |
| 652 | |
| 653 | size_t start = acb->sector_num * SECTOR_SIZE; |
| 654 | size_t end; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 655 | |
| 656 | // In case we have the requested data already (e.g. read-ahead), |
| 657 | // we can just call the callback and be done. |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 658 | switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 659 | case FIND_RET_OK: |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 660 | qemu_aio_unref(acb); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 661 | // fall through |
| 662 | case FIND_RET_WAIT: |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 663 | return; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 664 | default: |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | // No cache found, so let's start a new request |
Richard W.M. Jones | a2f468e | 2014-08-28 09:04:21 +0100 | [diff] [blame] | 669 | state = curl_init_state(acb->common.bs, s); |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 670 | if (!state) { |
| 671 | acb->common.cb(acb->common.opaque, -EIO); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 672 | qemu_aio_unref(acb); |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 673 | return; |
| 674 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 675 | |
| 676 | acb->start = 0; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 677 | acb->end = (acb->nb_sectors * SECTOR_SIZE); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 678 | |
| 679 | state->buf_off = 0; |
Markus Armbruster | f7047c2 | 2014-06-06 18:25:12 +0200 | [diff] [blame] | 680 | g_free(state->orig_buf); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 681 | state->buf_start = start; |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 682 | state->buf_len = acb->end + s->readahead_size; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 683 | end = MIN(start + state->buf_len, s->len) - 1; |
Kevin Wolf | 8dc7a77 | 2014-05-20 13:26:40 +0200 | [diff] [blame] | 684 | state->orig_buf = g_try_malloc(state->buf_len); |
| 685 | if (state->buf_len && state->orig_buf == NULL) { |
| 686 | curl_clean_state(state); |
| 687 | acb->common.cb(acb->common.opaque, -ENOMEM); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 688 | qemu_aio_unref(acb); |
Kevin Wolf | 8dc7a77 | 2014-05-20 13:26:40 +0200 | [diff] [blame] | 689 | return; |
| 690 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 691 | state->acb[0] = acb; |
| 692 | |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 693 | snprintf(state->range, 127, "%zd-%zd", start, end); |
| 694 | DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n", |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 695 | (acb->nb_sectors * SECTOR_SIZE), start, state->range); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 696 | curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); |
| 697 | |
| 698 | curl_multi_add_handle(s->multi, state->curl); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 699 | |
Matthew Booth | b69cdef | 2014-04-29 16:03:29 +0100 | [diff] [blame] | 700 | /* Tell curl it needs to kick things off */ |
| 701 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 704 | static BlockAIOCB *curl_aio_readv(BlockDriverState *bs, |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 705 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 706 | BlockCompletionFunc *cb, void *opaque) |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 707 | { |
| 708 | CURLAIOCB *acb; |
| 709 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 710 | acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque); |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 711 | |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 712 | acb->qiov = qiov; |
| 713 | acb->sector_num = sector_num; |
| 714 | acb->nb_sectors = nb_sectors; |
| 715 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 716 | acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb); |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 717 | qemu_bh_schedule(acb->bh); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 718 | return &acb->common; |
| 719 | } |
| 720 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 721 | static void curl_close(BlockDriverState *bs) |
| 722 | { |
| 723 | BDRVCURLState *s = bs->opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 724 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 725 | DPRINTF("CURL: Close\n"); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 726 | curl_detach_aio_context(bs); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 727 | |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 728 | g_free(s->cookie); |
Stefan Weil | 45724d6 | 2012-09-01 11:06:45 +0200 | [diff] [blame] | 729 | g_free(s->url); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static int64_t curl_getlength(BlockDriverState *bs) |
| 733 | { |
| 734 | BDRVCURLState *s = bs->opaque; |
| 735 | return s->len; |
| 736 | } |
| 737 | |
| 738 | static BlockDriver bdrv_http = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 739 | .format_name = "http", |
| 740 | .protocol_name = "http", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 741 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 742 | .instance_size = sizeof(BDRVCURLState), |
| 743 | .bdrv_parse_filename = curl_parse_filename, |
| 744 | .bdrv_file_open = curl_open, |
| 745 | .bdrv_close = curl_close, |
| 746 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 747 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 748 | .bdrv_aio_readv = curl_aio_readv, |
| 749 | |
| 750 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 751 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 752 | }; |
| 753 | |
| 754 | static BlockDriver bdrv_https = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 755 | .format_name = "https", |
| 756 | .protocol_name = "https", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 757 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 758 | .instance_size = sizeof(BDRVCURLState), |
| 759 | .bdrv_parse_filename = curl_parse_filename, |
| 760 | .bdrv_file_open = curl_open, |
| 761 | .bdrv_close = curl_close, |
| 762 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 763 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 764 | .bdrv_aio_readv = curl_aio_readv, |
| 765 | |
| 766 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 767 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 768 | }; |
| 769 | |
| 770 | static BlockDriver bdrv_ftp = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 771 | .format_name = "ftp", |
| 772 | .protocol_name = "ftp", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 773 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 774 | .instance_size = sizeof(BDRVCURLState), |
| 775 | .bdrv_parse_filename = curl_parse_filename, |
| 776 | .bdrv_file_open = curl_open, |
| 777 | .bdrv_close = curl_close, |
| 778 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 779 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 780 | .bdrv_aio_readv = curl_aio_readv, |
| 781 | |
| 782 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 783 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 784 | }; |
| 785 | |
| 786 | static BlockDriver bdrv_ftps = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 787 | .format_name = "ftps", |
| 788 | .protocol_name = "ftps", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 789 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 790 | .instance_size = sizeof(BDRVCURLState), |
| 791 | .bdrv_parse_filename = curl_parse_filename, |
| 792 | .bdrv_file_open = curl_open, |
| 793 | .bdrv_close = curl_close, |
| 794 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 795 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 796 | .bdrv_aio_readv = curl_aio_readv, |
| 797 | |
| 798 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 799 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 800 | }; |
| 801 | |
| 802 | static BlockDriver bdrv_tftp = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 803 | .format_name = "tftp", |
| 804 | .protocol_name = "tftp", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 805 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 806 | .instance_size = sizeof(BDRVCURLState), |
| 807 | .bdrv_parse_filename = curl_parse_filename, |
| 808 | .bdrv_file_open = curl_open, |
| 809 | .bdrv_close = curl_close, |
| 810 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 811 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 812 | .bdrv_aio_readv = curl_aio_readv, |
| 813 | |
| 814 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 815 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 816 | }; |
| 817 | |
| 818 | static void curl_block_init(void) |
| 819 | { |
| 820 | bdrv_register(&bdrv_http); |
| 821 | bdrv_register(&bdrv_https); |
| 822 | bdrv_register(&bdrv_ftp); |
| 823 | bdrv_register(&bdrv_ftps); |
| 824 | bdrv_register(&bdrv_tftp); |
| 825 | } |
| 826 | |
| 827 | block_init(curl_block_init); |