root / block / curl.c @ dc7588c1
History | View | Annotate | Download (16.8 kB)
1 | 769ce76d | Alexander Graf | /*
|
---|---|---|---|
2 | 769ce76d | Alexander Graf | * QEMU Block driver for CURL images
|
3 | 769ce76d | Alexander Graf | *
|
4 | 769ce76d | Alexander Graf | * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
|
5 | 769ce76d | Alexander Graf | *
|
6 | 769ce76d | Alexander Graf | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 769ce76d | Alexander Graf | * of this software and associated documentation files (the "Software"), to deal
|
8 | 769ce76d | Alexander Graf | * in the Software without restriction, including without limitation the rights
|
9 | 769ce76d | Alexander Graf | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 769ce76d | Alexander Graf | * copies of the Software, and to permit persons to whom the Software is
|
11 | 769ce76d | Alexander Graf | * furnished to do so, subject to the following conditions:
|
12 | 769ce76d | Alexander Graf | *
|
13 | 769ce76d | Alexander Graf | * The above copyright notice and this permission notice shall be included in
|
14 | 769ce76d | Alexander Graf | * all copies or substantial portions of the Software.
|
15 | 769ce76d | Alexander Graf | *
|
16 | 769ce76d | Alexander Graf | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 769ce76d | Alexander Graf | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 769ce76d | Alexander Graf | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 769ce76d | Alexander Graf | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 769ce76d | Alexander Graf | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 769ce76d | Alexander Graf | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 769ce76d | Alexander Graf | * THE SOFTWARE.
|
23 | 769ce76d | Alexander Graf | */
|
24 | 769ce76d | Alexander Graf | #include "qemu-common.h" |
25 | 737e150e | Paolo Bonzini | #include "block/block_int.h" |
26 | 769ce76d | Alexander Graf | #include <curl/curl.h> |
27 | 769ce76d | Alexander Graf | |
28 | 769ce76d | Alexander Graf | // #define DEBUG
|
29 | 769ce76d | Alexander Graf | // #define DEBUG_VERBOSE
|
30 | 769ce76d | Alexander Graf | |
31 | 769ce76d | Alexander Graf | #ifdef DEBUG_CURL
|
32 | d0f2c4c6 | malc | #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0) |
33 | 769ce76d | Alexander Graf | #else
|
34 | d0f2c4c6 | malc | #define DPRINTF(fmt, ...) do { } while (0) |
35 | 769ce76d | Alexander Graf | #endif
|
36 | 769ce76d | Alexander Graf | |
37 | fb6d1bbd | Stefan Hajnoczi | #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
|
38 | fb6d1bbd | Stefan Hajnoczi | CURLPROTO_FTP | CURLPROTO_FTPS | \ |
39 | fb6d1bbd | Stefan Hajnoczi | CURLPROTO_TFTP) |
40 | fb6d1bbd | Stefan Hajnoczi | |
41 | 769ce76d | Alexander Graf | #define CURL_NUM_STATES 8 |
42 | 769ce76d | Alexander Graf | #define CURL_NUM_ACB 8 |
43 | 769ce76d | Alexander Graf | #define SECTOR_SIZE 512 |
44 | 769ce76d | Alexander Graf | #define READ_AHEAD_SIZE (256 * 1024) |
45 | 769ce76d | Alexander Graf | |
46 | 769ce76d | Alexander Graf | #define FIND_RET_NONE 0 |
47 | 769ce76d | Alexander Graf | #define FIND_RET_OK 1 |
48 | 769ce76d | Alexander Graf | #define FIND_RET_WAIT 2 |
49 | 769ce76d | Alexander Graf | |
50 | 769ce76d | Alexander Graf | struct BDRVCURLState;
|
51 | 769ce76d | Alexander Graf | |
52 | 769ce76d | Alexander Graf | typedef struct CURLAIOCB { |
53 | 769ce76d | Alexander Graf | BlockDriverAIOCB common; |
54 | 363c3c85 | Nick Thomas | QEMUBH *bh; |
55 | 769ce76d | Alexander Graf | QEMUIOVector *qiov; |
56 | 363c3c85 | Nick Thomas | |
57 | 363c3c85 | Nick Thomas | int64_t sector_num; |
58 | 363c3c85 | Nick Thomas | int nb_sectors;
|
59 | 363c3c85 | Nick Thomas | |
60 | 769ce76d | Alexander Graf | size_t start; |
61 | 769ce76d | Alexander Graf | size_t end; |
62 | 769ce76d | Alexander Graf | } CURLAIOCB; |
63 | 769ce76d | Alexander Graf | |
64 | 769ce76d | Alexander Graf | typedef struct CURLState |
65 | 769ce76d | Alexander Graf | { |
66 | 769ce76d | Alexander Graf | struct BDRVCURLState *s;
|
67 | 769ce76d | Alexander Graf | CURLAIOCB *acb[CURL_NUM_ACB]; |
68 | 769ce76d | Alexander Graf | CURL *curl; |
69 | 769ce76d | Alexander Graf | char *orig_buf;
|
70 | 769ce76d | Alexander Graf | size_t buf_start; |
71 | 769ce76d | Alexander Graf | size_t buf_off; |
72 | 769ce76d | Alexander Graf | size_t buf_len; |
73 | 769ce76d | Alexander Graf | char range[128]; |
74 | 769ce76d | Alexander Graf | char errmsg[CURL_ERROR_SIZE];
|
75 | 769ce76d | Alexander Graf | char in_use;
|
76 | 769ce76d | Alexander Graf | } CURLState; |
77 | 769ce76d | Alexander Graf | |
78 | 769ce76d | Alexander Graf | typedef struct BDRVCURLState { |
79 | 769ce76d | Alexander Graf | CURLM *multi; |
80 | 769ce76d | Alexander Graf | size_t len; |
81 | 769ce76d | Alexander Graf | CURLState states[CURL_NUM_STATES]; |
82 | 769ce76d | Alexander Graf | char *url;
|
83 | c76f4952 | Nolan | size_t readahead_size; |
84 | 769ce76d | Alexander Graf | } BDRVCURLState; |
85 | 769ce76d | Alexander Graf | |
86 | 769ce76d | Alexander Graf | static void curl_clean_state(CURLState *s); |
87 | 769ce76d | Alexander Graf | static void curl_multi_do(void *arg); |
88 | c84dcdc1 | Nick Thomas | static int curl_aio_flush(void *opaque); |
89 | 769ce76d | Alexander Graf | |
90 | 769ce76d | Alexander Graf | static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, |
91 | 769ce76d | Alexander Graf | void *s, void *sp) |
92 | 769ce76d | Alexander Graf | { |
93 | d0f2c4c6 | malc | DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
|
94 | 769ce76d | Alexander Graf | switch (action) {
|
95 | 769ce76d | Alexander Graf | case CURL_POLL_IN:
|
96 | bafbd6a1 | Paolo Bonzini | qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, curl_aio_flush, s);
|
97 | 769ce76d | Alexander Graf | break;
|
98 | 769ce76d | Alexander Graf | case CURL_POLL_OUT:
|
99 | bafbd6a1 | Paolo Bonzini | qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, curl_aio_flush, s);
|
100 | 769ce76d | Alexander Graf | break;
|
101 | 769ce76d | Alexander Graf | case CURL_POLL_INOUT:
|
102 | c84dcdc1 | Nick Thomas | qemu_aio_set_fd_handler(fd, curl_multi_do, curl_multi_do, |
103 | bafbd6a1 | Paolo Bonzini | curl_aio_flush, s); |
104 | 769ce76d | Alexander Graf | break;
|
105 | 769ce76d | Alexander Graf | case CURL_POLL_REMOVE:
|
106 | bafbd6a1 | Paolo Bonzini | qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL); |
107 | 769ce76d | Alexander Graf | break;
|
108 | 769ce76d | Alexander Graf | } |
109 | 769ce76d | Alexander Graf | |
110 | 769ce76d | Alexander Graf | return 0; |
111 | 769ce76d | Alexander Graf | } |
112 | 769ce76d | Alexander Graf | |
113 | 769ce76d | Alexander Graf | static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
114 | 769ce76d | Alexander Graf | { |
115 | 769ce76d | Alexander Graf | CURLState *s = ((CURLState*)opaque); |
116 | 769ce76d | Alexander Graf | size_t realsize = size * nmemb; |
117 | 0bfcd599 | Blue Swirl | size_t fsize; |
118 | 769ce76d | Alexander Graf | |
119 | 0bfcd599 | Blue Swirl | if(sscanf(ptr, "Content-Length: %zd", &fsize) == 1) { |
120 | 769ce76d | Alexander Graf | s->s->len = fsize; |
121 | 0bfcd599 | Blue Swirl | } |
122 | 769ce76d | Alexander Graf | |
123 | 769ce76d | Alexander Graf | return realsize;
|
124 | 769ce76d | Alexander Graf | } |
125 | 769ce76d | Alexander Graf | |
126 | 769ce76d | Alexander Graf | static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
127 | 769ce76d | Alexander Graf | { |
128 | 769ce76d | Alexander Graf | CURLState *s = ((CURLState*)opaque); |
129 | 769ce76d | Alexander Graf | size_t realsize = size * nmemb; |
130 | 769ce76d | Alexander Graf | int i;
|
131 | 769ce76d | Alexander Graf | |
132 | 0bfcd599 | Blue Swirl | DPRINTF("CURL: Just reading %zd bytes\n", realsize);
|
133 | 769ce76d | Alexander Graf | |
134 | 769ce76d | Alexander Graf | if (!s || !s->orig_buf)
|
135 | 769ce76d | Alexander Graf | goto read_end;
|
136 | 769ce76d | Alexander Graf | |
137 | 769ce76d | Alexander Graf | memcpy(s->orig_buf + s->buf_off, ptr, realsize); |
138 | 769ce76d | Alexander Graf | s->buf_off += realsize; |
139 | 769ce76d | Alexander Graf | |
140 | 769ce76d | Alexander Graf | for(i=0; i<CURL_NUM_ACB; i++) { |
141 | 769ce76d | Alexander Graf | CURLAIOCB *acb = s->acb[i]; |
142 | 769ce76d | Alexander Graf | |
143 | 769ce76d | Alexander Graf | if (!acb)
|
144 | 769ce76d | Alexander Graf | continue;
|
145 | 769ce76d | Alexander Graf | |
146 | 769ce76d | Alexander Graf | if ((s->buf_off >= acb->end)) {
|
147 | 03396148 | Michael Tokarev | qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
|
148 | 03396148 | Michael Tokarev | acb->end - acb->start); |
149 | 769ce76d | Alexander Graf | acb->common.cb(acb->common.opaque, 0);
|
150 | 769ce76d | Alexander Graf | qemu_aio_release(acb); |
151 | 769ce76d | Alexander Graf | s->acb[i] = NULL;
|
152 | 769ce76d | Alexander Graf | } |
153 | 769ce76d | Alexander Graf | } |
154 | 769ce76d | Alexander Graf | |
155 | 769ce76d | Alexander Graf | read_end:
|
156 | 769ce76d | Alexander Graf | return realsize;
|
157 | 769ce76d | Alexander Graf | } |
158 | 769ce76d | Alexander Graf | |
159 | 769ce76d | Alexander Graf | static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len, |
160 | 769ce76d | Alexander Graf | CURLAIOCB *acb) |
161 | 769ce76d | Alexander Graf | { |
162 | 769ce76d | Alexander Graf | int i;
|
163 | 769ce76d | Alexander Graf | size_t end = start + len; |
164 | 769ce76d | Alexander Graf | |
165 | 769ce76d | Alexander Graf | for (i=0; i<CURL_NUM_STATES; i++) { |
166 | 769ce76d | Alexander Graf | CURLState *state = &s->states[i]; |
167 | 769ce76d | Alexander Graf | size_t buf_end = (state->buf_start + state->buf_off); |
168 | 769ce76d | Alexander Graf | size_t buf_fend = (state->buf_start + state->buf_len); |
169 | 769ce76d | Alexander Graf | |
170 | 769ce76d | Alexander Graf | if (!state->orig_buf)
|
171 | 769ce76d | Alexander Graf | continue;
|
172 | 769ce76d | Alexander Graf | if (!state->buf_off)
|
173 | 769ce76d | Alexander Graf | continue;
|
174 | 769ce76d | Alexander Graf | |
175 | 769ce76d | Alexander Graf | // Does the existing buffer cover our section?
|
176 | 769ce76d | Alexander Graf | if ((start >= state->buf_start) &&
|
177 | 769ce76d | Alexander Graf | (start <= buf_end) && |
178 | 769ce76d | Alexander Graf | (end >= state->buf_start) && |
179 | 769ce76d | Alexander Graf | (end <= buf_end)) |
180 | 769ce76d | Alexander Graf | { |
181 | 769ce76d | Alexander Graf | char *buf = state->orig_buf + (start - state->buf_start);
|
182 | 769ce76d | Alexander Graf | |
183 | 03396148 | Michael Tokarev | qemu_iovec_from_buf(acb->qiov, 0, buf, len);
|
184 | 769ce76d | Alexander Graf | acb->common.cb(acb->common.opaque, 0);
|
185 | 769ce76d | Alexander Graf | |
186 | 769ce76d | Alexander Graf | return FIND_RET_OK;
|
187 | 769ce76d | Alexander Graf | } |
188 | 769ce76d | Alexander Graf | |
189 | 769ce76d | Alexander Graf | // Wait for unfinished chunks
|
190 | 769ce76d | Alexander Graf | if ((start >= state->buf_start) &&
|
191 | 769ce76d | Alexander Graf | (start <= buf_fend) && |
192 | 769ce76d | Alexander Graf | (end >= state->buf_start) && |
193 | 769ce76d | Alexander Graf | (end <= buf_fend)) |
194 | 769ce76d | Alexander Graf | { |
195 | 769ce76d | Alexander Graf | int j;
|
196 | 769ce76d | Alexander Graf | |
197 | 769ce76d | Alexander Graf | acb->start = start - state->buf_start; |
198 | 769ce76d | Alexander Graf | acb->end = acb->start + len; |
199 | 769ce76d | Alexander Graf | |
200 | 769ce76d | Alexander Graf | for (j=0; j<CURL_NUM_ACB; j++) { |
201 | 769ce76d | Alexander Graf | if (!state->acb[j]) {
|
202 | 769ce76d | Alexander Graf | state->acb[j] = acb; |
203 | 769ce76d | Alexander Graf | return FIND_RET_WAIT;
|
204 | 769ce76d | Alexander Graf | } |
205 | 769ce76d | Alexander Graf | } |
206 | 769ce76d | Alexander Graf | } |
207 | 769ce76d | Alexander Graf | } |
208 | 769ce76d | Alexander Graf | |
209 | 769ce76d | Alexander Graf | return FIND_RET_NONE;
|
210 | 769ce76d | Alexander Graf | } |
211 | 769ce76d | Alexander Graf | |
212 | 769ce76d | Alexander Graf | static void curl_multi_do(void *arg) |
213 | 769ce76d | Alexander Graf | { |
214 | 769ce76d | Alexander Graf | BDRVCURLState *s = (BDRVCURLState *)arg; |
215 | 769ce76d | Alexander Graf | int running;
|
216 | 769ce76d | Alexander Graf | int r;
|
217 | 769ce76d | Alexander Graf | int msgs_in_queue;
|
218 | 769ce76d | Alexander Graf | |
219 | 769ce76d | Alexander Graf | if (!s->multi)
|
220 | 769ce76d | Alexander Graf | return;
|
221 | 769ce76d | Alexander Graf | |
222 | 769ce76d | Alexander Graf | do {
|
223 | 769ce76d | Alexander Graf | r = curl_multi_socket_all(s->multi, &running); |
224 | 769ce76d | Alexander Graf | } while(r == CURLM_CALL_MULTI_PERFORM);
|
225 | 769ce76d | Alexander Graf | |
226 | 769ce76d | Alexander Graf | /* Try to find done transfers, so we can free the easy
|
227 | 769ce76d | Alexander Graf | * handle again. */
|
228 | 769ce76d | Alexander Graf | do {
|
229 | 769ce76d | Alexander Graf | CURLMsg *msg; |
230 | 769ce76d | Alexander Graf | msg = curl_multi_info_read(s->multi, &msgs_in_queue); |
231 | 769ce76d | Alexander Graf | |
232 | 769ce76d | Alexander Graf | if (!msg)
|
233 | 769ce76d | Alexander Graf | break;
|
234 | 769ce76d | Alexander Graf | if (msg->msg == CURLMSG_NONE)
|
235 | 769ce76d | Alexander Graf | break;
|
236 | 769ce76d | Alexander Graf | |
237 | 769ce76d | Alexander Graf | switch (msg->msg) {
|
238 | 769ce76d | Alexander Graf | case CURLMSG_DONE:
|
239 | 769ce76d | Alexander Graf | { |
240 | 769ce76d | Alexander Graf | CURLState *state = NULL;
|
241 | 769ce76d | Alexander Graf | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
|
242 | f785a5ae | Nicholas Thomas | |
243 | f785a5ae | Nicholas Thomas | /* ACBs for successful messages get completed in curl_read_cb */
|
244 | f785a5ae | Nicholas Thomas | if (msg->data.result != CURLE_OK) {
|
245 | f785a5ae | Nicholas Thomas | int i;
|
246 | f785a5ae | Nicholas Thomas | for (i = 0; i < CURL_NUM_ACB; i++) { |
247 | f785a5ae | Nicholas Thomas | CURLAIOCB *acb = state->acb[i]; |
248 | f785a5ae | Nicholas Thomas | |
249 | f785a5ae | Nicholas Thomas | if (acb == NULL) { |
250 | f785a5ae | Nicholas Thomas | continue;
|
251 | f785a5ae | Nicholas Thomas | } |
252 | f785a5ae | Nicholas Thomas | |
253 | f785a5ae | Nicholas Thomas | acb->common.cb(acb->common.opaque, -EIO); |
254 | f785a5ae | Nicholas Thomas | qemu_aio_release(acb); |
255 | f785a5ae | Nicholas Thomas | state->acb[i] = NULL;
|
256 | f785a5ae | Nicholas Thomas | } |
257 | f785a5ae | Nicholas Thomas | } |
258 | f785a5ae | Nicholas Thomas | |
259 | 769ce76d | Alexander Graf | curl_clean_state(state); |
260 | 769ce76d | Alexander Graf | break;
|
261 | 769ce76d | Alexander Graf | } |
262 | 769ce76d | Alexander Graf | default:
|
263 | 769ce76d | Alexander Graf | msgs_in_queue = 0;
|
264 | 769ce76d | Alexander Graf | break;
|
265 | 769ce76d | Alexander Graf | } |
266 | 769ce76d | Alexander Graf | } while(msgs_in_queue);
|
267 | 769ce76d | Alexander Graf | } |
268 | 769ce76d | Alexander Graf | |
269 | 769ce76d | Alexander Graf | static CURLState *curl_init_state(BDRVCURLState *s)
|
270 | 769ce76d | Alexander Graf | { |
271 | 769ce76d | Alexander Graf | CURLState *state = NULL;
|
272 | 769ce76d | Alexander Graf | int i, j;
|
273 | 769ce76d | Alexander Graf | |
274 | 769ce76d | Alexander Graf | do {
|
275 | 769ce76d | Alexander Graf | for (i=0; i<CURL_NUM_STATES; i++) { |
276 | 769ce76d | Alexander Graf | for (j=0; j<CURL_NUM_ACB; j++) |
277 | 769ce76d | Alexander Graf | if (s->states[i].acb[j])
|
278 | 769ce76d | Alexander Graf | continue;
|
279 | 769ce76d | Alexander Graf | if (s->states[i].in_use)
|
280 | 769ce76d | Alexander Graf | continue;
|
281 | 769ce76d | Alexander Graf | |
282 | 769ce76d | Alexander Graf | state = &s->states[i]; |
283 | 769ce76d | Alexander Graf | state->in_use = 1;
|
284 | 769ce76d | Alexander Graf | break;
|
285 | 769ce76d | Alexander Graf | } |
286 | 769ce76d | Alexander Graf | if (!state) {
|
287 | fb7c8e8a | Stefan Weil | g_usleep(100);
|
288 | 769ce76d | Alexander Graf | curl_multi_do(s); |
289 | 769ce76d | Alexander Graf | } |
290 | 769ce76d | Alexander Graf | } while(!state);
|
291 | 769ce76d | Alexander Graf | |
292 | 769ce76d | Alexander Graf | if (state->curl)
|
293 | 769ce76d | Alexander Graf | goto has_curl;
|
294 | 769ce76d | Alexander Graf | |
295 | 769ce76d | Alexander Graf | state->curl = curl_easy_init(); |
296 | 769ce76d | Alexander Graf | if (!state->curl)
|
297 | 769ce76d | Alexander Graf | return NULL; |
298 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_URL, s->url); |
299 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
|
300 | df3cee1a | Blue Swirl | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
|
301 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
|
302 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
|
303 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
|
304 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
|
305 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
|
306 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); |
307 | f785a5ae | Nicholas Thomas | curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
|
308 | f785a5ae | Nicholas Thomas | |
309 | fb6d1bbd | Stefan Hajnoczi | /* Restrict supported protocols to avoid security issues in the more
|
310 | fb6d1bbd | Stefan Hajnoczi | * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
|
311 | fb6d1bbd | Stefan Hajnoczi | * CVE-2013-0249.
|
312 | 8a8f5840 | Stefan Hajnoczi | *
|
313 | 8a8f5840 | Stefan Hajnoczi | * Restricting protocols is only supported from 7.19.4 upwards.
|
314 | fb6d1bbd | Stefan Hajnoczi | */
|
315 | 8a8f5840 | Stefan Hajnoczi | #if LIBCURL_VERSION_NUM >= 0x071304 |
316 | fb6d1bbd | Stefan Hajnoczi | curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); |
317 | fb6d1bbd | Stefan Hajnoczi | curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); |
318 | 8a8f5840 | Stefan Hajnoczi | #endif
|
319 | fb6d1bbd | Stefan Hajnoczi | |
320 | 769ce76d | Alexander Graf | #ifdef DEBUG_VERBOSE
|
321 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
|
322 | 769ce76d | Alexander Graf | #endif
|
323 | 769ce76d | Alexander Graf | |
324 | 769ce76d | Alexander Graf | has_curl:
|
325 | 769ce76d | Alexander Graf | |
326 | 769ce76d | Alexander Graf | state->s = s; |
327 | 769ce76d | Alexander Graf | |
328 | 769ce76d | Alexander Graf | return state;
|
329 | 769ce76d | Alexander Graf | } |
330 | 769ce76d | Alexander Graf | |
331 | 769ce76d | Alexander Graf | static void curl_clean_state(CURLState *s) |
332 | 769ce76d | Alexander Graf | { |
333 | 769ce76d | Alexander Graf | if (s->s->multi)
|
334 | 769ce76d | Alexander Graf | curl_multi_remove_handle(s->s->multi, s->curl); |
335 | 769ce76d | Alexander Graf | s->in_use = 0;
|
336 | 769ce76d | Alexander Graf | } |
337 | 769ce76d | Alexander Graf | |
338 | 787e4a85 | Kevin Wolf | static int curl_open(BlockDriverState *bs, const char *filename, |
339 | 787e4a85 | Kevin Wolf | QDict *options, int flags)
|
340 | 769ce76d | Alexander Graf | { |
341 | 769ce76d | Alexander Graf | BDRVCURLState *s = bs->opaque; |
342 | 769ce76d | Alexander Graf | CURLState *state = NULL;
|
343 | 769ce76d | Alexander Graf | double d;
|
344 | c76f4952 | Nolan | |
345 | c76f4952 | Nolan | #define RA_OPTSTR ":readahead=" |
346 | c76f4952 | Nolan | char *file;
|
347 | c76f4952 | Nolan | char *ra;
|
348 | c76f4952 | Nolan | const char *ra_val; |
349 | c76f4952 | Nolan | int parse_state = 0; |
350 | c76f4952 | Nolan | |
351 | 769ce76d | Alexander Graf | static int inited = 0; |
352 | 769ce76d | Alexander Graf | |
353 | 7267c094 | Anthony Liguori | file = g_strdup(filename); |
354 | c76f4952 | Nolan | s->readahead_size = READ_AHEAD_SIZE; |
355 | c76f4952 | Nolan | |
356 | c76f4952 | Nolan | /* Parse a trailing ":readahead=#:" param, if present. */
|
357 | c76f4952 | Nolan | ra = file + strlen(file) - 1;
|
358 | c76f4952 | Nolan | while (ra >= file) {
|
359 | c76f4952 | Nolan | if (parse_state == 0) { |
360 | c76f4952 | Nolan | if (*ra == ':') |
361 | c76f4952 | Nolan | parse_state++; |
362 | c76f4952 | Nolan | else
|
363 | c76f4952 | Nolan | break;
|
364 | c76f4952 | Nolan | } else if (parse_state == 1) { |
365 | c76f4952 | Nolan | if (*ra > '9' || *ra < '0') { |
366 | c76f4952 | Nolan | char *opt_start = ra - strlen(RA_OPTSTR) + 1; |
367 | c76f4952 | Nolan | if (opt_start > file &&
|
368 | c76f4952 | Nolan | strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) {
|
369 | c76f4952 | Nolan | ra_val = ra + 1;
|
370 | c76f4952 | Nolan | ra -= strlen(RA_OPTSTR) - 1;
|
371 | c76f4952 | Nolan | *ra = '\0';
|
372 | c76f4952 | Nolan | s->readahead_size = atoi(ra_val); |
373 | c76f4952 | Nolan | break;
|
374 | c76f4952 | Nolan | } else {
|
375 | c76f4952 | Nolan | break;
|
376 | c76f4952 | Nolan | } |
377 | c76f4952 | Nolan | } |
378 | c76f4952 | Nolan | } |
379 | c76f4952 | Nolan | ra--; |
380 | c76f4952 | Nolan | } |
381 | c76f4952 | Nolan | |
382 | c76f4952 | Nolan | if ((s->readahead_size & 0x1ff) != 0) { |
383 | 48a402e6 | malc | fprintf(stderr, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n",
|
384 | c76f4952 | Nolan | s->readahead_size); |
385 | c76f4952 | Nolan | goto out_noclean;
|
386 | c76f4952 | Nolan | } |
387 | c76f4952 | Nolan | |
388 | 769ce76d | Alexander Graf | if (!inited) {
|
389 | 769ce76d | Alexander Graf | curl_global_init(CURL_GLOBAL_ALL); |
390 | 769ce76d | Alexander Graf | inited = 1;
|
391 | 769ce76d | Alexander Graf | } |
392 | 769ce76d | Alexander Graf | |
393 | d0f2c4c6 | malc | DPRINTF("CURL: Opening %s\n", file);
|
394 | c76f4952 | Nolan | s->url = file; |
395 | 769ce76d | Alexander Graf | state = curl_init_state(s); |
396 | 769ce76d | Alexander Graf | if (!state)
|
397 | 769ce76d | Alexander Graf | goto out_noclean;
|
398 | 769ce76d | Alexander Graf | |
399 | 769ce76d | Alexander Graf | // Get file size
|
400 | 769ce76d | Alexander Graf | |
401 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
|
402 | df3cee1a | Blue Swirl | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_size_cb);
|
403 | 769ce76d | Alexander Graf | if (curl_easy_perform(state->curl))
|
404 | 769ce76d | Alexander Graf | goto out;
|
405 | 769ce76d | Alexander Graf | curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d); |
406 | df3cee1a | Blue Swirl | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
|
407 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0);
|
408 | 769ce76d | Alexander Graf | if (d)
|
409 | 769ce76d | Alexander Graf | s->len = (size_t)d; |
410 | 769ce76d | Alexander Graf | else if(!s->len) |
411 | 769ce76d | Alexander Graf | goto out;
|
412 | 0bfcd599 | Blue Swirl | DPRINTF("CURL: Size = %zd\n", s->len);
|
413 | 769ce76d | Alexander Graf | |
414 | 769ce76d | Alexander Graf | curl_clean_state(state); |
415 | 769ce76d | Alexander Graf | curl_easy_cleanup(state->curl); |
416 | 769ce76d | Alexander Graf | state->curl = NULL;
|
417 | 769ce76d | Alexander Graf | |
418 | 769ce76d | Alexander Graf | // Now we know the file exists and its size, so let's
|
419 | 769ce76d | Alexander Graf | // initialize the multi interface!
|
420 | 769ce76d | Alexander Graf | |
421 | 769ce76d | Alexander Graf | s->multi = curl_multi_init(); |
422 | 769ce76d | Alexander Graf | curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s); |
423 | 769ce76d | Alexander Graf | curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb ); |
424 | 769ce76d | Alexander Graf | curl_multi_do(s); |
425 | 769ce76d | Alexander Graf | |
426 | 769ce76d | Alexander Graf | return 0; |
427 | 769ce76d | Alexander Graf | |
428 | 769ce76d | Alexander Graf | out:
|
429 | 769ce76d | Alexander Graf | fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
|
430 | 769ce76d | Alexander Graf | curl_easy_cleanup(state->curl); |
431 | 769ce76d | Alexander Graf | state->curl = NULL;
|
432 | 769ce76d | Alexander Graf | out_noclean:
|
433 | 7267c094 | Anthony Liguori | g_free(file); |
434 | 769ce76d | Alexander Graf | return -EINVAL;
|
435 | 769ce76d | Alexander Graf | } |
436 | 769ce76d | Alexander Graf | |
437 | c84dcdc1 | Nick Thomas | static int curl_aio_flush(void *opaque) |
438 | c84dcdc1 | Nick Thomas | { |
439 | c84dcdc1 | Nick Thomas | BDRVCURLState *s = opaque; |
440 | c84dcdc1 | Nick Thomas | int i, j;
|
441 | c84dcdc1 | Nick Thomas | |
442 | c84dcdc1 | Nick Thomas | for (i=0; i < CURL_NUM_STATES; i++) { |
443 | c84dcdc1 | Nick Thomas | for(j=0; j < CURL_NUM_ACB; j++) { |
444 | c84dcdc1 | Nick Thomas | if (s->states[i].acb[j]) {
|
445 | c84dcdc1 | Nick Thomas | return 1; |
446 | c84dcdc1 | Nick Thomas | } |
447 | c84dcdc1 | Nick Thomas | } |
448 | c84dcdc1 | Nick Thomas | } |
449 | c84dcdc1 | Nick Thomas | return 0; |
450 | c84dcdc1 | Nick Thomas | } |
451 | c84dcdc1 | Nick Thomas | |
452 | c16b5a2c | Christoph Hellwig | static void curl_aio_cancel(BlockDriverAIOCB *blockacb) |
453 | c16b5a2c | Christoph Hellwig | { |
454 | c16b5a2c | Christoph Hellwig | // Do we have to implement canceling? Seems to work without...
|
455 | c16b5a2c | Christoph Hellwig | } |
456 | c16b5a2c | Christoph Hellwig | |
457 | d7331bed | Stefan Hajnoczi | static const AIOCBInfo curl_aiocb_info = { |
458 | c16b5a2c | Christoph Hellwig | .aiocb_size = sizeof(CURLAIOCB),
|
459 | c16b5a2c | Christoph Hellwig | .cancel = curl_aio_cancel, |
460 | c16b5a2c | Christoph Hellwig | }; |
461 | c16b5a2c | Christoph Hellwig | |
462 | 363c3c85 | Nick Thomas | |
463 | 363c3c85 | Nick Thomas | static void curl_readv_bh_cb(void *p) |
464 | 769ce76d | Alexander Graf | { |
465 | 769ce76d | Alexander Graf | CURLState *state; |
466 | 769ce76d | Alexander Graf | |
467 | 363c3c85 | Nick Thomas | CURLAIOCB *acb = p; |
468 | 363c3c85 | Nick Thomas | BDRVCURLState *s = acb->common.bs->opaque; |
469 | 769ce76d | Alexander Graf | |
470 | 363c3c85 | Nick Thomas | qemu_bh_delete(acb->bh); |
471 | 363c3c85 | Nick Thomas | acb->bh = NULL;
|
472 | 363c3c85 | Nick Thomas | |
473 | 363c3c85 | Nick Thomas | size_t start = acb->sector_num * SECTOR_SIZE; |
474 | 363c3c85 | Nick Thomas | size_t end; |
475 | 769ce76d | Alexander Graf | |
476 | 769ce76d | Alexander Graf | // In case we have the requested data already (e.g. read-ahead),
|
477 | 769ce76d | Alexander Graf | // we can just call the callback and be done.
|
478 | 363c3c85 | Nick Thomas | switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
|
479 | 769ce76d | Alexander Graf | case FIND_RET_OK:
|
480 | 769ce76d | Alexander Graf | qemu_aio_release(acb); |
481 | 769ce76d | Alexander Graf | // fall through
|
482 | 769ce76d | Alexander Graf | case FIND_RET_WAIT:
|
483 | 363c3c85 | Nick Thomas | return;
|
484 | 769ce76d | Alexander Graf | default:
|
485 | 769ce76d | Alexander Graf | break;
|
486 | 769ce76d | Alexander Graf | } |
487 | 769ce76d | Alexander Graf | |
488 | 769ce76d | Alexander Graf | // No cache found, so let's start a new request
|
489 | 769ce76d | Alexander Graf | state = curl_init_state(s); |
490 | 363c3c85 | Nick Thomas | if (!state) {
|
491 | 363c3c85 | Nick Thomas | acb->common.cb(acb->common.opaque, -EIO); |
492 | 363c3c85 | Nick Thomas | qemu_aio_release(acb); |
493 | 363c3c85 | Nick Thomas | return;
|
494 | 363c3c85 | Nick Thomas | } |
495 | 769ce76d | Alexander Graf | |
496 | 769ce76d | Alexander Graf | acb->start = 0;
|
497 | 363c3c85 | Nick Thomas | acb->end = (acb->nb_sectors * SECTOR_SIZE); |
498 | 769ce76d | Alexander Graf | |
499 | 769ce76d | Alexander Graf | state->buf_off = 0;
|
500 | 769ce76d | Alexander Graf | if (state->orig_buf)
|
501 | 7267c094 | Anthony Liguori | g_free(state->orig_buf); |
502 | 769ce76d | Alexander Graf | state->buf_start = start; |
503 | c76f4952 | Nolan | state->buf_len = acb->end + s->readahead_size; |
504 | 769ce76d | Alexander Graf | end = MIN(start + state->buf_len, s->len) - 1;
|
505 | 7267c094 | Anthony Liguori | state->orig_buf = g_malloc(state->buf_len); |
506 | 769ce76d | Alexander Graf | state->acb[0] = acb;
|
507 | 769ce76d | Alexander Graf | |
508 | 0bfcd599 | Blue Swirl | snprintf(state->range, 127, "%zd-%zd", start, end); |
509 | 0bfcd599 | Blue Swirl | DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
|
510 | 363c3c85 | Nick Thomas | (acb->nb_sectors * SECTOR_SIZE), start, state->range); |
511 | 769ce76d | Alexander Graf | curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); |
512 | 769ce76d | Alexander Graf | |
513 | 769ce76d | Alexander Graf | curl_multi_add_handle(s->multi, state->curl); |
514 | 769ce76d | Alexander Graf | curl_multi_do(s); |
515 | 769ce76d | Alexander Graf | |
516 | 363c3c85 | Nick Thomas | } |
517 | 363c3c85 | Nick Thomas | |
518 | 363c3c85 | Nick Thomas | static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
|
519 | 363c3c85 | Nick Thomas | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
520 | 363c3c85 | Nick Thomas | BlockDriverCompletionFunc *cb, void *opaque)
|
521 | 363c3c85 | Nick Thomas | { |
522 | 363c3c85 | Nick Thomas | CURLAIOCB *acb; |
523 | 363c3c85 | Nick Thomas | |
524 | d7331bed | Stefan Hajnoczi | acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque); |
525 | 363c3c85 | Nick Thomas | |
526 | 363c3c85 | Nick Thomas | acb->qiov = qiov; |
527 | 363c3c85 | Nick Thomas | acb->sector_num = sector_num; |
528 | 363c3c85 | Nick Thomas | acb->nb_sectors = nb_sectors; |
529 | 363c3c85 | Nick Thomas | |
530 | 363c3c85 | Nick Thomas | acb->bh = qemu_bh_new(curl_readv_bh_cb, acb); |
531 | 363c3c85 | Nick Thomas | |
532 | 363c3c85 | Nick Thomas | if (!acb->bh) {
|
533 | 363c3c85 | Nick Thomas | DPRINTF("CURL: qemu_bh_new failed\n");
|
534 | 363c3c85 | Nick Thomas | return NULL; |
535 | 363c3c85 | Nick Thomas | } |
536 | 363c3c85 | Nick Thomas | |
537 | 363c3c85 | Nick Thomas | qemu_bh_schedule(acb->bh); |
538 | 769ce76d | Alexander Graf | return &acb->common;
|
539 | 769ce76d | Alexander Graf | } |
540 | 769ce76d | Alexander Graf | |
541 | 769ce76d | Alexander Graf | static void curl_close(BlockDriverState *bs) |
542 | 769ce76d | Alexander Graf | { |
543 | 769ce76d | Alexander Graf | BDRVCURLState *s = bs->opaque; |
544 | 769ce76d | Alexander Graf | int i;
|
545 | 769ce76d | Alexander Graf | |
546 | d0f2c4c6 | malc | DPRINTF("CURL: Close\n");
|
547 | 769ce76d | Alexander Graf | for (i=0; i<CURL_NUM_STATES; i++) { |
548 | 769ce76d | Alexander Graf | if (s->states[i].in_use)
|
549 | 769ce76d | Alexander Graf | curl_clean_state(&s->states[i]); |
550 | 769ce76d | Alexander Graf | if (s->states[i].curl) {
|
551 | 769ce76d | Alexander Graf | curl_easy_cleanup(s->states[i].curl); |
552 | 769ce76d | Alexander Graf | s->states[i].curl = NULL;
|
553 | 769ce76d | Alexander Graf | } |
554 | 769ce76d | Alexander Graf | if (s->states[i].orig_buf) {
|
555 | 7267c094 | Anthony Liguori | g_free(s->states[i].orig_buf); |
556 | 769ce76d | Alexander Graf | s->states[i].orig_buf = NULL;
|
557 | 769ce76d | Alexander Graf | } |
558 | 769ce76d | Alexander Graf | } |
559 | 769ce76d | Alexander Graf | if (s->multi)
|
560 | 769ce76d | Alexander Graf | curl_multi_cleanup(s->multi); |
561 | 45724d6d | Stefan Weil | g_free(s->url); |
562 | 769ce76d | Alexander Graf | } |
563 | 769ce76d | Alexander Graf | |
564 | 769ce76d | Alexander Graf | static int64_t curl_getlength(BlockDriverState *bs)
|
565 | 769ce76d | Alexander Graf | { |
566 | 769ce76d | Alexander Graf | BDRVCURLState *s = bs->opaque; |
567 | 769ce76d | Alexander Graf | return s->len;
|
568 | 769ce76d | Alexander Graf | } |
569 | 769ce76d | Alexander Graf | |
570 | 769ce76d | Alexander Graf | static BlockDriver bdrv_http = {
|
571 | 769ce76d | Alexander Graf | .format_name = "http",
|
572 | 769ce76d | Alexander Graf | .protocol_name = "http",
|
573 | 769ce76d | Alexander Graf | |
574 | 769ce76d | Alexander Graf | .instance_size = sizeof(BDRVCURLState),
|
575 | 66f82cee | Kevin Wolf | .bdrv_file_open = curl_open, |
576 | 769ce76d | Alexander Graf | .bdrv_close = curl_close, |
577 | 769ce76d | Alexander Graf | .bdrv_getlength = curl_getlength, |
578 | 769ce76d | Alexander Graf | |
579 | 769ce76d | Alexander Graf | .bdrv_aio_readv = curl_aio_readv, |
580 | 769ce76d | Alexander Graf | }; |
581 | 769ce76d | Alexander Graf | |
582 | 769ce76d | Alexander Graf | static BlockDriver bdrv_https = {
|
583 | 769ce76d | Alexander Graf | .format_name = "https",
|
584 | 769ce76d | Alexander Graf | .protocol_name = "https",
|
585 | 769ce76d | Alexander Graf | |
586 | 769ce76d | Alexander Graf | .instance_size = sizeof(BDRVCURLState),
|
587 | 66f82cee | Kevin Wolf | .bdrv_file_open = curl_open, |
588 | 769ce76d | Alexander Graf | .bdrv_close = curl_close, |
589 | 769ce76d | Alexander Graf | .bdrv_getlength = curl_getlength, |
590 | 769ce76d | Alexander Graf | |
591 | 769ce76d | Alexander Graf | .bdrv_aio_readv = curl_aio_readv, |
592 | 769ce76d | Alexander Graf | }; |
593 | 769ce76d | Alexander Graf | |
594 | 769ce76d | Alexander Graf | static BlockDriver bdrv_ftp = {
|
595 | 769ce76d | Alexander Graf | .format_name = "ftp",
|
596 | 769ce76d | Alexander Graf | .protocol_name = "ftp",
|
597 | 769ce76d | Alexander Graf | |
598 | 769ce76d | Alexander Graf | .instance_size = sizeof(BDRVCURLState),
|
599 | 66f82cee | Kevin Wolf | .bdrv_file_open = curl_open, |
600 | 769ce76d | Alexander Graf | .bdrv_close = curl_close, |
601 | 769ce76d | Alexander Graf | .bdrv_getlength = curl_getlength, |
602 | 769ce76d | Alexander Graf | |
603 | 769ce76d | Alexander Graf | .bdrv_aio_readv = curl_aio_readv, |
604 | 769ce76d | Alexander Graf | }; |
605 | 769ce76d | Alexander Graf | |
606 | 769ce76d | Alexander Graf | static BlockDriver bdrv_ftps = {
|
607 | 769ce76d | Alexander Graf | .format_name = "ftps",
|
608 | 769ce76d | Alexander Graf | .protocol_name = "ftps",
|
609 | 769ce76d | Alexander Graf | |
610 | 769ce76d | Alexander Graf | .instance_size = sizeof(BDRVCURLState),
|
611 | 66f82cee | Kevin Wolf | .bdrv_file_open = curl_open, |
612 | 769ce76d | Alexander Graf | .bdrv_close = curl_close, |
613 | 769ce76d | Alexander Graf | .bdrv_getlength = curl_getlength, |
614 | 769ce76d | Alexander Graf | |
615 | 769ce76d | Alexander Graf | .bdrv_aio_readv = curl_aio_readv, |
616 | 769ce76d | Alexander Graf | }; |
617 | 769ce76d | Alexander Graf | |
618 | 769ce76d | Alexander Graf | static BlockDriver bdrv_tftp = {
|
619 | 769ce76d | Alexander Graf | .format_name = "tftp",
|
620 | 769ce76d | Alexander Graf | .protocol_name = "tftp",
|
621 | 769ce76d | Alexander Graf | |
622 | 769ce76d | Alexander Graf | .instance_size = sizeof(BDRVCURLState),
|
623 | 66f82cee | Kevin Wolf | .bdrv_file_open = curl_open, |
624 | 769ce76d | Alexander Graf | .bdrv_close = curl_close, |
625 | 769ce76d | Alexander Graf | .bdrv_getlength = curl_getlength, |
626 | 769ce76d | Alexander Graf | |
627 | 769ce76d | Alexander Graf | .bdrv_aio_readv = curl_aio_readv, |
628 | 769ce76d | Alexander Graf | }; |
629 | 769ce76d | Alexander Graf | |
630 | 769ce76d | Alexander Graf | static void curl_block_init(void) |
631 | 769ce76d | Alexander Graf | { |
632 | 769ce76d | Alexander Graf | bdrv_register(&bdrv_http); |
633 | 769ce76d | Alexander Graf | bdrv_register(&bdrv_https); |
634 | 769ce76d | Alexander Graf | bdrv_register(&bdrv_ftp); |
635 | 769ce76d | Alexander Graf | bdrv_register(&bdrv_ftps); |
636 | 769ce76d | Alexander Graf | bdrv_register(&bdrv_tftp); |
637 | 769ce76d | Alexander Graf | } |
638 | 769ce76d | Alexander Graf | |
639 | 769ce76d | Alexander Graf | block_init(curl_block_init); |