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