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