1769ce76dSAlexander Graf /* 2769ce76dSAlexander Graf * QEMU Block driver for CURL images 3769ce76dSAlexander Graf * 4769ce76dSAlexander Graf * Copyright (c) 2009 Alexander Graf <agraf@suse.de> 5769ce76dSAlexander Graf * 6769ce76dSAlexander Graf * Permission is hereby granted, free of charge, to any person obtaining a copy 7769ce76dSAlexander Graf * of this software and associated documentation files (the "Software"), to deal 8769ce76dSAlexander Graf * in the Software without restriction, including without limitation the rights 9769ce76dSAlexander Graf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10769ce76dSAlexander Graf * copies of the Software, and to permit persons to whom the Software is 11769ce76dSAlexander Graf * furnished to do so, subject to the following conditions: 12769ce76dSAlexander Graf * 13769ce76dSAlexander Graf * The above copyright notice and this permission notice shall be included in 14769ce76dSAlexander Graf * all copies or substantial portions of the Software. 15769ce76dSAlexander Graf * 16769ce76dSAlexander Graf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17769ce76dSAlexander Graf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18769ce76dSAlexander Graf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19769ce76dSAlexander Graf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20769ce76dSAlexander Graf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21769ce76dSAlexander Graf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22769ce76dSAlexander Graf * THE SOFTWARE. 23769ce76dSAlexander Graf */ 24769ce76dSAlexander Graf #include "qemu-common.h" 25737e150eSPaolo Bonzini #include "block/block_int.h" 26*97a3ea57SMatthew Booth #include "qapi/qmp/qbool.h" 27769ce76dSAlexander Graf #include <curl/curl.h> 28769ce76dSAlexander Graf 29769ce76dSAlexander Graf // #define DEBUG 30769ce76dSAlexander Graf // #define DEBUG_VERBOSE 31769ce76dSAlexander Graf 32769ce76dSAlexander Graf #ifdef DEBUG_CURL 33d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0) 34769ce76dSAlexander Graf #else 35d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { } while (0) 36769ce76dSAlexander Graf #endif 37769ce76dSAlexander Graf 38031fd1beSPeter Maydell #if LIBCURL_VERSION_NUM >= 0x071000 39031fd1beSPeter Maydell /* The multi interface timer callback was introduced in 7.16.0 */ 40031fd1beSPeter Maydell #define NEED_CURL_TIMER_CALLBACK 419aedd5a5SMatthew Booth #define HAVE_SOCKET_ACTION 429aedd5a5SMatthew Booth #endif 439aedd5a5SMatthew Booth 449aedd5a5SMatthew Booth #ifndef HAVE_SOCKET_ACTION 459aedd5a5SMatthew Booth /* If curl_multi_socket_action isn't available, define it statically here in 469aedd5a5SMatthew Booth * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is 479aedd5a5SMatthew Booth * less efficient but still safe. */ 489aedd5a5SMatthew Booth static CURLMcode __curl_multi_socket_action(CURLM *multi_handle, 499aedd5a5SMatthew Booth curl_socket_t sockfd, 509aedd5a5SMatthew Booth int ev_bitmask, 519aedd5a5SMatthew Booth int *running_handles) 529aedd5a5SMatthew Booth { 539aedd5a5SMatthew Booth return curl_multi_socket(multi_handle, sockfd, running_handles); 549aedd5a5SMatthew Booth } 559aedd5a5SMatthew Booth #define curl_multi_socket_action __curl_multi_socket_action 56031fd1beSPeter Maydell #endif 57031fd1beSPeter Maydell 58fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ 59fb6d1bbdSStefan Hajnoczi CURLPROTO_FTP | CURLPROTO_FTPS | \ 60fb6d1bbdSStefan Hajnoczi CURLPROTO_TFTP) 61fb6d1bbdSStefan Hajnoczi 62769ce76dSAlexander Graf #define CURL_NUM_STATES 8 63769ce76dSAlexander Graf #define CURL_NUM_ACB 8 64769ce76dSAlexander Graf #define SECTOR_SIZE 512 65e3542c67SMatthew Booth #define READ_AHEAD_DEFAULT (256 * 1024) 66769ce76dSAlexander Graf 67769ce76dSAlexander Graf #define FIND_RET_NONE 0 68769ce76dSAlexander Graf #define FIND_RET_OK 1 69769ce76dSAlexander Graf #define FIND_RET_WAIT 2 70769ce76dSAlexander Graf 71e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL "url" 72e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead" 73*97a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" 74e3542c67SMatthew Booth 75769ce76dSAlexander Graf struct BDRVCURLState; 76769ce76dSAlexander Graf 77769ce76dSAlexander Graf typedef struct CURLAIOCB { 78769ce76dSAlexander Graf BlockDriverAIOCB common; 79363c3c85SNick Thomas QEMUBH *bh; 80769ce76dSAlexander Graf QEMUIOVector *qiov; 81363c3c85SNick Thomas 82363c3c85SNick Thomas int64_t sector_num; 83363c3c85SNick Thomas int nb_sectors; 84363c3c85SNick Thomas 85769ce76dSAlexander Graf size_t start; 86769ce76dSAlexander Graf size_t end; 87769ce76dSAlexander Graf } CURLAIOCB; 88769ce76dSAlexander Graf 89769ce76dSAlexander Graf typedef struct CURLState 90769ce76dSAlexander Graf { 91769ce76dSAlexander Graf struct BDRVCURLState *s; 92769ce76dSAlexander Graf CURLAIOCB *acb[CURL_NUM_ACB]; 93769ce76dSAlexander Graf CURL *curl; 94838ef602SMatthew Booth curl_socket_t sock_fd; 95769ce76dSAlexander Graf char *orig_buf; 96769ce76dSAlexander Graf size_t buf_start; 97769ce76dSAlexander Graf size_t buf_off; 98769ce76dSAlexander Graf size_t buf_len; 99769ce76dSAlexander Graf char range[128]; 100769ce76dSAlexander Graf char errmsg[CURL_ERROR_SIZE]; 101769ce76dSAlexander Graf char in_use; 102769ce76dSAlexander Graf } CURLState; 103769ce76dSAlexander Graf 104769ce76dSAlexander Graf typedef struct BDRVCURLState { 105769ce76dSAlexander Graf CURLM *multi; 106031fd1beSPeter Maydell QEMUTimer timer; 107769ce76dSAlexander Graf size_t len; 108769ce76dSAlexander Graf CURLState states[CURL_NUM_STATES]; 109769ce76dSAlexander Graf char *url; 110c76f4952SNolan size_t readahead_size; 111*97a3ea57SMatthew Booth bool sslverify; 1123494d650SFam Zheng bool accept_range; 113769ce76dSAlexander Graf } BDRVCURLState; 114769ce76dSAlexander Graf 115769ce76dSAlexander Graf static void curl_clean_state(CURLState *s); 116769ce76dSAlexander Graf static void curl_multi_do(void *arg); 117838ef602SMatthew Booth static void curl_multi_read(void *arg); 118769ce76dSAlexander Graf 119031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 120031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) 121031fd1beSPeter Maydell { 122031fd1beSPeter Maydell BDRVCURLState *s = opaque; 123031fd1beSPeter Maydell 124031fd1beSPeter Maydell DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms); 125031fd1beSPeter Maydell if (timeout_ms == -1) { 126031fd1beSPeter Maydell timer_del(&s->timer); 127031fd1beSPeter Maydell } else { 128031fd1beSPeter Maydell int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; 129031fd1beSPeter Maydell timer_mod(&s->timer, 130031fd1beSPeter Maydell qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); 131031fd1beSPeter Maydell } 132031fd1beSPeter Maydell return 0; 133031fd1beSPeter Maydell } 134031fd1beSPeter Maydell #endif 135031fd1beSPeter Maydell 136769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, 137769ce76dSAlexander Graf void *s, void *sp) 138769ce76dSAlexander Graf { 139838ef602SMatthew Booth CURLState *state = NULL; 140838ef602SMatthew Booth curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); 141838ef602SMatthew Booth state->sock_fd = fd; 142838ef602SMatthew Booth 143d0f2c4c6Smalc DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd); 144769ce76dSAlexander Graf switch (action) { 145769ce76dSAlexander Graf case CURL_POLL_IN: 146838ef602SMatthew Booth qemu_aio_set_fd_handler(fd, curl_multi_read, NULL, state); 147769ce76dSAlexander Graf break; 148769ce76dSAlexander Graf case CURL_POLL_OUT: 149838ef602SMatthew Booth qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, state); 150769ce76dSAlexander Graf break; 151769ce76dSAlexander Graf case CURL_POLL_INOUT: 152838ef602SMatthew Booth qemu_aio_set_fd_handler(fd, curl_multi_read, curl_multi_do, state); 153769ce76dSAlexander Graf break; 154769ce76dSAlexander Graf case CURL_POLL_REMOVE: 155f2e5dca4SStefan Hajnoczi qemu_aio_set_fd_handler(fd, NULL, NULL, NULL); 156769ce76dSAlexander Graf break; 157769ce76dSAlexander Graf } 158769ce76dSAlexander Graf 159769ce76dSAlexander Graf return 0; 160769ce76dSAlexander Graf } 161769ce76dSAlexander Graf 1623494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 163769ce76dSAlexander Graf { 1643494d650SFam Zheng BDRVCURLState *s = opaque; 165769ce76dSAlexander Graf size_t realsize = size * nmemb; 1663494d650SFam Zheng const char *accept_line = "Accept-Ranges: bytes"; 167769ce76dSAlexander Graf 1683494d650SFam Zheng if (realsize >= strlen(accept_line) 1693494d650SFam Zheng && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) { 1703494d650SFam Zheng s->accept_range = true; 1710bfcd599SBlue Swirl } 172769ce76dSAlexander Graf 173769ce76dSAlexander Graf return realsize; 174769ce76dSAlexander Graf } 175769ce76dSAlexander Graf 176769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 177769ce76dSAlexander Graf { 178769ce76dSAlexander Graf CURLState *s = ((CURLState*)opaque); 179769ce76dSAlexander Graf size_t realsize = size * nmemb; 180769ce76dSAlexander Graf int i; 181769ce76dSAlexander Graf 1820bfcd599SBlue Swirl DPRINTF("CURL: Just reading %zd bytes\n", realsize); 183769ce76dSAlexander Graf 184769ce76dSAlexander Graf if (!s || !s->orig_buf) 18538bbc0a5SMatthew Booth return 0; 186769ce76dSAlexander Graf 1876d4b9e55SFam Zheng if (s->buf_off >= s->buf_len) { 1886d4b9e55SFam Zheng /* buffer full, read nothing */ 1896d4b9e55SFam Zheng return 0; 1906d4b9e55SFam Zheng } 1916d4b9e55SFam Zheng realsize = MIN(realsize, s->buf_len - s->buf_off); 192769ce76dSAlexander Graf memcpy(s->orig_buf + s->buf_off, ptr, realsize); 193769ce76dSAlexander Graf s->buf_off += realsize; 194769ce76dSAlexander Graf 195769ce76dSAlexander Graf for(i=0; i<CURL_NUM_ACB; i++) { 196769ce76dSAlexander Graf CURLAIOCB *acb = s->acb[i]; 197769ce76dSAlexander Graf 198769ce76dSAlexander Graf if (!acb) 199769ce76dSAlexander Graf continue; 200769ce76dSAlexander Graf 201769ce76dSAlexander Graf if ((s->buf_off >= acb->end)) { 20203396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start, 203769ce76dSAlexander Graf acb->end - acb->start); 204769ce76dSAlexander Graf acb->common.cb(acb->common.opaque, 0); 205769ce76dSAlexander Graf qemu_aio_release(acb); 206769ce76dSAlexander Graf s->acb[i] = NULL; 207769ce76dSAlexander Graf } 208769ce76dSAlexander Graf } 209769ce76dSAlexander Graf 210769ce76dSAlexander Graf return realsize; 211769ce76dSAlexander Graf } 212769ce76dSAlexander Graf 213769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len, 214769ce76dSAlexander Graf CURLAIOCB *acb) 215769ce76dSAlexander Graf { 216769ce76dSAlexander Graf int i; 217769ce76dSAlexander Graf size_t end = start + len; 218769ce76dSAlexander Graf 219769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 220769ce76dSAlexander Graf CURLState *state = &s->states[i]; 221769ce76dSAlexander Graf size_t buf_end = (state->buf_start + state->buf_off); 222769ce76dSAlexander Graf size_t buf_fend = (state->buf_start + state->buf_len); 223769ce76dSAlexander Graf 224769ce76dSAlexander Graf if (!state->orig_buf) 225769ce76dSAlexander Graf continue; 226769ce76dSAlexander Graf if (!state->buf_off) 227769ce76dSAlexander Graf continue; 228769ce76dSAlexander Graf 229769ce76dSAlexander Graf // Does the existing buffer cover our section? 230769ce76dSAlexander Graf if ((start >= state->buf_start) && 231769ce76dSAlexander Graf (start <= buf_end) && 232769ce76dSAlexander Graf (end >= state->buf_start) && 233769ce76dSAlexander Graf (end <= buf_end)) 234769ce76dSAlexander Graf { 235769ce76dSAlexander Graf char *buf = state->orig_buf + (start - state->buf_start); 236769ce76dSAlexander Graf 23703396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, buf, len); 238769ce76dSAlexander Graf acb->common.cb(acb->common.opaque, 0); 239769ce76dSAlexander Graf 240769ce76dSAlexander Graf return FIND_RET_OK; 241769ce76dSAlexander Graf } 242769ce76dSAlexander Graf 243769ce76dSAlexander Graf // Wait for unfinished chunks 244b7079df4SMatthew Booth if (state->in_use && 245b7079df4SMatthew Booth (start >= state->buf_start) && 246769ce76dSAlexander Graf (start <= buf_fend) && 247769ce76dSAlexander Graf (end >= state->buf_start) && 248769ce76dSAlexander Graf (end <= buf_fend)) 249769ce76dSAlexander Graf { 250769ce76dSAlexander Graf int j; 251769ce76dSAlexander Graf 252769ce76dSAlexander Graf acb->start = start - state->buf_start; 253769ce76dSAlexander Graf acb->end = acb->start + len; 254769ce76dSAlexander Graf 255769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) { 256769ce76dSAlexander Graf if (!state->acb[j]) { 257769ce76dSAlexander Graf state->acb[j] = acb; 258769ce76dSAlexander Graf return FIND_RET_WAIT; 259769ce76dSAlexander Graf } 260769ce76dSAlexander Graf } 261769ce76dSAlexander Graf } 262769ce76dSAlexander Graf } 263769ce76dSAlexander Graf 264769ce76dSAlexander Graf return FIND_RET_NONE; 265769ce76dSAlexander Graf } 266769ce76dSAlexander Graf 267838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s) 268769ce76dSAlexander Graf { 269769ce76dSAlexander Graf int msgs_in_queue; 270769ce76dSAlexander Graf 271769ce76dSAlexander Graf /* Try to find done transfers, so we can free the easy 272769ce76dSAlexander Graf * handle again. */ 2731f2cead3SMatthew Booth for (;;) { 274769ce76dSAlexander Graf CURLMsg *msg; 275769ce76dSAlexander Graf msg = curl_multi_info_read(s->multi, &msgs_in_queue); 276769ce76dSAlexander Graf 2771f2cead3SMatthew Booth /* Quit when there are no more completions */ 278769ce76dSAlexander Graf if (!msg) 279769ce76dSAlexander Graf break; 280769ce76dSAlexander Graf 2811f2cead3SMatthew Booth if (msg->msg == CURLMSG_DONE) { 282769ce76dSAlexander Graf CURLState *state = NULL; 283f6246509SMatthew Booth curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, 284f6246509SMatthew Booth (char **)&state); 285f785a5aeSNicholas Thomas 286f785a5aeSNicholas Thomas /* ACBs for successful messages get completed in curl_read_cb */ 287f785a5aeSNicholas Thomas if (msg->data.result != CURLE_OK) { 288f785a5aeSNicholas Thomas int i; 289f785a5aeSNicholas Thomas for (i = 0; i < CURL_NUM_ACB; i++) { 290f785a5aeSNicholas Thomas CURLAIOCB *acb = state->acb[i]; 291f785a5aeSNicholas Thomas 292f785a5aeSNicholas Thomas if (acb == NULL) { 293f785a5aeSNicholas Thomas continue; 294f785a5aeSNicholas Thomas } 295f785a5aeSNicholas Thomas 296f785a5aeSNicholas Thomas acb->common.cb(acb->common.opaque, -EIO); 297f785a5aeSNicholas Thomas qemu_aio_release(acb); 298f785a5aeSNicholas Thomas state->acb[i] = NULL; 299f785a5aeSNicholas Thomas } 300f785a5aeSNicholas Thomas } 301f785a5aeSNicholas Thomas 302769ce76dSAlexander Graf curl_clean_state(state); 303769ce76dSAlexander Graf break; 304769ce76dSAlexander Graf } 305769ce76dSAlexander Graf } 306769ce76dSAlexander Graf } 307769ce76dSAlexander Graf 308031fd1beSPeter Maydell static void curl_multi_do(void *arg) 309031fd1beSPeter Maydell { 310838ef602SMatthew Booth CURLState *s = (CURLState *)arg; 311031fd1beSPeter Maydell int running; 312031fd1beSPeter Maydell int r; 313031fd1beSPeter Maydell 314838ef602SMatthew Booth if (!s->s->multi) { 315031fd1beSPeter Maydell return; 316031fd1beSPeter Maydell } 317031fd1beSPeter Maydell 318031fd1beSPeter Maydell do { 319838ef602SMatthew Booth r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running); 320031fd1beSPeter Maydell } while(r == CURLM_CALL_MULTI_PERFORM); 321031fd1beSPeter Maydell 322838ef602SMatthew Booth } 323838ef602SMatthew Booth 324838ef602SMatthew Booth static void curl_multi_read(void *arg) 325838ef602SMatthew Booth { 326838ef602SMatthew Booth CURLState *s = (CURLState *)arg; 327838ef602SMatthew Booth 328838ef602SMatthew Booth curl_multi_do(arg); 329838ef602SMatthew Booth curl_multi_check_completion(s->s); 330031fd1beSPeter Maydell } 331031fd1beSPeter Maydell 332031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg) 333031fd1beSPeter Maydell { 334031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 335031fd1beSPeter Maydell BDRVCURLState *s = (BDRVCURLState *)arg; 336031fd1beSPeter Maydell int running; 337031fd1beSPeter Maydell 338031fd1beSPeter Maydell if (!s->multi) { 339031fd1beSPeter Maydell return; 340031fd1beSPeter Maydell } 341031fd1beSPeter Maydell 342031fd1beSPeter Maydell curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 343031fd1beSPeter Maydell 344838ef602SMatthew Booth curl_multi_check_completion(s); 345031fd1beSPeter Maydell #else 346031fd1beSPeter Maydell abort(); 347031fd1beSPeter Maydell #endif 348031fd1beSPeter Maydell } 349031fd1beSPeter Maydell 350769ce76dSAlexander Graf static CURLState *curl_init_state(BDRVCURLState *s) 351769ce76dSAlexander Graf { 352769ce76dSAlexander Graf CURLState *state = NULL; 353769ce76dSAlexander Graf int i, j; 354769ce76dSAlexander Graf 355769ce76dSAlexander Graf do { 356769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 357769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) 358769ce76dSAlexander Graf if (s->states[i].acb[j]) 359769ce76dSAlexander Graf continue; 360769ce76dSAlexander Graf if (s->states[i].in_use) 361769ce76dSAlexander Graf continue; 362769ce76dSAlexander Graf 363769ce76dSAlexander Graf state = &s->states[i]; 364769ce76dSAlexander Graf state->in_use = 1; 365769ce76dSAlexander Graf break; 366769ce76dSAlexander Graf } 367769ce76dSAlexander Graf if (!state) { 368e4661837SMatthew Booth qemu_aio_wait(); 369769ce76dSAlexander Graf } 370769ce76dSAlexander Graf } while(!state); 371769ce76dSAlexander Graf 3729e550b32SMatthew Booth if (!state->curl) { 373769ce76dSAlexander Graf state->curl = curl_easy_init(); 3749e550b32SMatthew Booth if (!state->curl) { 375769ce76dSAlexander Graf return NULL; 3769e550b32SMatthew Booth } 377769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_URL, s->url); 378*97a3ea57SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, 379*97a3ea57SMatthew Booth (long) s->sslverify); 380769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5); 3819e550b32SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, 3829e550b32SMatthew Booth (void *)curl_read_cb); 383769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); 384769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); 385769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); 386769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); 387769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); 388769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); 389f785a5aeSNicholas Thomas curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); 390769ce76dSAlexander Graf 391fb6d1bbdSStefan Hajnoczi /* Restrict supported protocols to avoid security issues in the more 392fb6d1bbdSStefan Hajnoczi * obscure protocols. For example, do not allow POP3/SMTP/IMAP see 393fb6d1bbdSStefan Hajnoczi * CVE-2013-0249. 3948a8f5840SStefan Hajnoczi * 3958a8f5840SStefan Hajnoczi * Restricting protocols is only supported from 7.19.4 upwards. 396fb6d1bbdSStefan Hajnoczi */ 3978a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304 398fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); 399fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); 4008a8f5840SStefan Hajnoczi #endif 401fb6d1bbdSStefan Hajnoczi 402769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE 403769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); 404769ce76dSAlexander Graf #endif 4059e550b32SMatthew Booth } 406769ce76dSAlexander Graf 407769ce76dSAlexander Graf state->s = s; 408769ce76dSAlexander Graf 409769ce76dSAlexander Graf return state; 410769ce76dSAlexander Graf } 411769ce76dSAlexander Graf 412769ce76dSAlexander Graf static void curl_clean_state(CURLState *s) 413769ce76dSAlexander Graf { 414769ce76dSAlexander Graf if (s->s->multi) 415769ce76dSAlexander Graf curl_multi_remove_handle(s->s->multi, s->curl); 416769ce76dSAlexander Graf s->in_use = 0; 417769ce76dSAlexander Graf } 418769ce76dSAlexander Graf 4198e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options, 4208e6d58cdSKevin Wolf Error **errp) 421769ce76dSAlexander Graf { 422e3542c67SMatthew Booth qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename)); 4238e6d58cdSKevin Wolf } 4248e6d58cdSKevin Wolf 4258e6d58cdSKevin Wolf static QemuOptsList runtime_opts = { 4268e6d58cdSKevin Wolf .name = "curl", 4278e6d58cdSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 4288e6d58cdSKevin Wolf .desc = { 4298e6d58cdSKevin Wolf { 430e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_URL, 4318e6d58cdSKevin Wolf .type = QEMU_OPT_STRING, 4328e6d58cdSKevin Wolf .help = "URL to open", 4338e6d58cdSKevin Wolf }, 4348e6d58cdSKevin Wolf { 435e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_READAHEAD, 4368e6d58cdSKevin Wolf .type = QEMU_OPT_SIZE, 4378e6d58cdSKevin Wolf .help = "Readahead size", 4388e6d58cdSKevin Wolf }, 439*97a3ea57SMatthew Booth { 440*97a3ea57SMatthew Booth .name = CURL_BLOCK_OPT_SSLVERIFY, 441*97a3ea57SMatthew Booth .type = QEMU_OPT_BOOL, 442*97a3ea57SMatthew Booth .help = "Verify SSL certificate" 443*97a3ea57SMatthew Booth }, 4448e6d58cdSKevin Wolf { /* end of list */ } 4458e6d58cdSKevin Wolf }, 4468e6d58cdSKevin Wolf }; 4478e6d58cdSKevin Wolf 448015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags, 449015a1036SMax Reitz Error **errp) 4508e6d58cdSKevin Wolf { 4518e6d58cdSKevin Wolf BDRVCURLState *s = bs->opaque; 4528e6d58cdSKevin Wolf CURLState *state = NULL; 4538e6d58cdSKevin Wolf QemuOpts *opts; 4548e6d58cdSKevin Wolf Error *local_err = NULL; 4558e6d58cdSKevin Wolf const char *file; 4568e6d58cdSKevin Wolf double d; 4578e6d58cdSKevin Wolf 4588e6d58cdSKevin Wolf static int inited = 0; 4598e6d58cdSKevin Wolf 460a7cea2baSRichard W.M. Jones if (flags & BDRV_O_RDWR) { 4612a94fee3SPaolo Bonzini error_setg(errp, "curl block device does not support writes"); 462a7cea2baSRichard W.M. Jones return -EROFS; 463a7cea2baSRichard W.M. Jones } 464a7cea2baSRichard W.M. Jones 46587ea75d5SPeter Crosthwaite opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 4668e6d58cdSKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 46784d18f06SMarkus Armbruster if (local_err) { 4682a94fee3SPaolo Bonzini error_propagate(errp, local_err); 4698e6d58cdSKevin Wolf goto out_noclean; 4708e6d58cdSKevin Wolf } 4718e6d58cdSKevin Wolf 472e3542c67SMatthew Booth s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, 473e3542c67SMatthew Booth READ_AHEAD_DEFAULT); 474c76f4952SNolan if ((s->readahead_size & 0x1ff) != 0) { 4752a94fee3SPaolo Bonzini error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", 476c76f4952SNolan s->readahead_size); 477c76f4952SNolan goto out_noclean; 478c76f4952SNolan } 479c76f4952SNolan 480*97a3ea57SMatthew Booth s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true); 481*97a3ea57SMatthew Booth 482e3542c67SMatthew Booth file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); 4838e6d58cdSKevin Wolf if (file == NULL) { 4842a94fee3SPaolo Bonzini error_setg(errp, "curl block driver requires an 'url' option"); 4858e6d58cdSKevin Wolf goto out_noclean; 4868e6d58cdSKevin Wolf } 4878e6d58cdSKevin Wolf 488769ce76dSAlexander Graf if (!inited) { 489769ce76dSAlexander Graf curl_global_init(CURL_GLOBAL_ALL); 490769ce76dSAlexander Graf inited = 1; 491769ce76dSAlexander Graf } 492769ce76dSAlexander Graf 493d0f2c4c6Smalc DPRINTF("CURL: Opening %s\n", file); 4948e6d58cdSKevin Wolf s->url = g_strdup(file); 495769ce76dSAlexander Graf state = curl_init_state(s); 496769ce76dSAlexander Graf if (!state) 497769ce76dSAlexander Graf goto out_noclean; 498769ce76dSAlexander Graf 499769ce76dSAlexander Graf // Get file size 500769ce76dSAlexander Graf 5013494d650SFam Zheng s->accept_range = false; 502769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); 5033494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, 5043494d650SFam Zheng curl_header_cb); 5053494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); 506769ce76dSAlexander Graf if (curl_easy_perform(state->curl)) 507769ce76dSAlexander Graf goto out; 508769ce76dSAlexander Graf curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d); 509769ce76dSAlexander Graf if (d) 510769ce76dSAlexander Graf s->len = (size_t)d; 511769ce76dSAlexander Graf else if(!s->len) 512769ce76dSAlexander Graf goto out; 5133494d650SFam Zheng if ((!strncasecmp(s->url, "http://", strlen("http://")) 5143494d650SFam Zheng || !strncasecmp(s->url, "https://", strlen("https://"))) 5153494d650SFam Zheng && !s->accept_range) { 5163494d650SFam Zheng pstrcpy(state->errmsg, CURL_ERROR_SIZE, 5173494d650SFam Zheng "Server does not support 'range' (byte ranges)."); 5183494d650SFam Zheng goto out; 5193494d650SFam Zheng } 5200bfcd599SBlue Swirl DPRINTF("CURL: Size = %zd\n", s->len); 521769ce76dSAlexander Graf 522769ce76dSAlexander Graf curl_clean_state(state); 523769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 524769ce76dSAlexander Graf state->curl = NULL; 525769ce76dSAlexander Graf 526031fd1beSPeter Maydell aio_timer_init(bdrv_get_aio_context(bs), &s->timer, 527031fd1beSPeter Maydell QEMU_CLOCK_REALTIME, SCALE_NS, 528031fd1beSPeter Maydell curl_multi_timeout_do, s); 529031fd1beSPeter Maydell 530769ce76dSAlexander Graf // Now we know the file exists and its size, so let's 531769ce76dSAlexander Graf // initialize the multi interface! 532769ce76dSAlexander Graf 533769ce76dSAlexander Graf s->multi = curl_multi_init(); 534769ce76dSAlexander Graf curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); 535031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 536031fd1beSPeter Maydell curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); 537031fd1beSPeter Maydell curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); 538031fd1beSPeter Maydell #endif 539769ce76dSAlexander Graf 5408e6d58cdSKevin Wolf qemu_opts_del(opts); 541769ce76dSAlexander Graf return 0; 542769ce76dSAlexander Graf 543769ce76dSAlexander Graf out: 544acd7fdc6SMaria Kustova error_setg(errp, "CURL: Error opening file: %s", state->errmsg); 545769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 546769ce76dSAlexander Graf state->curl = NULL; 547769ce76dSAlexander Graf out_noclean: 5488e6d58cdSKevin Wolf g_free(s->url); 5498e6d58cdSKevin Wolf qemu_opts_del(opts); 550769ce76dSAlexander Graf return -EINVAL; 551769ce76dSAlexander Graf } 552769ce76dSAlexander Graf 553c16b5a2cSChristoph Hellwig static void curl_aio_cancel(BlockDriverAIOCB *blockacb) 554c16b5a2cSChristoph Hellwig { 555c16b5a2cSChristoph Hellwig // Do we have to implement canceling? Seems to work without... 556c16b5a2cSChristoph Hellwig } 557c16b5a2cSChristoph Hellwig 558d7331bedSStefan Hajnoczi static const AIOCBInfo curl_aiocb_info = { 559c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(CURLAIOCB), 560c16b5a2cSChristoph Hellwig .cancel = curl_aio_cancel, 561c16b5a2cSChristoph Hellwig }; 562c16b5a2cSChristoph Hellwig 563363c3c85SNick Thomas 564363c3c85SNick Thomas static void curl_readv_bh_cb(void *p) 565769ce76dSAlexander Graf { 566769ce76dSAlexander Graf CURLState *state; 567b69cdef8SMatthew Booth int running; 568769ce76dSAlexander Graf 569363c3c85SNick Thomas CURLAIOCB *acb = p; 570363c3c85SNick Thomas BDRVCURLState *s = acb->common.bs->opaque; 571769ce76dSAlexander Graf 572363c3c85SNick Thomas qemu_bh_delete(acb->bh); 573363c3c85SNick Thomas acb->bh = NULL; 574363c3c85SNick Thomas 575363c3c85SNick Thomas size_t start = acb->sector_num * SECTOR_SIZE; 576363c3c85SNick Thomas size_t end; 577769ce76dSAlexander Graf 578769ce76dSAlexander Graf // In case we have the requested data already (e.g. read-ahead), 579769ce76dSAlexander Graf // we can just call the callback and be done. 580363c3c85SNick Thomas switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) { 581769ce76dSAlexander Graf case FIND_RET_OK: 582769ce76dSAlexander Graf qemu_aio_release(acb); 583769ce76dSAlexander Graf // fall through 584769ce76dSAlexander Graf case FIND_RET_WAIT: 585363c3c85SNick Thomas return; 586769ce76dSAlexander Graf default: 587769ce76dSAlexander Graf break; 588769ce76dSAlexander Graf } 589769ce76dSAlexander Graf 590769ce76dSAlexander Graf // No cache found, so let's start a new request 591769ce76dSAlexander Graf state = curl_init_state(s); 592363c3c85SNick Thomas if (!state) { 593363c3c85SNick Thomas acb->common.cb(acb->common.opaque, -EIO); 594363c3c85SNick Thomas qemu_aio_release(acb); 595363c3c85SNick Thomas return; 596363c3c85SNick Thomas } 597769ce76dSAlexander Graf 598769ce76dSAlexander Graf acb->start = 0; 599363c3c85SNick Thomas acb->end = (acb->nb_sectors * SECTOR_SIZE); 600769ce76dSAlexander Graf 601769ce76dSAlexander Graf state->buf_off = 0; 602769ce76dSAlexander Graf if (state->orig_buf) 6037267c094SAnthony Liguori g_free(state->orig_buf); 604769ce76dSAlexander Graf state->buf_start = start; 605c76f4952SNolan state->buf_len = acb->end + s->readahead_size; 606769ce76dSAlexander Graf end = MIN(start + state->buf_len, s->len) - 1; 6077267c094SAnthony Liguori state->orig_buf = g_malloc(state->buf_len); 608769ce76dSAlexander Graf state->acb[0] = acb; 609769ce76dSAlexander Graf 6100bfcd599SBlue Swirl snprintf(state->range, 127, "%zd-%zd", start, end); 6110bfcd599SBlue Swirl DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n", 612363c3c85SNick Thomas (acb->nb_sectors * SECTOR_SIZE), start, state->range); 613769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); 614769ce76dSAlexander Graf 615769ce76dSAlexander Graf curl_multi_add_handle(s->multi, state->curl); 616769ce76dSAlexander Graf 617b69cdef8SMatthew Booth /* Tell curl it needs to kick things off */ 618b69cdef8SMatthew Booth curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 619363c3c85SNick Thomas } 620363c3c85SNick Thomas 621363c3c85SNick Thomas static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs, 622363c3c85SNick Thomas int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 623363c3c85SNick Thomas BlockDriverCompletionFunc *cb, void *opaque) 624363c3c85SNick Thomas { 625363c3c85SNick Thomas CURLAIOCB *acb; 626363c3c85SNick Thomas 627d7331bedSStefan Hajnoczi acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque); 628363c3c85SNick Thomas 629363c3c85SNick Thomas acb->qiov = qiov; 630363c3c85SNick Thomas acb->sector_num = sector_num; 631363c3c85SNick Thomas acb->nb_sectors = nb_sectors; 632363c3c85SNick Thomas 633363c3c85SNick Thomas acb->bh = qemu_bh_new(curl_readv_bh_cb, acb); 634363c3c85SNick Thomas qemu_bh_schedule(acb->bh); 635769ce76dSAlexander Graf return &acb->common; 636769ce76dSAlexander Graf } 637769ce76dSAlexander Graf 638769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs) 639769ce76dSAlexander Graf { 640769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 641769ce76dSAlexander Graf int i; 642769ce76dSAlexander Graf 643d0f2c4c6Smalc DPRINTF("CURL: Close\n"); 644769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 645769ce76dSAlexander Graf if (s->states[i].in_use) 646769ce76dSAlexander Graf curl_clean_state(&s->states[i]); 647769ce76dSAlexander Graf if (s->states[i].curl) { 648769ce76dSAlexander Graf curl_easy_cleanup(s->states[i].curl); 649769ce76dSAlexander Graf s->states[i].curl = NULL; 650769ce76dSAlexander Graf } 651769ce76dSAlexander Graf if (s->states[i].orig_buf) { 6527267c094SAnthony Liguori g_free(s->states[i].orig_buf); 653769ce76dSAlexander Graf s->states[i].orig_buf = NULL; 654769ce76dSAlexander Graf } 655769ce76dSAlexander Graf } 656769ce76dSAlexander Graf if (s->multi) 657769ce76dSAlexander Graf curl_multi_cleanup(s->multi); 658031fd1beSPeter Maydell 659031fd1beSPeter Maydell timer_del(&s->timer); 660031fd1beSPeter Maydell 66145724d6dSStefan Weil g_free(s->url); 662769ce76dSAlexander Graf } 663769ce76dSAlexander Graf 664769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs) 665769ce76dSAlexander Graf { 666769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 667769ce76dSAlexander Graf return s->len; 668769ce76dSAlexander Graf } 669769ce76dSAlexander Graf 670769ce76dSAlexander Graf static BlockDriver bdrv_http = { 671769ce76dSAlexander Graf .format_name = "http", 672769ce76dSAlexander Graf .protocol_name = "http", 673769ce76dSAlexander Graf 674769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 6758e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 67666f82ceeSKevin Wolf .bdrv_file_open = curl_open, 677769ce76dSAlexander Graf .bdrv_close = curl_close, 678769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 679769ce76dSAlexander Graf 680769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 681769ce76dSAlexander Graf }; 682769ce76dSAlexander Graf 683769ce76dSAlexander Graf static BlockDriver bdrv_https = { 684769ce76dSAlexander Graf .format_name = "https", 685769ce76dSAlexander Graf .protocol_name = "https", 686769ce76dSAlexander Graf 687769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 6888e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 68966f82ceeSKevin Wolf .bdrv_file_open = curl_open, 690769ce76dSAlexander Graf .bdrv_close = curl_close, 691769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 692769ce76dSAlexander Graf 693769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 694769ce76dSAlexander Graf }; 695769ce76dSAlexander Graf 696769ce76dSAlexander Graf static BlockDriver bdrv_ftp = { 697769ce76dSAlexander Graf .format_name = "ftp", 698769ce76dSAlexander Graf .protocol_name = "ftp", 699769ce76dSAlexander Graf 700769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 7018e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 70266f82ceeSKevin Wolf .bdrv_file_open = curl_open, 703769ce76dSAlexander Graf .bdrv_close = curl_close, 704769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 705769ce76dSAlexander Graf 706769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 707769ce76dSAlexander Graf }; 708769ce76dSAlexander Graf 709769ce76dSAlexander Graf static BlockDriver bdrv_ftps = { 710769ce76dSAlexander Graf .format_name = "ftps", 711769ce76dSAlexander Graf .protocol_name = "ftps", 712769ce76dSAlexander Graf 713769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 7148e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 71566f82ceeSKevin Wolf .bdrv_file_open = curl_open, 716769ce76dSAlexander Graf .bdrv_close = curl_close, 717769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 718769ce76dSAlexander Graf 719769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 720769ce76dSAlexander Graf }; 721769ce76dSAlexander Graf 722769ce76dSAlexander Graf static BlockDriver bdrv_tftp = { 723769ce76dSAlexander Graf .format_name = "tftp", 724769ce76dSAlexander Graf .protocol_name = "tftp", 725769ce76dSAlexander Graf 726769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 7278e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 72866f82ceeSKevin Wolf .bdrv_file_open = curl_open, 729769ce76dSAlexander Graf .bdrv_close = curl_close, 730769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 731769ce76dSAlexander Graf 732769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 733769ce76dSAlexander Graf }; 734769ce76dSAlexander Graf 735769ce76dSAlexander Graf static void curl_block_init(void) 736769ce76dSAlexander Graf { 737769ce76dSAlexander Graf bdrv_register(&bdrv_http); 738769ce76dSAlexander Graf bdrv_register(&bdrv_https); 739769ce76dSAlexander Graf bdrv_register(&bdrv_ftp); 740769ce76dSAlexander Graf bdrv_register(&bdrv_ftps); 741769ce76dSAlexander Graf bdrv_register(&bdrv_tftp); 742769ce76dSAlexander Graf } 743769ce76dSAlexander Graf 744769ce76dSAlexander Graf block_init(curl_block_init); 745