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 */ 2480c71a24SPeter Maydell #include "qemu/osdep.h" 25da34e65cSMarkus Armbruster #include "qapi/error.h" 26769ce76dSAlexander Graf #include "qemu-common.h" 27796a060bSRichard W.M. Jones #include "qemu/error-report.h" 28737e150eSPaolo Bonzini #include "block/block_int.h" 2997a3ea57SMatthew Booth #include "qapi/qmp/qbool.h" 30d49b6836SMarkus Armbruster #include "qapi/qmp/qstring.h" 311bff9606SDaniel P. Berrange #include "crypto/secret.h" 32769ce76dSAlexander Graf #include <curl/curl.h> 33f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 34769ce76dSAlexander Graf 3541c23467SRichard W.M. Jones // #define DEBUG_CURL 36769ce76dSAlexander Graf // #define DEBUG_VERBOSE 37769ce76dSAlexander Graf 38769ce76dSAlexander Graf #ifdef DEBUG_CURL 39ed79f37dSZhou Jie #define DEBUG_CURL_PRINT 1 40769ce76dSAlexander Graf #else 41ed79f37dSZhou Jie #define DEBUG_CURL_PRINT 0 42769ce76dSAlexander Graf #endif 43ed79f37dSZhou Jie #define DPRINTF(fmt, ...) \ 44ed79f37dSZhou Jie do { \ 45ed79f37dSZhou Jie if (DEBUG_CURL_PRINT) { \ 46ed79f37dSZhou Jie fprintf(stderr, fmt, ## __VA_ARGS__); \ 47ed79f37dSZhou Jie } \ 48ed79f37dSZhou Jie } while (0) 49769ce76dSAlexander Graf 50031fd1beSPeter Maydell #if LIBCURL_VERSION_NUM >= 0x071000 51031fd1beSPeter Maydell /* The multi interface timer callback was introduced in 7.16.0 */ 52031fd1beSPeter Maydell #define NEED_CURL_TIMER_CALLBACK 539aedd5a5SMatthew Booth #define HAVE_SOCKET_ACTION 549aedd5a5SMatthew Booth #endif 559aedd5a5SMatthew Booth 569aedd5a5SMatthew Booth #ifndef HAVE_SOCKET_ACTION 579aedd5a5SMatthew Booth /* If curl_multi_socket_action isn't available, define it statically here in 589aedd5a5SMatthew Booth * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is 599aedd5a5SMatthew Booth * less efficient but still safe. */ 609aedd5a5SMatthew Booth static CURLMcode __curl_multi_socket_action(CURLM *multi_handle, 619aedd5a5SMatthew Booth curl_socket_t sockfd, 629aedd5a5SMatthew Booth int ev_bitmask, 639aedd5a5SMatthew Booth int *running_handles) 649aedd5a5SMatthew Booth { 659aedd5a5SMatthew Booth return curl_multi_socket(multi_handle, sockfd, running_handles); 669aedd5a5SMatthew Booth } 679aedd5a5SMatthew Booth #define curl_multi_socket_action __curl_multi_socket_action 68031fd1beSPeter Maydell #endif 69031fd1beSPeter Maydell 70fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ 71fb6d1bbdSStefan Hajnoczi CURLPROTO_FTP | CURLPROTO_FTPS | \ 72fb6d1bbdSStefan Hajnoczi CURLPROTO_TFTP) 73fb6d1bbdSStefan Hajnoczi 74769ce76dSAlexander Graf #define CURL_NUM_STATES 8 75769ce76dSAlexander Graf #define CURL_NUM_ACB 8 76769ce76dSAlexander Graf #define SECTOR_SIZE 512 77e3542c67SMatthew Booth #define READ_AHEAD_DEFAULT (256 * 1024) 78212aefaaSDaniel Henrique Barboza #define CURL_TIMEOUT_DEFAULT 5 79f76faedaSRichard W.M. Jones #define CURL_TIMEOUT_MAX 10000 80769ce76dSAlexander Graf 81769ce76dSAlexander Graf #define FIND_RET_NONE 0 82769ce76dSAlexander Graf #define FIND_RET_OK 1 83769ce76dSAlexander Graf #define FIND_RET_WAIT 2 84769ce76dSAlexander Graf 85e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL "url" 86e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead" 8797a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" 88212aefaaSDaniel Henrique Barboza #define CURL_BLOCK_OPT_TIMEOUT "timeout" 89a94f83d9SRichard W.M. Jones #define CURL_BLOCK_OPT_COOKIE "cookie" 901bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_USERNAME "username" 911bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret" 921bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username" 931bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret" 94e3542c67SMatthew Booth 95769ce76dSAlexander Graf struct BDRVCURLState; 96769ce76dSAlexander Graf 97769ce76dSAlexander Graf typedef struct CURLAIOCB { 987c84b1b8SMarkus Armbruster BlockAIOCB common; 99363c3c85SNick Thomas QEMUBH *bh; 100769ce76dSAlexander Graf QEMUIOVector *qiov; 101363c3c85SNick Thomas 102363c3c85SNick Thomas int64_t sector_num; 103363c3c85SNick Thomas int nb_sectors; 104363c3c85SNick Thomas 105769ce76dSAlexander Graf size_t start; 106769ce76dSAlexander Graf size_t end; 107769ce76dSAlexander Graf } CURLAIOCB; 108769ce76dSAlexander Graf 109769ce76dSAlexander Graf typedef struct CURLState 110769ce76dSAlexander Graf { 111769ce76dSAlexander Graf struct BDRVCURLState *s; 112769ce76dSAlexander Graf CURLAIOCB *acb[CURL_NUM_ACB]; 113769ce76dSAlexander Graf CURL *curl; 114838ef602SMatthew Booth curl_socket_t sock_fd; 115769ce76dSAlexander Graf char *orig_buf; 116769ce76dSAlexander Graf size_t buf_start; 117769ce76dSAlexander Graf size_t buf_off; 118769ce76dSAlexander Graf size_t buf_len; 119769ce76dSAlexander Graf char range[128]; 120769ce76dSAlexander Graf char errmsg[CURL_ERROR_SIZE]; 121769ce76dSAlexander Graf char in_use; 122769ce76dSAlexander Graf } CURLState; 123769ce76dSAlexander Graf 124769ce76dSAlexander Graf typedef struct BDRVCURLState { 125769ce76dSAlexander Graf CURLM *multi; 126031fd1beSPeter Maydell QEMUTimer timer; 127769ce76dSAlexander Graf size_t len; 128769ce76dSAlexander Graf CURLState states[CURL_NUM_STATES]; 129769ce76dSAlexander Graf char *url; 130c76f4952SNolan size_t readahead_size; 13197a3ea57SMatthew Booth bool sslverify; 132f76faedaSRichard W.M. Jones uint64_t timeout; 133a94f83d9SRichard W.M. Jones char *cookie; 1343494d650SFam Zheng bool accept_range; 13563f0f45fSStefan Hajnoczi AioContext *aio_context; 1361bff9606SDaniel P. Berrange char *username; 1371bff9606SDaniel P. Berrange char *password; 1381bff9606SDaniel P. Berrange char *proxyusername; 1391bff9606SDaniel P. Berrange char *proxypassword; 140769ce76dSAlexander Graf } BDRVCURLState; 141769ce76dSAlexander Graf 142769ce76dSAlexander Graf static void curl_clean_state(CURLState *s); 143769ce76dSAlexander Graf static void curl_multi_do(void *arg); 144838ef602SMatthew Booth static void curl_multi_read(void *arg); 145769ce76dSAlexander Graf 146031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 147031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) 148031fd1beSPeter Maydell { 149031fd1beSPeter Maydell BDRVCURLState *s = opaque; 150031fd1beSPeter Maydell 151031fd1beSPeter Maydell DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms); 152031fd1beSPeter Maydell if (timeout_ms == -1) { 153031fd1beSPeter Maydell timer_del(&s->timer); 154031fd1beSPeter Maydell } else { 155031fd1beSPeter Maydell int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; 156031fd1beSPeter Maydell timer_mod(&s->timer, 157031fd1beSPeter Maydell qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); 158031fd1beSPeter Maydell } 159031fd1beSPeter Maydell return 0; 160031fd1beSPeter Maydell } 161031fd1beSPeter Maydell #endif 162031fd1beSPeter Maydell 163769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, 16463f0f45fSStefan Hajnoczi void *userp, void *sp) 165769ce76dSAlexander Graf { 16663f0f45fSStefan Hajnoczi BDRVCURLState *s; 167838ef602SMatthew Booth CURLState *state = NULL; 168838ef602SMatthew Booth curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); 169838ef602SMatthew Booth state->sock_fd = fd; 17063f0f45fSStefan Hajnoczi s = state->s; 171838ef602SMatthew Booth 17292b6a160SFam Zheng DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, (int)fd); 173769ce76dSAlexander Graf switch (action) { 174769ce76dSAlexander Graf case CURL_POLL_IN: 175dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 176dca21ef2SFam Zheng curl_multi_read, NULL, state); 177769ce76dSAlexander Graf break; 178769ce76dSAlexander Graf case CURL_POLL_OUT: 179dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 180dca21ef2SFam Zheng NULL, curl_multi_do, state); 181769ce76dSAlexander Graf break; 182769ce76dSAlexander Graf case CURL_POLL_INOUT: 183dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 184dca21ef2SFam Zheng curl_multi_read, curl_multi_do, state); 185769ce76dSAlexander Graf break; 186769ce76dSAlexander Graf case CURL_POLL_REMOVE: 187dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 188dca21ef2SFam Zheng NULL, NULL, NULL); 189769ce76dSAlexander Graf break; 190769ce76dSAlexander Graf } 191769ce76dSAlexander Graf 192769ce76dSAlexander Graf return 0; 193769ce76dSAlexander Graf } 194769ce76dSAlexander Graf 1953494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 196769ce76dSAlexander Graf { 1973494d650SFam Zheng BDRVCURLState *s = opaque; 198769ce76dSAlexander Graf size_t realsize = size * nmemb; 1993494d650SFam Zheng const char *accept_line = "Accept-Ranges: bytes"; 200769ce76dSAlexander Graf 2013494d650SFam Zheng if (realsize >= strlen(accept_line) 2023494d650SFam Zheng && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) { 2033494d650SFam Zheng s->accept_range = true; 2040bfcd599SBlue Swirl } 205769ce76dSAlexander Graf 206769ce76dSAlexander Graf return realsize; 207769ce76dSAlexander Graf } 208769ce76dSAlexander Graf 209769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 210769ce76dSAlexander Graf { 211769ce76dSAlexander Graf CURLState *s = ((CURLState*)opaque); 212769ce76dSAlexander Graf size_t realsize = size * nmemb; 213769ce76dSAlexander Graf int i; 214769ce76dSAlexander Graf 2150bfcd599SBlue Swirl DPRINTF("CURL: Just reading %zd bytes\n", realsize); 216769ce76dSAlexander Graf 217769ce76dSAlexander Graf if (!s || !s->orig_buf) 21838bbc0a5SMatthew Booth return 0; 219769ce76dSAlexander Graf 2206d4b9e55SFam Zheng if (s->buf_off >= s->buf_len) { 2216d4b9e55SFam Zheng /* buffer full, read nothing */ 2226d4b9e55SFam Zheng return 0; 2236d4b9e55SFam Zheng } 2246d4b9e55SFam Zheng realsize = MIN(realsize, s->buf_len - s->buf_off); 225769ce76dSAlexander Graf memcpy(s->orig_buf + s->buf_off, ptr, realsize); 226769ce76dSAlexander Graf s->buf_off += realsize; 227769ce76dSAlexander Graf 228769ce76dSAlexander Graf for(i=0; i<CURL_NUM_ACB; i++) { 229769ce76dSAlexander Graf CURLAIOCB *acb = s->acb[i]; 230769ce76dSAlexander Graf 231769ce76dSAlexander Graf if (!acb) 232769ce76dSAlexander Graf continue; 233769ce76dSAlexander Graf 234769ce76dSAlexander Graf if ((s->buf_off >= acb->end)) { 23503396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start, 236769ce76dSAlexander Graf acb->end - acb->start); 237769ce76dSAlexander Graf acb->common.cb(acb->common.opaque, 0); 2388007429aSFam Zheng qemu_aio_unref(acb); 239769ce76dSAlexander Graf s->acb[i] = NULL; 240769ce76dSAlexander Graf } 241769ce76dSAlexander Graf } 242769ce76dSAlexander Graf 243769ce76dSAlexander Graf return realsize; 244769ce76dSAlexander Graf } 245769ce76dSAlexander Graf 246769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len, 247769ce76dSAlexander Graf CURLAIOCB *acb) 248769ce76dSAlexander Graf { 249769ce76dSAlexander Graf int i; 250769ce76dSAlexander Graf size_t end = start + len; 251769ce76dSAlexander Graf 252769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 253769ce76dSAlexander Graf CURLState *state = &s->states[i]; 254769ce76dSAlexander Graf size_t buf_end = (state->buf_start + state->buf_off); 255769ce76dSAlexander Graf size_t buf_fend = (state->buf_start + state->buf_len); 256769ce76dSAlexander Graf 257769ce76dSAlexander Graf if (!state->orig_buf) 258769ce76dSAlexander Graf continue; 259769ce76dSAlexander Graf if (!state->buf_off) 260769ce76dSAlexander Graf continue; 261769ce76dSAlexander Graf 262769ce76dSAlexander Graf // Does the existing buffer cover our section? 263769ce76dSAlexander Graf if ((start >= state->buf_start) && 264769ce76dSAlexander Graf (start <= buf_end) && 265769ce76dSAlexander Graf (end >= state->buf_start) && 266769ce76dSAlexander Graf (end <= buf_end)) 267769ce76dSAlexander Graf { 268769ce76dSAlexander Graf char *buf = state->orig_buf + (start - state->buf_start); 269769ce76dSAlexander Graf 27003396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, buf, len); 271769ce76dSAlexander Graf acb->common.cb(acb->common.opaque, 0); 272769ce76dSAlexander Graf 273769ce76dSAlexander Graf return FIND_RET_OK; 274769ce76dSAlexander Graf } 275769ce76dSAlexander Graf 276769ce76dSAlexander Graf // Wait for unfinished chunks 277b7079df4SMatthew Booth if (state->in_use && 278b7079df4SMatthew Booth (start >= state->buf_start) && 279769ce76dSAlexander Graf (start <= buf_fend) && 280769ce76dSAlexander Graf (end >= state->buf_start) && 281769ce76dSAlexander Graf (end <= buf_fend)) 282769ce76dSAlexander Graf { 283769ce76dSAlexander Graf int j; 284769ce76dSAlexander Graf 285769ce76dSAlexander Graf acb->start = start - state->buf_start; 286769ce76dSAlexander Graf acb->end = acb->start + len; 287769ce76dSAlexander Graf 288769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) { 289769ce76dSAlexander Graf if (!state->acb[j]) { 290769ce76dSAlexander Graf state->acb[j] = acb; 291769ce76dSAlexander Graf return FIND_RET_WAIT; 292769ce76dSAlexander Graf } 293769ce76dSAlexander Graf } 294769ce76dSAlexander Graf } 295769ce76dSAlexander Graf } 296769ce76dSAlexander Graf 297769ce76dSAlexander Graf return FIND_RET_NONE; 298769ce76dSAlexander Graf } 299769ce76dSAlexander Graf 300838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s) 301769ce76dSAlexander Graf { 302769ce76dSAlexander Graf int msgs_in_queue; 303769ce76dSAlexander Graf 304769ce76dSAlexander Graf /* Try to find done transfers, so we can free the easy 305769ce76dSAlexander Graf * handle again. */ 3061f2cead3SMatthew Booth for (;;) { 307769ce76dSAlexander Graf CURLMsg *msg; 308769ce76dSAlexander Graf msg = curl_multi_info_read(s->multi, &msgs_in_queue); 309769ce76dSAlexander Graf 3101f2cead3SMatthew Booth /* Quit when there are no more completions */ 311769ce76dSAlexander Graf if (!msg) 312769ce76dSAlexander Graf break; 313769ce76dSAlexander Graf 3141f2cead3SMatthew Booth if (msg->msg == CURLMSG_DONE) { 315769ce76dSAlexander Graf CURLState *state = NULL; 316f6246509SMatthew Booth curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, 317f6246509SMatthew Booth (char **)&state); 318f785a5aeSNicholas Thomas 319f785a5aeSNicholas Thomas /* ACBs for successful messages get completed in curl_read_cb */ 320f785a5aeSNicholas Thomas if (msg->data.result != CURLE_OK) { 321f785a5aeSNicholas Thomas int i; 322796a060bSRichard W.M. Jones static int errcount = 100; 323796a060bSRichard W.M. Jones 324796a060bSRichard W.M. Jones /* Don't lose the original error message from curl, since 325796a060bSRichard W.M. Jones * it contains extra data. 326796a060bSRichard W.M. Jones */ 327796a060bSRichard W.M. Jones if (errcount > 0) { 328796a060bSRichard W.M. Jones error_report("curl: %s", state->errmsg); 329796a060bSRichard W.M. Jones if (--errcount == 0) { 330796a060bSRichard W.M. Jones error_report("curl: further errors suppressed"); 331796a060bSRichard W.M. Jones } 332796a060bSRichard W.M. Jones } 333796a060bSRichard W.M. Jones 334f785a5aeSNicholas Thomas for (i = 0; i < CURL_NUM_ACB; i++) { 335f785a5aeSNicholas Thomas CURLAIOCB *acb = state->acb[i]; 336f785a5aeSNicholas Thomas 337f785a5aeSNicholas Thomas if (acb == NULL) { 338f785a5aeSNicholas Thomas continue; 339f785a5aeSNicholas Thomas } 340f785a5aeSNicholas Thomas 341796a060bSRichard W.M. Jones acb->common.cb(acb->common.opaque, -EPROTO); 3428007429aSFam Zheng qemu_aio_unref(acb); 343f785a5aeSNicholas Thomas state->acb[i] = NULL; 344f785a5aeSNicholas Thomas } 345f785a5aeSNicholas Thomas } 346f785a5aeSNicholas Thomas 347769ce76dSAlexander Graf curl_clean_state(state); 348769ce76dSAlexander Graf break; 349769ce76dSAlexander Graf } 350769ce76dSAlexander Graf } 351769ce76dSAlexander Graf } 352769ce76dSAlexander Graf 353031fd1beSPeter Maydell static void curl_multi_do(void *arg) 354031fd1beSPeter Maydell { 355838ef602SMatthew Booth CURLState *s = (CURLState *)arg; 356031fd1beSPeter Maydell int running; 357031fd1beSPeter Maydell int r; 358031fd1beSPeter Maydell 359838ef602SMatthew Booth if (!s->s->multi) { 360031fd1beSPeter Maydell return; 361031fd1beSPeter Maydell } 362031fd1beSPeter Maydell 363031fd1beSPeter Maydell do { 364838ef602SMatthew Booth r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running); 365031fd1beSPeter Maydell } while(r == CURLM_CALL_MULTI_PERFORM); 366031fd1beSPeter Maydell 367838ef602SMatthew Booth } 368838ef602SMatthew Booth 369838ef602SMatthew Booth static void curl_multi_read(void *arg) 370838ef602SMatthew Booth { 371838ef602SMatthew Booth CURLState *s = (CURLState *)arg; 372838ef602SMatthew Booth 373838ef602SMatthew Booth curl_multi_do(arg); 374838ef602SMatthew Booth curl_multi_check_completion(s->s); 375031fd1beSPeter Maydell } 376031fd1beSPeter Maydell 377031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg) 378031fd1beSPeter Maydell { 379031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 380031fd1beSPeter Maydell BDRVCURLState *s = (BDRVCURLState *)arg; 381031fd1beSPeter Maydell int running; 382031fd1beSPeter Maydell 383031fd1beSPeter Maydell if (!s->multi) { 384031fd1beSPeter Maydell return; 385031fd1beSPeter Maydell } 386031fd1beSPeter Maydell 387031fd1beSPeter Maydell curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 388031fd1beSPeter Maydell 389838ef602SMatthew Booth curl_multi_check_completion(s); 390031fd1beSPeter Maydell #else 391031fd1beSPeter Maydell abort(); 392031fd1beSPeter Maydell #endif 393031fd1beSPeter Maydell } 394031fd1beSPeter Maydell 395a2f468e4SRichard W.M. Jones static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s) 396769ce76dSAlexander Graf { 397769ce76dSAlexander Graf CURLState *state = NULL; 398769ce76dSAlexander Graf int i, j; 399769ce76dSAlexander Graf 400769ce76dSAlexander Graf do { 401769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 402769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) 403769ce76dSAlexander Graf if (s->states[i].acb[j]) 404769ce76dSAlexander Graf continue; 405769ce76dSAlexander Graf if (s->states[i].in_use) 406769ce76dSAlexander Graf continue; 407769ce76dSAlexander Graf 408769ce76dSAlexander Graf state = &s->states[i]; 409769ce76dSAlexander Graf state->in_use = 1; 410769ce76dSAlexander Graf break; 411769ce76dSAlexander Graf } 412769ce76dSAlexander Graf if (!state) { 413a2f468e4SRichard W.M. Jones aio_poll(bdrv_get_aio_context(bs), true); 414769ce76dSAlexander Graf } 415769ce76dSAlexander Graf } while(!state); 416769ce76dSAlexander Graf 4179e550b32SMatthew Booth if (!state->curl) { 418769ce76dSAlexander Graf state->curl = curl_easy_init(); 4199e550b32SMatthew Booth if (!state->curl) { 420769ce76dSAlexander Graf return NULL; 4219e550b32SMatthew Booth } 422769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_URL, s->url); 42397a3ea57SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, 42497a3ea57SMatthew Booth (long) s->sslverify); 425a94f83d9SRichard W.M. Jones if (s->cookie) { 426a94f83d9SRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie); 427a94f83d9SRichard W.M. Jones } 428f76faedaSRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout); 4299e550b32SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, 4309e550b32SMatthew Booth (void *)curl_read_cb); 431769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); 432769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); 433769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); 434769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); 435769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); 436769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); 437f785a5aeSNicholas Thomas curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); 438769ce76dSAlexander Graf 4391bff9606SDaniel P. Berrange if (s->username) { 4401bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username); 4411bff9606SDaniel P. Berrange } 4421bff9606SDaniel P. Berrange if (s->password) { 4431bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password); 4441bff9606SDaniel P. Berrange } 4451bff9606SDaniel P. Berrange if (s->proxyusername) { 4461bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, 4471bff9606SDaniel P. Berrange CURLOPT_PROXYUSERNAME, s->proxyusername); 4481bff9606SDaniel P. Berrange } 4491bff9606SDaniel P. Berrange if (s->proxypassword) { 4501bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, 4511bff9606SDaniel P. Berrange CURLOPT_PROXYPASSWORD, s->proxypassword); 4521bff9606SDaniel P. Berrange } 4531bff9606SDaniel P. Berrange 454fb6d1bbdSStefan Hajnoczi /* Restrict supported protocols to avoid security issues in the more 455fb6d1bbdSStefan Hajnoczi * obscure protocols. For example, do not allow POP3/SMTP/IMAP see 456fb6d1bbdSStefan Hajnoczi * CVE-2013-0249. 4578a8f5840SStefan Hajnoczi * 4588a8f5840SStefan Hajnoczi * Restricting protocols is only supported from 7.19.4 upwards. 459fb6d1bbdSStefan Hajnoczi */ 4608a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304 461fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); 462fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); 4638a8f5840SStefan Hajnoczi #endif 464fb6d1bbdSStefan Hajnoczi 465769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE 466769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); 467769ce76dSAlexander Graf #endif 4689e550b32SMatthew Booth } 469769ce76dSAlexander Graf 470769ce76dSAlexander Graf state->s = s; 471769ce76dSAlexander Graf 472769ce76dSAlexander Graf return state; 473769ce76dSAlexander Graf } 474769ce76dSAlexander Graf 475769ce76dSAlexander Graf static void curl_clean_state(CURLState *s) 476769ce76dSAlexander Graf { 477769ce76dSAlexander Graf if (s->s->multi) 478769ce76dSAlexander Graf curl_multi_remove_handle(s->s->multi, s->curl); 479769ce76dSAlexander Graf s->in_use = 0; 480769ce76dSAlexander Graf } 481769ce76dSAlexander Graf 4828e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options, 4838e6d58cdSKevin Wolf Error **errp) 484769ce76dSAlexander Graf { 485e3542c67SMatthew Booth qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename)); 4868e6d58cdSKevin Wolf } 4878e6d58cdSKevin Wolf 48863f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs) 48963f0f45fSStefan Hajnoczi { 49063f0f45fSStefan Hajnoczi BDRVCURLState *s = bs->opaque; 49163f0f45fSStefan Hajnoczi int i; 49263f0f45fSStefan Hajnoczi 49363f0f45fSStefan Hajnoczi for (i = 0; i < CURL_NUM_STATES; i++) { 49463f0f45fSStefan Hajnoczi if (s->states[i].in_use) { 49563f0f45fSStefan Hajnoczi curl_clean_state(&s->states[i]); 49663f0f45fSStefan Hajnoczi } 49763f0f45fSStefan Hajnoczi if (s->states[i].curl) { 49863f0f45fSStefan Hajnoczi curl_easy_cleanup(s->states[i].curl); 49963f0f45fSStefan Hajnoczi s->states[i].curl = NULL; 50063f0f45fSStefan Hajnoczi } 50163f0f45fSStefan Hajnoczi g_free(s->states[i].orig_buf); 50263f0f45fSStefan Hajnoczi s->states[i].orig_buf = NULL; 50363f0f45fSStefan Hajnoczi } 50463f0f45fSStefan Hajnoczi if (s->multi) { 50563f0f45fSStefan Hajnoczi curl_multi_cleanup(s->multi); 50663f0f45fSStefan Hajnoczi s->multi = NULL; 50763f0f45fSStefan Hajnoczi } 50863f0f45fSStefan Hajnoczi 50963f0f45fSStefan Hajnoczi timer_del(&s->timer); 51063f0f45fSStefan Hajnoczi } 51163f0f45fSStefan Hajnoczi 51263f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs, 51363f0f45fSStefan Hajnoczi AioContext *new_context) 51463f0f45fSStefan Hajnoczi { 51563f0f45fSStefan Hajnoczi BDRVCURLState *s = bs->opaque; 51663f0f45fSStefan Hajnoczi 51763f0f45fSStefan Hajnoczi aio_timer_init(new_context, &s->timer, 51863f0f45fSStefan Hajnoczi QEMU_CLOCK_REALTIME, SCALE_NS, 51963f0f45fSStefan Hajnoczi curl_multi_timeout_do, s); 52063f0f45fSStefan Hajnoczi 52163f0f45fSStefan Hajnoczi assert(!s->multi); 52263f0f45fSStefan Hajnoczi s->multi = curl_multi_init(); 52363f0f45fSStefan Hajnoczi s->aio_context = new_context; 52463f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); 52563f0f45fSStefan Hajnoczi #ifdef NEED_CURL_TIMER_CALLBACK 52663f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); 52763f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); 52863f0f45fSStefan Hajnoczi #endif 52963f0f45fSStefan Hajnoczi } 53063f0f45fSStefan Hajnoczi 5318e6d58cdSKevin Wolf static QemuOptsList runtime_opts = { 5328e6d58cdSKevin Wolf .name = "curl", 5338e6d58cdSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 5348e6d58cdSKevin Wolf .desc = { 5358e6d58cdSKevin Wolf { 536e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_URL, 5378e6d58cdSKevin Wolf .type = QEMU_OPT_STRING, 5388e6d58cdSKevin Wolf .help = "URL to open", 5398e6d58cdSKevin Wolf }, 5408e6d58cdSKevin Wolf { 541e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_READAHEAD, 5428e6d58cdSKevin Wolf .type = QEMU_OPT_SIZE, 5438e6d58cdSKevin Wolf .help = "Readahead size", 5448e6d58cdSKevin Wolf }, 54597a3ea57SMatthew Booth { 54697a3ea57SMatthew Booth .name = CURL_BLOCK_OPT_SSLVERIFY, 54797a3ea57SMatthew Booth .type = QEMU_OPT_BOOL, 54897a3ea57SMatthew Booth .help = "Verify SSL certificate" 54997a3ea57SMatthew Booth }, 550212aefaaSDaniel Henrique Barboza { 551212aefaaSDaniel Henrique Barboza .name = CURL_BLOCK_OPT_TIMEOUT, 552212aefaaSDaniel Henrique Barboza .type = QEMU_OPT_NUMBER, 553212aefaaSDaniel Henrique Barboza .help = "Curl timeout" 554212aefaaSDaniel Henrique Barboza }, 555a94f83d9SRichard W.M. Jones { 556a94f83d9SRichard W.M. Jones .name = CURL_BLOCK_OPT_COOKIE, 557a94f83d9SRichard W.M. Jones .type = QEMU_OPT_STRING, 558a94f83d9SRichard W.M. Jones .help = "Pass the cookie or list of cookies with each request" 559a94f83d9SRichard W.M. Jones }, 5601bff9606SDaniel P. Berrange { 5611bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_USERNAME, 5621bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 5631bff9606SDaniel P. Berrange .help = "Username for HTTP auth" 5641bff9606SDaniel P. Berrange }, 5651bff9606SDaniel P. Berrange { 5661bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PASSWORD_SECRET, 5671bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 5681bff9606SDaniel P. Berrange .help = "ID of secret used as password for HTTP auth", 5691bff9606SDaniel P. Berrange }, 5701bff9606SDaniel P. Berrange { 5711bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PROXY_USERNAME, 5721bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 5731bff9606SDaniel P. Berrange .help = "Username for HTTP proxy auth" 5741bff9606SDaniel P. Berrange }, 5751bff9606SDaniel P. Berrange { 5761bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, 5771bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 5781bff9606SDaniel P. Berrange .help = "ID of secret used as password for HTTP proxy auth", 5791bff9606SDaniel P. Berrange }, 5808e6d58cdSKevin Wolf { /* end of list */ } 5818e6d58cdSKevin Wolf }, 5828e6d58cdSKevin Wolf }; 5838e6d58cdSKevin Wolf 5841bff9606SDaniel P. Berrange 585015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags, 586015a1036SMax Reitz Error **errp) 5878e6d58cdSKevin Wolf { 5888e6d58cdSKevin Wolf BDRVCURLState *s = bs->opaque; 5898e6d58cdSKevin Wolf CURLState *state = NULL; 5908e6d58cdSKevin Wolf QemuOpts *opts; 5918e6d58cdSKevin Wolf Error *local_err = NULL; 5928e6d58cdSKevin Wolf const char *file; 593a94f83d9SRichard W.M. Jones const char *cookie; 5948e6d58cdSKevin Wolf double d; 5951bff9606SDaniel P. Berrange const char *secretid; 5968e6d58cdSKevin Wolf 5978e6d58cdSKevin Wolf static int inited = 0; 5988e6d58cdSKevin Wolf 599a7cea2baSRichard W.M. Jones if (flags & BDRV_O_RDWR) { 6002a94fee3SPaolo Bonzini error_setg(errp, "curl block device does not support writes"); 601a7cea2baSRichard W.M. Jones return -EROFS; 602a7cea2baSRichard W.M. Jones } 603a7cea2baSRichard W.M. Jones 60487ea75d5SPeter Crosthwaite opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 6058e6d58cdSKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 60684d18f06SMarkus Armbruster if (local_err) { 6072a94fee3SPaolo Bonzini error_propagate(errp, local_err); 6088e6d58cdSKevin Wolf goto out_noclean; 6098e6d58cdSKevin Wolf } 6108e6d58cdSKevin Wolf 611e3542c67SMatthew Booth s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, 612e3542c67SMatthew Booth READ_AHEAD_DEFAULT); 613c76f4952SNolan if ((s->readahead_size & 0x1ff) != 0) { 6142a94fee3SPaolo Bonzini error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", 615c76f4952SNolan s->readahead_size); 616c76f4952SNolan goto out_noclean; 617c76f4952SNolan } 618c76f4952SNolan 619212aefaaSDaniel Henrique Barboza s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, 620212aefaaSDaniel Henrique Barboza CURL_TIMEOUT_DEFAULT); 621f76faedaSRichard W.M. Jones if (s->timeout > CURL_TIMEOUT_MAX) { 622f76faedaSRichard W.M. Jones error_setg(errp, "timeout parameter is too large or negative"); 623f76faedaSRichard W.M. Jones goto out_noclean; 624f76faedaSRichard W.M. Jones } 625212aefaaSDaniel Henrique Barboza 62697a3ea57SMatthew Booth s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true); 62797a3ea57SMatthew Booth 628a94f83d9SRichard W.M. Jones cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); 629a94f83d9SRichard W.M. Jones s->cookie = g_strdup(cookie); 630a94f83d9SRichard W.M. Jones 631e3542c67SMatthew Booth file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); 6328e6d58cdSKevin Wolf if (file == NULL) { 6332a94fee3SPaolo Bonzini error_setg(errp, "curl block driver requires an 'url' option"); 6348e6d58cdSKevin Wolf goto out_noclean; 6358e6d58cdSKevin Wolf } 6368e6d58cdSKevin Wolf 6371bff9606SDaniel P. Berrange s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME)); 6381bff9606SDaniel P. Berrange secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET); 6391bff9606SDaniel P. Berrange 6401bff9606SDaniel P. Berrange if (secretid) { 6411bff9606SDaniel P. Berrange s->password = qcrypto_secret_lookup_as_utf8(secretid, errp); 6421bff9606SDaniel P. Berrange if (!s->password) { 6431bff9606SDaniel P. Berrange goto out_noclean; 6441bff9606SDaniel P. Berrange } 6451bff9606SDaniel P. Berrange } 6461bff9606SDaniel P. Berrange 6471bff9606SDaniel P. Berrange s->proxyusername = g_strdup( 6481bff9606SDaniel P. Berrange qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME)); 6491bff9606SDaniel P. Berrange secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET); 6501bff9606SDaniel P. Berrange if (secretid) { 6511bff9606SDaniel P. Berrange s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp); 6521bff9606SDaniel P. Berrange if (!s->proxypassword) { 6531bff9606SDaniel P. Berrange goto out_noclean; 6541bff9606SDaniel P. Berrange } 6551bff9606SDaniel P. Berrange } 6561bff9606SDaniel P. Berrange 657769ce76dSAlexander Graf if (!inited) { 658769ce76dSAlexander Graf curl_global_init(CURL_GLOBAL_ALL); 659769ce76dSAlexander Graf inited = 1; 660769ce76dSAlexander Graf } 661769ce76dSAlexander Graf 662d0f2c4c6Smalc DPRINTF("CURL: Opening %s\n", file); 66363f0f45fSStefan Hajnoczi s->aio_context = bdrv_get_aio_context(bs); 6648e6d58cdSKevin Wolf s->url = g_strdup(file); 665a2f468e4SRichard W.M. Jones state = curl_init_state(bs, s); 666769ce76dSAlexander Graf if (!state) 667769ce76dSAlexander Graf goto out_noclean; 668769ce76dSAlexander Graf 669769ce76dSAlexander Graf // Get file size 670769ce76dSAlexander Graf 6713494d650SFam Zheng s->accept_range = false; 672769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); 6733494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, 6743494d650SFam Zheng curl_header_cb); 6753494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); 676769ce76dSAlexander Graf if (curl_easy_perform(state->curl)) 677769ce76dSAlexander Graf goto out; 678*a41c4578STomáš Golembiovský if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { 679769ce76dSAlexander Graf goto out; 680*a41c4578STomáš Golembiovský } 681*a41c4578STomáš Golembiovský /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not 682*a41c4578STomáš Golembiovský * know or the size is zero. From 7.19.4 CURL returns -1 if size is not 683*a41c4578STomáš Golembiovský * known and zero if it is realy zero-length file. */ 684*a41c4578STomáš Golembiovský #if LIBCURL_VERSION_NUM >= 0x071304 685*a41c4578STomáš Golembiovský if (d < 0) { 686*a41c4578STomáš Golembiovský pstrcpy(state->errmsg, CURL_ERROR_SIZE, 687*a41c4578STomáš Golembiovský "Server didn't report file size."); 688*a41c4578STomáš Golembiovský goto out; 689*a41c4578STomáš Golembiovský } 690*a41c4578STomáš Golembiovský #else 691*a41c4578STomáš Golembiovský if (d <= 0) { 692*a41c4578STomáš Golembiovský pstrcpy(state->errmsg, CURL_ERROR_SIZE, 693*a41c4578STomáš Golembiovský "Unknown file size or zero-length file."); 694*a41c4578STomáš Golembiovský goto out; 695*a41c4578STomáš Golembiovský } 696*a41c4578STomáš Golembiovský #endif 697*a41c4578STomáš Golembiovský 698*a41c4578STomáš Golembiovský s->len = (size_t)d; 699*a41c4578STomáš Golembiovský 7003494d650SFam Zheng if ((!strncasecmp(s->url, "http://", strlen("http://")) 7013494d650SFam Zheng || !strncasecmp(s->url, "https://", strlen("https://"))) 7023494d650SFam Zheng && !s->accept_range) { 7033494d650SFam Zheng pstrcpy(state->errmsg, CURL_ERROR_SIZE, 7043494d650SFam Zheng "Server does not support 'range' (byte ranges)."); 7053494d650SFam Zheng goto out; 7063494d650SFam Zheng } 7070bfcd599SBlue Swirl DPRINTF("CURL: Size = %zd\n", s->len); 708769ce76dSAlexander Graf 709769ce76dSAlexander Graf curl_clean_state(state); 710769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 711769ce76dSAlexander Graf state->curl = NULL; 712769ce76dSAlexander Graf 71363f0f45fSStefan Hajnoczi curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); 714769ce76dSAlexander Graf 7158e6d58cdSKevin Wolf qemu_opts_del(opts); 716769ce76dSAlexander Graf return 0; 717769ce76dSAlexander Graf 718769ce76dSAlexander Graf out: 719acd7fdc6SMaria Kustova error_setg(errp, "CURL: Error opening file: %s", state->errmsg); 720769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 721769ce76dSAlexander Graf state->curl = NULL; 722769ce76dSAlexander Graf out_noclean: 723a94f83d9SRichard W.M. Jones g_free(s->cookie); 7248e6d58cdSKevin Wolf g_free(s->url); 7258e6d58cdSKevin Wolf qemu_opts_del(opts); 726769ce76dSAlexander Graf return -EINVAL; 727769ce76dSAlexander Graf } 728769ce76dSAlexander Graf 729d7331bedSStefan Hajnoczi static const AIOCBInfo curl_aiocb_info = { 730c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(CURLAIOCB), 731c16b5a2cSChristoph Hellwig }; 732c16b5a2cSChristoph Hellwig 733363c3c85SNick Thomas 734363c3c85SNick Thomas static void curl_readv_bh_cb(void *p) 735769ce76dSAlexander Graf { 736769ce76dSAlexander Graf CURLState *state; 737b69cdef8SMatthew Booth int running; 738769ce76dSAlexander Graf 739363c3c85SNick Thomas CURLAIOCB *acb = p; 740363c3c85SNick Thomas BDRVCURLState *s = acb->common.bs->opaque; 741769ce76dSAlexander Graf 742363c3c85SNick Thomas qemu_bh_delete(acb->bh); 743363c3c85SNick Thomas acb->bh = NULL; 744363c3c85SNick Thomas 745363c3c85SNick Thomas size_t start = acb->sector_num * SECTOR_SIZE; 746363c3c85SNick Thomas size_t end; 747769ce76dSAlexander Graf 748769ce76dSAlexander Graf // In case we have the requested data already (e.g. read-ahead), 749769ce76dSAlexander Graf // we can just call the callback and be done. 750363c3c85SNick Thomas switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) { 751769ce76dSAlexander Graf case FIND_RET_OK: 7528007429aSFam Zheng qemu_aio_unref(acb); 753769ce76dSAlexander Graf // fall through 754769ce76dSAlexander Graf case FIND_RET_WAIT: 755363c3c85SNick Thomas return; 756769ce76dSAlexander Graf default: 757769ce76dSAlexander Graf break; 758769ce76dSAlexander Graf } 759769ce76dSAlexander Graf 760769ce76dSAlexander Graf // No cache found, so let's start a new request 761a2f468e4SRichard W.M. Jones state = curl_init_state(acb->common.bs, s); 762363c3c85SNick Thomas if (!state) { 763363c3c85SNick Thomas acb->common.cb(acb->common.opaque, -EIO); 7648007429aSFam Zheng qemu_aio_unref(acb); 765363c3c85SNick Thomas return; 766363c3c85SNick Thomas } 767769ce76dSAlexander Graf 768769ce76dSAlexander Graf acb->start = 0; 769363c3c85SNick Thomas acb->end = (acb->nb_sectors * SECTOR_SIZE); 770769ce76dSAlexander Graf 771769ce76dSAlexander Graf state->buf_off = 0; 7727267c094SAnthony Liguori g_free(state->orig_buf); 773769ce76dSAlexander Graf state->buf_start = start; 774c76f4952SNolan state->buf_len = acb->end + s->readahead_size; 775769ce76dSAlexander Graf end = MIN(start + state->buf_len, s->len) - 1; 7768dc7a772SKevin Wolf state->orig_buf = g_try_malloc(state->buf_len); 7778dc7a772SKevin Wolf if (state->buf_len && state->orig_buf == NULL) { 7788dc7a772SKevin Wolf curl_clean_state(state); 7798dc7a772SKevin Wolf acb->common.cb(acb->common.opaque, -ENOMEM); 7808007429aSFam Zheng qemu_aio_unref(acb); 7818dc7a772SKevin Wolf return; 7828dc7a772SKevin Wolf } 783769ce76dSAlexander Graf state->acb[0] = acb; 784769ce76dSAlexander Graf 7850bfcd599SBlue Swirl snprintf(state->range, 127, "%zd-%zd", start, end); 7860bfcd599SBlue Swirl DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n", 787363c3c85SNick Thomas (acb->nb_sectors * SECTOR_SIZE), start, state->range); 788769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); 789769ce76dSAlexander Graf 790769ce76dSAlexander Graf curl_multi_add_handle(s->multi, state->curl); 791769ce76dSAlexander Graf 792b69cdef8SMatthew Booth /* Tell curl it needs to kick things off */ 793b69cdef8SMatthew Booth curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 794363c3c85SNick Thomas } 795363c3c85SNick Thomas 7967c84b1b8SMarkus Armbruster static BlockAIOCB *curl_aio_readv(BlockDriverState *bs, 797363c3c85SNick Thomas int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 798097310b5SMarkus Armbruster BlockCompletionFunc *cb, void *opaque) 799363c3c85SNick Thomas { 800363c3c85SNick Thomas CURLAIOCB *acb; 801363c3c85SNick Thomas 802d7331bedSStefan Hajnoczi acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque); 803363c3c85SNick Thomas 804363c3c85SNick Thomas acb->qiov = qiov; 805363c3c85SNick Thomas acb->sector_num = sector_num; 806363c3c85SNick Thomas acb->nb_sectors = nb_sectors; 807363c3c85SNick Thomas 80863f0f45fSStefan Hajnoczi acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb); 809363c3c85SNick Thomas qemu_bh_schedule(acb->bh); 810769ce76dSAlexander Graf return &acb->common; 811769ce76dSAlexander Graf } 812769ce76dSAlexander Graf 813769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs) 814769ce76dSAlexander Graf { 815769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 816769ce76dSAlexander Graf 817d0f2c4c6Smalc DPRINTF("CURL: Close\n"); 81863f0f45fSStefan Hajnoczi curl_detach_aio_context(bs); 819031fd1beSPeter Maydell 820a94f83d9SRichard W.M. Jones g_free(s->cookie); 82145724d6dSStefan Weil g_free(s->url); 822769ce76dSAlexander Graf } 823769ce76dSAlexander Graf 824769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs) 825769ce76dSAlexander Graf { 826769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 827769ce76dSAlexander Graf return s->len; 828769ce76dSAlexander Graf } 829769ce76dSAlexander Graf 830769ce76dSAlexander Graf static BlockDriver bdrv_http = { 831769ce76dSAlexander Graf .format_name = "http", 832769ce76dSAlexander Graf .protocol_name = "http", 833769ce76dSAlexander Graf 834769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 8358e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 83666f82ceeSKevin Wolf .bdrv_file_open = curl_open, 837769ce76dSAlexander Graf .bdrv_close = curl_close, 838769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 839769ce76dSAlexander Graf 840769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 84163f0f45fSStefan Hajnoczi 84263f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 84363f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 844769ce76dSAlexander Graf }; 845769ce76dSAlexander Graf 846769ce76dSAlexander Graf static BlockDriver bdrv_https = { 847769ce76dSAlexander Graf .format_name = "https", 848769ce76dSAlexander Graf .protocol_name = "https", 849769ce76dSAlexander Graf 850769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 8518e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 85266f82ceeSKevin Wolf .bdrv_file_open = curl_open, 853769ce76dSAlexander Graf .bdrv_close = curl_close, 854769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 855769ce76dSAlexander Graf 856769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 85763f0f45fSStefan Hajnoczi 85863f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 85963f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 860769ce76dSAlexander Graf }; 861769ce76dSAlexander Graf 862769ce76dSAlexander Graf static BlockDriver bdrv_ftp = { 863769ce76dSAlexander Graf .format_name = "ftp", 864769ce76dSAlexander Graf .protocol_name = "ftp", 865769ce76dSAlexander Graf 866769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 8678e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 86866f82ceeSKevin Wolf .bdrv_file_open = curl_open, 869769ce76dSAlexander Graf .bdrv_close = curl_close, 870769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 871769ce76dSAlexander Graf 872769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 87363f0f45fSStefan Hajnoczi 87463f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 87563f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 876769ce76dSAlexander Graf }; 877769ce76dSAlexander Graf 878769ce76dSAlexander Graf static BlockDriver bdrv_ftps = { 879769ce76dSAlexander Graf .format_name = "ftps", 880769ce76dSAlexander Graf .protocol_name = "ftps", 881769ce76dSAlexander Graf 882769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 8838e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 88466f82ceeSKevin Wolf .bdrv_file_open = curl_open, 885769ce76dSAlexander Graf .bdrv_close = curl_close, 886769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 887769ce76dSAlexander Graf 888769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 88963f0f45fSStefan Hajnoczi 89063f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 89163f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 892769ce76dSAlexander Graf }; 893769ce76dSAlexander Graf 894769ce76dSAlexander Graf static BlockDriver bdrv_tftp = { 895769ce76dSAlexander Graf .format_name = "tftp", 896769ce76dSAlexander Graf .protocol_name = "tftp", 897769ce76dSAlexander Graf 898769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 8998e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 90066f82ceeSKevin Wolf .bdrv_file_open = curl_open, 901769ce76dSAlexander Graf .bdrv_close = curl_close, 902769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 903769ce76dSAlexander Graf 904769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 90563f0f45fSStefan Hajnoczi 90663f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 90763f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 908769ce76dSAlexander Graf }; 909769ce76dSAlexander Graf 910769ce76dSAlexander Graf static void curl_block_init(void) 911769ce76dSAlexander Graf { 912769ce76dSAlexander Graf bdrv_register(&bdrv_http); 913769ce76dSAlexander Graf bdrv_register(&bdrv_https); 914769ce76dSAlexander Graf bdrv_register(&bdrv_ftp); 915769ce76dSAlexander Graf bdrv_register(&bdrv_ftps); 916769ce76dSAlexander Graf bdrv_register(&bdrv_tftp); 917769ce76dSAlexander Graf } 918769ce76dSAlexander Graf 919769ce76dSAlexander Graf block_init(curl_block_init); 920