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