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 */ 24452fcdbcSMarkus Armbruster 2580c71a24SPeter Maydell #include "qemu/osdep.h" 26da34e65cSMarkus Armbruster #include "qapi/error.h" 27796a060bSRichard W.M. Jones #include "qemu/error-report.h" 28*922a01a0SMarkus Armbruster #include "qemu/option.h" 29737e150eSPaolo Bonzini #include "block/block_int.h" 30452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 31d49b6836SMarkus Armbruster #include "qapi/qmp/qstring.h" 321bff9606SDaniel P. Berrange #include "crypto/secret.h" 33769ce76dSAlexander Graf #include <curl/curl.h> 34f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 35769ce76dSAlexander Graf 3641c23467SRichard W.M. Jones // #define DEBUG_CURL 37769ce76dSAlexander Graf // #define DEBUG_VERBOSE 38769ce76dSAlexander Graf 39769ce76dSAlexander Graf #ifdef DEBUG_CURL 40ed79f37dSZhou Jie #define DEBUG_CURL_PRINT 1 41769ce76dSAlexander Graf #else 42ed79f37dSZhou Jie #define DEBUG_CURL_PRINT 0 43769ce76dSAlexander Graf #endif 44ed79f37dSZhou Jie #define DPRINTF(fmt, ...) \ 45ed79f37dSZhou Jie do { \ 46ed79f37dSZhou Jie if (DEBUG_CURL_PRINT) { \ 47ed79f37dSZhou Jie fprintf(stderr, fmt, ## __VA_ARGS__); \ 48ed79f37dSZhou Jie } \ 49ed79f37dSZhou Jie } while (0) 50769ce76dSAlexander Graf 51031fd1beSPeter Maydell #if LIBCURL_VERSION_NUM >= 0x071000 52031fd1beSPeter Maydell /* The multi interface timer callback was introduced in 7.16.0 */ 53031fd1beSPeter Maydell #define NEED_CURL_TIMER_CALLBACK 549aedd5a5SMatthew Booth #define HAVE_SOCKET_ACTION 559aedd5a5SMatthew Booth #endif 569aedd5a5SMatthew Booth 579aedd5a5SMatthew Booth #ifndef HAVE_SOCKET_ACTION 589aedd5a5SMatthew Booth /* If curl_multi_socket_action isn't available, define it statically here in 599aedd5a5SMatthew Booth * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is 609aedd5a5SMatthew Booth * less efficient but still safe. */ 619aedd5a5SMatthew Booth static CURLMcode __curl_multi_socket_action(CURLM *multi_handle, 629aedd5a5SMatthew Booth curl_socket_t sockfd, 639aedd5a5SMatthew Booth int ev_bitmask, 649aedd5a5SMatthew Booth int *running_handles) 659aedd5a5SMatthew Booth { 669aedd5a5SMatthew Booth return curl_multi_socket(multi_handle, sockfd, running_handles); 679aedd5a5SMatthew Booth } 689aedd5a5SMatthew Booth #define curl_multi_socket_action __curl_multi_socket_action 69031fd1beSPeter Maydell #endif 70031fd1beSPeter Maydell 71fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ 7223dce387SMax Reitz CURLPROTO_FTP | CURLPROTO_FTPS) 73fb6d1bbdSStefan Hajnoczi 74769ce76dSAlexander Graf #define CURL_NUM_STATES 8 75769ce76dSAlexander Graf #define CURL_NUM_ACB 8 76e3542c67SMatthew Booth #define READ_AHEAD_DEFAULT (256 * 1024) 77212aefaaSDaniel Henrique Barboza #define CURL_TIMEOUT_DEFAULT 5 78f76faedaSRichard W.M. Jones #define CURL_TIMEOUT_MAX 10000 79769ce76dSAlexander Graf 80e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL "url" 81e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead" 8297a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" 83212aefaaSDaniel Henrique Barboza #define CURL_BLOCK_OPT_TIMEOUT "timeout" 84a94f83d9SRichard W.M. Jones #define CURL_BLOCK_OPT_COOKIE "cookie" 85327c8ebdSPeter Krempa #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret" 861bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_USERNAME "username" 871bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret" 881bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username" 891bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret" 90e3542c67SMatthew Booth 91769ce76dSAlexander Graf struct BDRVCURLState; 92769ce76dSAlexander Graf 932d25964dSJeff Cody static bool libcurl_initialized; 942d25964dSJeff Cody 95769ce76dSAlexander Graf typedef struct CURLAIOCB { 9628256d82SPaolo Bonzini Coroutine *co; 97769ce76dSAlexander Graf QEMUIOVector *qiov; 98363c3c85SNick Thomas 992125e5eaSPaolo Bonzini uint64_t offset; 1002125e5eaSPaolo Bonzini uint64_t bytes; 10128256d82SPaolo Bonzini int ret; 102363c3c85SNick Thomas 103769ce76dSAlexander Graf size_t start; 104769ce76dSAlexander Graf size_t end; 105769ce76dSAlexander Graf } CURLAIOCB; 106769ce76dSAlexander Graf 107ff5ca166SMax Reitz typedef struct CURLSocket { 108ff5ca166SMax Reitz int fd; 109ff5ca166SMax Reitz QLIST_ENTRY(CURLSocket) next; 110ff5ca166SMax Reitz } CURLSocket; 111ff5ca166SMax Reitz 112769ce76dSAlexander Graf typedef struct CURLState 113769ce76dSAlexander Graf { 114769ce76dSAlexander Graf struct BDRVCURLState *s; 115769ce76dSAlexander Graf CURLAIOCB *acb[CURL_NUM_ACB]; 116769ce76dSAlexander Graf CURL *curl; 117ff5ca166SMax Reitz QLIST_HEAD(, CURLSocket) sockets; 118769ce76dSAlexander Graf char *orig_buf; 1192125e5eaSPaolo Bonzini uint64_t buf_start; 120769ce76dSAlexander Graf size_t buf_off; 121769ce76dSAlexander Graf size_t buf_len; 122769ce76dSAlexander Graf char range[128]; 123769ce76dSAlexander Graf char errmsg[CURL_ERROR_SIZE]; 124769ce76dSAlexander Graf char in_use; 125769ce76dSAlexander Graf } CURLState; 126769ce76dSAlexander Graf 127769ce76dSAlexander Graf typedef struct BDRVCURLState { 128769ce76dSAlexander Graf CURLM *multi; 129031fd1beSPeter Maydell QEMUTimer timer; 1302125e5eaSPaolo Bonzini uint64_t len; 131769ce76dSAlexander Graf CURLState states[CURL_NUM_STATES]; 132769ce76dSAlexander Graf char *url; 133c76f4952SNolan size_t readahead_size; 13497a3ea57SMatthew Booth bool sslverify; 135f76faedaSRichard W.M. Jones uint64_t timeout; 136a94f83d9SRichard W.M. Jones char *cookie; 1373494d650SFam Zheng bool accept_range; 13863f0f45fSStefan Hajnoczi AioContext *aio_context; 139ba3186c4SPaolo Bonzini QemuMutex mutex; 140709f2132SPaolo Bonzini CoQueue free_state_waitq; 1411bff9606SDaniel P. Berrange char *username; 1421bff9606SDaniel P. Berrange char *password; 1431bff9606SDaniel P. Berrange char *proxyusername; 1441bff9606SDaniel P. Berrange char *proxypassword; 145769ce76dSAlexander Graf } BDRVCURLState; 146769ce76dSAlexander Graf 147769ce76dSAlexander Graf static void curl_clean_state(CURLState *s); 148769ce76dSAlexander Graf static void curl_multi_do(void *arg); 149838ef602SMatthew Booth static void curl_multi_read(void *arg); 150769ce76dSAlexander Graf 151031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 15234db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 153031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) 154031fd1beSPeter Maydell { 155031fd1beSPeter Maydell BDRVCURLState *s = opaque; 156031fd1beSPeter Maydell 157031fd1beSPeter Maydell DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms); 158031fd1beSPeter Maydell if (timeout_ms == -1) { 159031fd1beSPeter Maydell timer_del(&s->timer); 160031fd1beSPeter Maydell } else { 161031fd1beSPeter Maydell int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; 162031fd1beSPeter Maydell timer_mod(&s->timer, 163031fd1beSPeter Maydell qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); 164031fd1beSPeter Maydell } 165031fd1beSPeter Maydell return 0; 166031fd1beSPeter Maydell } 167031fd1beSPeter Maydell #endif 168031fd1beSPeter Maydell 16934db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 170769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, 17163f0f45fSStefan Hajnoczi void *userp, void *sp) 172769ce76dSAlexander Graf { 17363f0f45fSStefan Hajnoczi BDRVCURLState *s; 174838ef602SMatthew Booth CURLState *state = NULL; 175ff5ca166SMax Reitz CURLSocket *socket; 176ff5ca166SMax Reitz 177838ef602SMatthew Booth curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); 17863f0f45fSStefan Hajnoczi s = state->s; 179838ef602SMatthew Booth 180ff5ca166SMax Reitz QLIST_FOREACH(socket, &state->sockets, next) { 181ff5ca166SMax Reitz if (socket->fd == fd) { 182ff5ca166SMax Reitz if (action == CURL_POLL_REMOVE) { 183ff5ca166SMax Reitz QLIST_REMOVE(socket, next); 184ff5ca166SMax Reitz g_free(socket); 185ff5ca166SMax Reitz } 186ff5ca166SMax Reitz break; 187ff5ca166SMax Reitz } 188ff5ca166SMax Reitz } 189ff5ca166SMax Reitz if (!socket) { 190ff5ca166SMax Reitz socket = g_new0(CURLSocket, 1); 191ff5ca166SMax Reitz socket->fd = fd; 192ff5ca166SMax Reitz QLIST_INSERT_HEAD(&state->sockets, socket, next); 193ff5ca166SMax Reitz } 194ff5ca166SMax Reitz socket = NULL; 195ff5ca166SMax Reitz 19692b6a160SFam Zheng DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, (int)fd); 197769ce76dSAlexander Graf switch (action) { 198769ce76dSAlexander Graf case CURL_POLL_IN: 199dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 200f6a51c84SStefan Hajnoczi curl_multi_read, NULL, NULL, state); 201769ce76dSAlexander Graf break; 202769ce76dSAlexander Graf case CURL_POLL_OUT: 203dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 204f6a51c84SStefan Hajnoczi NULL, curl_multi_do, NULL, state); 205769ce76dSAlexander Graf break; 206769ce76dSAlexander Graf case CURL_POLL_INOUT: 207dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 208f6a51c84SStefan Hajnoczi curl_multi_read, curl_multi_do, NULL, state); 209769ce76dSAlexander Graf break; 210769ce76dSAlexander Graf case CURL_POLL_REMOVE: 211dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 212f6a51c84SStefan Hajnoczi NULL, NULL, NULL, NULL); 213769ce76dSAlexander Graf break; 214769ce76dSAlexander Graf } 215769ce76dSAlexander Graf 216769ce76dSAlexander Graf return 0; 217769ce76dSAlexander Graf } 218769ce76dSAlexander Graf 21934db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 2203494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 221769ce76dSAlexander Graf { 2223494d650SFam Zheng BDRVCURLState *s = opaque; 223769ce76dSAlexander Graf size_t realsize = size * nmemb; 2243494d650SFam Zheng const char *accept_line = "Accept-Ranges: bytes"; 225769ce76dSAlexander Graf 2263494d650SFam Zheng if (realsize >= strlen(accept_line) 2273494d650SFam Zheng && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) { 2283494d650SFam Zheng s->accept_range = true; 2290bfcd599SBlue Swirl } 230769ce76dSAlexander Graf 231769ce76dSAlexander Graf return realsize; 232769ce76dSAlexander Graf } 233769ce76dSAlexander Graf 23434db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 235769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 236769ce76dSAlexander Graf { 237769ce76dSAlexander Graf CURLState *s = ((CURLState*)opaque); 238769ce76dSAlexander Graf size_t realsize = size * nmemb; 239769ce76dSAlexander Graf int i; 240769ce76dSAlexander Graf 2410bfcd599SBlue Swirl DPRINTF("CURL: Just reading %zd bytes\n", realsize); 242769ce76dSAlexander Graf 2434e767657SMax Reitz if (!s || !s->orig_buf) { 2444e767657SMax Reitz goto read_end; 2454e767657SMax Reitz } 246769ce76dSAlexander Graf 2476d4b9e55SFam Zheng if (s->buf_off >= s->buf_len) { 2486d4b9e55SFam Zheng /* buffer full, read nothing */ 2494e767657SMax Reitz goto read_end; 2506d4b9e55SFam Zheng } 2516d4b9e55SFam Zheng realsize = MIN(realsize, s->buf_len - s->buf_off); 252769ce76dSAlexander Graf memcpy(s->orig_buf + s->buf_off, ptr, realsize); 253769ce76dSAlexander Graf s->buf_off += realsize; 254769ce76dSAlexander Graf 255769ce76dSAlexander Graf for(i=0; i<CURL_NUM_ACB; i++) { 256769ce76dSAlexander Graf CURLAIOCB *acb = s->acb[i]; 257769ce76dSAlexander Graf 258769ce76dSAlexander Graf if (!acb) 259769ce76dSAlexander Graf continue; 260769ce76dSAlexander Graf 261769ce76dSAlexander Graf if ((s->buf_off >= acb->end)) { 2622125e5eaSPaolo Bonzini size_t request_length = acb->bytes; 2634e504535SMax Reitz 26403396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start, 265769ce76dSAlexander Graf acb->end - acb->start); 2664e504535SMax Reitz 2674e504535SMax Reitz if (acb->end - acb->start < request_length) { 2684e504535SMax Reitz size_t offset = acb->end - acb->start; 2694e504535SMax Reitz qemu_iovec_memset(acb->qiov, offset, 0, 2704e504535SMax Reitz request_length - offset); 2714e504535SMax Reitz } 2724e504535SMax Reitz 27328256d82SPaolo Bonzini acb->ret = 0; 274769ce76dSAlexander Graf s->acb[i] = NULL; 27528256d82SPaolo Bonzini qemu_mutex_unlock(&s->s->mutex); 27628256d82SPaolo Bonzini aio_co_wake(acb->co); 27728256d82SPaolo Bonzini qemu_mutex_lock(&s->s->mutex); 278769ce76dSAlexander Graf } 279769ce76dSAlexander Graf } 280769ce76dSAlexander Graf 2814e767657SMax Reitz read_end: 2824e767657SMax Reitz /* curl will error out if we do not return this value */ 2834e767657SMax Reitz return size * nmemb; 284769ce76dSAlexander Graf } 285769ce76dSAlexander Graf 286456af346SPaolo Bonzini /* Called with s->mutex held. */ 28728256d82SPaolo Bonzini static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len, 288769ce76dSAlexander Graf CURLAIOCB *acb) 289769ce76dSAlexander Graf { 290769ce76dSAlexander Graf int i; 2912125e5eaSPaolo Bonzini uint64_t end = start + len; 2922125e5eaSPaolo Bonzini uint64_t clamped_end = MIN(end, s->len); 2932125e5eaSPaolo Bonzini uint64_t clamped_len = clamped_end - start; 294769ce76dSAlexander Graf 295769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 296769ce76dSAlexander Graf CURLState *state = &s->states[i]; 2972125e5eaSPaolo Bonzini uint64_t buf_end = (state->buf_start + state->buf_off); 2982125e5eaSPaolo Bonzini uint64_t buf_fend = (state->buf_start + state->buf_len); 299769ce76dSAlexander Graf 300769ce76dSAlexander Graf if (!state->orig_buf) 301769ce76dSAlexander Graf continue; 302769ce76dSAlexander Graf if (!state->buf_off) 303769ce76dSAlexander Graf continue; 304769ce76dSAlexander Graf 305769ce76dSAlexander Graf // Does the existing buffer cover our section? 306769ce76dSAlexander Graf if ((start >= state->buf_start) && 307769ce76dSAlexander Graf (start <= buf_end) && 3084e504535SMax Reitz (clamped_end >= state->buf_start) && 3094e504535SMax Reitz (clamped_end <= buf_end)) 310769ce76dSAlexander Graf { 311769ce76dSAlexander Graf char *buf = state->orig_buf + (start - state->buf_start); 312769ce76dSAlexander Graf 3134e504535SMax Reitz qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len); 3144e504535SMax Reitz if (clamped_len < len) { 3154e504535SMax Reitz qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len); 3164e504535SMax Reitz } 31728256d82SPaolo Bonzini acb->ret = 0; 31828256d82SPaolo Bonzini return true; 319769ce76dSAlexander Graf } 320769ce76dSAlexander Graf 321769ce76dSAlexander Graf // Wait for unfinished chunks 322b7079df4SMatthew Booth if (state->in_use && 323b7079df4SMatthew Booth (start >= state->buf_start) && 324769ce76dSAlexander Graf (start <= buf_fend) && 3254e504535SMax Reitz (clamped_end >= state->buf_start) && 3264e504535SMax Reitz (clamped_end <= buf_fend)) 327769ce76dSAlexander Graf { 328769ce76dSAlexander Graf int j; 329769ce76dSAlexander Graf 330769ce76dSAlexander Graf acb->start = start - state->buf_start; 3314e504535SMax Reitz acb->end = acb->start + clamped_len; 332769ce76dSAlexander Graf 333769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) { 334769ce76dSAlexander Graf if (!state->acb[j]) { 335769ce76dSAlexander Graf state->acb[j] = acb; 33628256d82SPaolo Bonzini return true; 337769ce76dSAlexander Graf } 338769ce76dSAlexander Graf } 339769ce76dSAlexander Graf } 340769ce76dSAlexander Graf } 341769ce76dSAlexander Graf 34228256d82SPaolo Bonzini return false; 343769ce76dSAlexander Graf } 344769ce76dSAlexander Graf 345ba3186c4SPaolo Bonzini /* Called with s->mutex held. */ 346838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s) 347769ce76dSAlexander Graf { 348769ce76dSAlexander Graf int msgs_in_queue; 349769ce76dSAlexander Graf 350769ce76dSAlexander Graf /* Try to find done transfers, so we can free the easy 351769ce76dSAlexander Graf * handle again. */ 3521f2cead3SMatthew Booth for (;;) { 353769ce76dSAlexander Graf CURLMsg *msg; 354769ce76dSAlexander Graf msg = curl_multi_info_read(s->multi, &msgs_in_queue); 355769ce76dSAlexander Graf 3561f2cead3SMatthew Booth /* Quit when there are no more completions */ 357769ce76dSAlexander Graf if (!msg) 358769ce76dSAlexander Graf break; 359769ce76dSAlexander Graf 3601f2cead3SMatthew Booth if (msg->msg == CURLMSG_DONE) { 361769ce76dSAlexander Graf CURLState *state = NULL; 362f6246509SMatthew Booth curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, 363f6246509SMatthew Booth (char **)&state); 364f785a5aeSNicholas Thomas 365f785a5aeSNicholas Thomas /* ACBs for successful messages get completed in curl_read_cb */ 366f785a5aeSNicholas Thomas if (msg->data.result != CURLE_OK) { 367f785a5aeSNicholas Thomas int i; 368796a060bSRichard W.M. Jones static int errcount = 100; 369796a060bSRichard W.M. Jones 370796a060bSRichard W.M. Jones /* Don't lose the original error message from curl, since 371796a060bSRichard W.M. Jones * it contains extra data. 372796a060bSRichard W.M. Jones */ 373796a060bSRichard W.M. Jones if (errcount > 0) { 374796a060bSRichard W.M. Jones error_report("curl: %s", state->errmsg); 375796a060bSRichard W.M. Jones if (--errcount == 0) { 376796a060bSRichard W.M. Jones error_report("curl: further errors suppressed"); 377796a060bSRichard W.M. Jones } 378796a060bSRichard W.M. Jones } 379796a060bSRichard W.M. Jones 380f785a5aeSNicholas Thomas for (i = 0; i < CURL_NUM_ACB; i++) { 381f785a5aeSNicholas Thomas CURLAIOCB *acb = state->acb[i]; 382f785a5aeSNicholas Thomas 383f785a5aeSNicholas Thomas if (acb == NULL) { 384f785a5aeSNicholas Thomas continue; 385f785a5aeSNicholas Thomas } 386f785a5aeSNicholas Thomas 38728256d82SPaolo Bonzini acb->ret = -EIO; 388f785a5aeSNicholas Thomas state->acb[i] = NULL; 38928256d82SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 39028256d82SPaolo Bonzini aio_co_wake(acb->co); 39128256d82SPaolo Bonzini qemu_mutex_lock(&s->mutex); 392f785a5aeSNicholas Thomas } 393f785a5aeSNicholas Thomas } 394f785a5aeSNicholas Thomas 395769ce76dSAlexander Graf curl_clean_state(state); 396769ce76dSAlexander Graf break; 397769ce76dSAlexander Graf } 398769ce76dSAlexander Graf } 399769ce76dSAlexander Graf } 400769ce76dSAlexander Graf 401ba3186c4SPaolo Bonzini /* Called with s->mutex held. */ 4029d456654SPaolo Bonzini static void curl_multi_do_locked(CURLState *s) 403031fd1beSPeter Maydell { 404ff5ca166SMax Reitz CURLSocket *socket, *next_socket; 405031fd1beSPeter Maydell int running; 406031fd1beSPeter Maydell int r; 407031fd1beSPeter Maydell 408838ef602SMatthew Booth if (!s->s->multi) { 409031fd1beSPeter Maydell return; 410031fd1beSPeter Maydell } 411031fd1beSPeter Maydell 412ff5ca166SMax Reitz /* Need to use _SAFE because curl_multi_socket_action() may trigger 413ff5ca166SMax Reitz * curl_sock_cb() which might modify this list */ 414ff5ca166SMax Reitz QLIST_FOREACH_SAFE(socket, &s->sockets, next, next_socket) { 415031fd1beSPeter Maydell do { 416ff5ca166SMax Reitz r = curl_multi_socket_action(s->s->multi, socket->fd, 0, &running); 417031fd1beSPeter Maydell } while (r == CURLM_CALL_MULTI_PERFORM); 418ff5ca166SMax Reitz } 419838ef602SMatthew Booth } 420838ef602SMatthew Booth 4219d456654SPaolo Bonzini static void curl_multi_do(void *arg) 4229d456654SPaolo Bonzini { 4239d456654SPaolo Bonzini CURLState *s = (CURLState *)arg; 4249d456654SPaolo Bonzini 425ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->s->mutex); 4269d456654SPaolo Bonzini curl_multi_do_locked(s); 427ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->s->mutex); 4289d456654SPaolo Bonzini } 4299d456654SPaolo Bonzini 430838ef602SMatthew Booth static void curl_multi_read(void *arg) 431838ef602SMatthew Booth { 432838ef602SMatthew Booth CURLState *s = (CURLState *)arg; 433838ef602SMatthew Booth 434ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->s->mutex); 4359d456654SPaolo Bonzini curl_multi_do_locked(s); 436838ef602SMatthew Booth curl_multi_check_completion(s->s); 437ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->s->mutex); 438031fd1beSPeter Maydell } 439031fd1beSPeter Maydell 440031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg) 441031fd1beSPeter Maydell { 442031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 443031fd1beSPeter Maydell BDRVCURLState *s = (BDRVCURLState *)arg; 444031fd1beSPeter Maydell int running; 445031fd1beSPeter Maydell 446031fd1beSPeter Maydell if (!s->multi) { 447031fd1beSPeter Maydell return; 448031fd1beSPeter Maydell } 449031fd1beSPeter Maydell 450ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->mutex); 451031fd1beSPeter Maydell curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 452031fd1beSPeter Maydell 453838ef602SMatthew Booth curl_multi_check_completion(s); 454ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 455031fd1beSPeter Maydell #else 456031fd1beSPeter Maydell abort(); 457031fd1beSPeter Maydell #endif 458031fd1beSPeter Maydell } 459031fd1beSPeter Maydell 460456af346SPaolo Bonzini /* Called with s->mutex held. */ 4613ce6a729SPaolo Bonzini static CURLState *curl_find_state(BDRVCURLState *s) 462769ce76dSAlexander Graf { 463769ce76dSAlexander Graf CURLState *state = NULL; 4643ce6a729SPaolo Bonzini int i; 465769ce76dSAlexander Graf 466769ce76dSAlexander Graf for (i = 0; i < CURL_NUM_STATES; i++) { 4673ce6a729SPaolo Bonzini if (!s->states[i].in_use) { 468769ce76dSAlexander Graf state = &s->states[i]; 469769ce76dSAlexander Graf state->in_use = 1; 470769ce76dSAlexander Graf break; 471769ce76dSAlexander Graf } 472769ce76dSAlexander Graf } 4733ce6a729SPaolo Bonzini return state; 4743ce6a729SPaolo Bonzini } 475769ce76dSAlexander Graf 4763ce6a729SPaolo Bonzini static int curl_init_state(BDRVCURLState *s, CURLState *state) 4773ce6a729SPaolo Bonzini { 4789e550b32SMatthew Booth if (!state->curl) { 479769ce76dSAlexander Graf state->curl = curl_easy_init(); 4809e550b32SMatthew Booth if (!state->curl) { 4813ce6a729SPaolo Bonzini return -EIO; 4829e550b32SMatthew Booth } 483769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_URL, s->url); 48497a3ea57SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, 48597a3ea57SMatthew Booth (long) s->sslverify); 486a94f83d9SRichard W.M. Jones if (s->cookie) { 487a94f83d9SRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie); 488a94f83d9SRichard W.M. Jones } 489f76faedaSRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout); 4909e550b32SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, 4919e550b32SMatthew Booth (void *)curl_read_cb); 492769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); 493769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); 494769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); 495769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); 496769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); 497769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); 498f785a5aeSNicholas Thomas curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); 499769ce76dSAlexander Graf 5001bff9606SDaniel P. Berrange if (s->username) { 5011bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username); 5021bff9606SDaniel P. Berrange } 5031bff9606SDaniel P. Berrange if (s->password) { 5041bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password); 5051bff9606SDaniel P. Berrange } 5061bff9606SDaniel P. Berrange if (s->proxyusername) { 5071bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, 5081bff9606SDaniel P. Berrange CURLOPT_PROXYUSERNAME, s->proxyusername); 5091bff9606SDaniel P. Berrange } 5101bff9606SDaniel P. Berrange if (s->proxypassword) { 5111bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, 5121bff9606SDaniel P. Berrange CURLOPT_PROXYPASSWORD, s->proxypassword); 5131bff9606SDaniel P. Berrange } 5141bff9606SDaniel P. Berrange 515fb6d1bbdSStefan Hajnoczi /* Restrict supported protocols to avoid security issues in the more 516fb6d1bbdSStefan Hajnoczi * obscure protocols. For example, do not allow POP3/SMTP/IMAP see 517fb6d1bbdSStefan Hajnoczi * CVE-2013-0249. 5188a8f5840SStefan Hajnoczi * 5198a8f5840SStefan Hajnoczi * Restricting protocols is only supported from 7.19.4 upwards. 520fb6d1bbdSStefan Hajnoczi */ 5218a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304 522fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); 523fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); 5248a8f5840SStefan Hajnoczi #endif 525fb6d1bbdSStefan Hajnoczi 526769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE 527769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); 528769ce76dSAlexander Graf #endif 5299e550b32SMatthew Booth } 530769ce76dSAlexander Graf 531ff5ca166SMax Reitz QLIST_INIT(&state->sockets); 532769ce76dSAlexander Graf state->s = s; 533769ce76dSAlexander Graf 5343ce6a729SPaolo Bonzini return 0; 535769ce76dSAlexander Graf } 536769ce76dSAlexander Graf 537456af346SPaolo Bonzini /* Called with s->mutex held. */ 538769ce76dSAlexander Graf static void curl_clean_state(CURLState *s) 539769ce76dSAlexander Graf { 540675a7756SPaolo Bonzini int j; 541675a7756SPaolo Bonzini for (j = 0; j < CURL_NUM_ACB; j++) { 542675a7756SPaolo Bonzini assert(!s->acb[j]); 543675a7756SPaolo Bonzini } 544675a7756SPaolo Bonzini 545769ce76dSAlexander Graf if (s->s->multi) 546769ce76dSAlexander Graf curl_multi_remove_handle(s->s->multi, s->curl); 547ff5ca166SMax Reitz 548ff5ca166SMax Reitz while (!QLIST_EMPTY(&s->sockets)) { 549ff5ca166SMax Reitz CURLSocket *socket = QLIST_FIRST(&s->sockets); 550ff5ca166SMax Reitz 551ff5ca166SMax Reitz QLIST_REMOVE(socket, next); 552ff5ca166SMax Reitz g_free(socket); 553ff5ca166SMax Reitz } 554ff5ca166SMax Reitz 555769ce76dSAlexander Graf s->in_use = 0; 5562bb5c936SPaolo Bonzini 557709f2132SPaolo Bonzini qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex); 558769ce76dSAlexander Graf } 559769ce76dSAlexander Graf 5608e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options, 5618e6d58cdSKevin Wolf Error **errp) 562769ce76dSAlexander Graf { 56346f5ac20SEric Blake qdict_put_str(options, CURL_BLOCK_OPT_URL, filename); 5648e6d58cdSKevin Wolf } 5658e6d58cdSKevin Wolf 56663f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs) 56763f0f45fSStefan Hajnoczi { 56863f0f45fSStefan Hajnoczi BDRVCURLState *s = bs->opaque; 56963f0f45fSStefan Hajnoczi int i; 57063f0f45fSStefan Hajnoczi 571456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 57263f0f45fSStefan Hajnoczi for (i = 0; i < CURL_NUM_STATES; i++) { 57363f0f45fSStefan Hajnoczi if (s->states[i].in_use) { 57463f0f45fSStefan Hajnoczi curl_clean_state(&s->states[i]); 57563f0f45fSStefan Hajnoczi } 57663f0f45fSStefan Hajnoczi if (s->states[i].curl) { 57763f0f45fSStefan Hajnoczi curl_easy_cleanup(s->states[i].curl); 57863f0f45fSStefan Hajnoczi s->states[i].curl = NULL; 57963f0f45fSStefan Hajnoczi } 58063f0f45fSStefan Hajnoczi g_free(s->states[i].orig_buf); 58163f0f45fSStefan Hajnoczi s->states[i].orig_buf = NULL; 58263f0f45fSStefan Hajnoczi } 58363f0f45fSStefan Hajnoczi if (s->multi) { 58463f0f45fSStefan Hajnoczi curl_multi_cleanup(s->multi); 58563f0f45fSStefan Hajnoczi s->multi = NULL; 58663f0f45fSStefan Hajnoczi } 587456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 58863f0f45fSStefan Hajnoczi 58963f0f45fSStefan Hajnoczi timer_del(&s->timer); 59063f0f45fSStefan Hajnoczi } 59163f0f45fSStefan Hajnoczi 59263f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs, 59363f0f45fSStefan Hajnoczi AioContext *new_context) 59463f0f45fSStefan Hajnoczi { 59563f0f45fSStefan Hajnoczi BDRVCURLState *s = bs->opaque; 59663f0f45fSStefan Hajnoczi 59763f0f45fSStefan Hajnoczi aio_timer_init(new_context, &s->timer, 59863f0f45fSStefan Hajnoczi QEMU_CLOCK_REALTIME, SCALE_NS, 59963f0f45fSStefan Hajnoczi curl_multi_timeout_do, s); 60063f0f45fSStefan Hajnoczi 60163f0f45fSStefan Hajnoczi assert(!s->multi); 60263f0f45fSStefan Hajnoczi s->multi = curl_multi_init(); 60363f0f45fSStefan Hajnoczi s->aio_context = new_context; 60463f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); 60563f0f45fSStefan Hajnoczi #ifdef NEED_CURL_TIMER_CALLBACK 60663f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); 60763f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); 60863f0f45fSStefan Hajnoczi #endif 60963f0f45fSStefan Hajnoczi } 61063f0f45fSStefan Hajnoczi 6118e6d58cdSKevin Wolf static QemuOptsList runtime_opts = { 6128e6d58cdSKevin Wolf .name = "curl", 6138e6d58cdSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 6148e6d58cdSKevin Wolf .desc = { 6158e6d58cdSKevin Wolf { 616e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_URL, 6178e6d58cdSKevin Wolf .type = QEMU_OPT_STRING, 6188e6d58cdSKevin Wolf .help = "URL to open", 6198e6d58cdSKevin Wolf }, 6208e6d58cdSKevin Wolf { 621e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_READAHEAD, 6228e6d58cdSKevin Wolf .type = QEMU_OPT_SIZE, 6238e6d58cdSKevin Wolf .help = "Readahead size", 6248e6d58cdSKevin Wolf }, 62597a3ea57SMatthew Booth { 62697a3ea57SMatthew Booth .name = CURL_BLOCK_OPT_SSLVERIFY, 62797a3ea57SMatthew Booth .type = QEMU_OPT_BOOL, 62897a3ea57SMatthew Booth .help = "Verify SSL certificate" 62997a3ea57SMatthew Booth }, 630212aefaaSDaniel Henrique Barboza { 631212aefaaSDaniel Henrique Barboza .name = CURL_BLOCK_OPT_TIMEOUT, 632212aefaaSDaniel Henrique Barboza .type = QEMU_OPT_NUMBER, 633212aefaaSDaniel Henrique Barboza .help = "Curl timeout" 634212aefaaSDaniel Henrique Barboza }, 635a94f83d9SRichard W.M. Jones { 636a94f83d9SRichard W.M. Jones .name = CURL_BLOCK_OPT_COOKIE, 637a94f83d9SRichard W.M. Jones .type = QEMU_OPT_STRING, 638a94f83d9SRichard W.M. Jones .help = "Pass the cookie or list of cookies with each request" 639a94f83d9SRichard W.M. Jones }, 6401bff9606SDaniel P. Berrange { 641327c8ebdSPeter Krempa .name = CURL_BLOCK_OPT_COOKIE_SECRET, 642327c8ebdSPeter Krempa .type = QEMU_OPT_STRING, 643327c8ebdSPeter Krempa .help = "ID of secret used as cookie passed with each request" 644327c8ebdSPeter Krempa }, 645327c8ebdSPeter Krempa { 6461bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_USERNAME, 6471bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6481bff9606SDaniel P. Berrange .help = "Username for HTTP auth" 6491bff9606SDaniel P. Berrange }, 6501bff9606SDaniel P. Berrange { 6511bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PASSWORD_SECRET, 6521bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6531bff9606SDaniel P. Berrange .help = "ID of secret used as password for HTTP auth", 6541bff9606SDaniel P. Berrange }, 6551bff9606SDaniel P. Berrange { 6561bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PROXY_USERNAME, 6571bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6581bff9606SDaniel P. Berrange .help = "Username for HTTP proxy auth" 6591bff9606SDaniel P. Berrange }, 6601bff9606SDaniel P. Berrange { 6611bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, 6621bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6631bff9606SDaniel P. Berrange .help = "ID of secret used as password for HTTP proxy auth", 6641bff9606SDaniel P. Berrange }, 6658e6d58cdSKevin Wolf { /* end of list */ } 6668e6d58cdSKevin Wolf }, 6678e6d58cdSKevin Wolf }; 6688e6d58cdSKevin Wolf 6691bff9606SDaniel P. Berrange 670015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags, 671015a1036SMax Reitz Error **errp) 6728e6d58cdSKevin Wolf { 6738e6d58cdSKevin Wolf BDRVCURLState *s = bs->opaque; 6748e6d58cdSKevin Wolf CURLState *state = NULL; 6758e6d58cdSKevin Wolf QemuOpts *opts; 6768e6d58cdSKevin Wolf Error *local_err = NULL; 6778e6d58cdSKevin Wolf const char *file; 678a94f83d9SRichard W.M. Jones const char *cookie; 679327c8ebdSPeter Krempa const char *cookie_secret; 6808e6d58cdSKevin Wolf double d; 6811bff9606SDaniel P. Berrange const char *secretid; 68234634ca2SMax Reitz const char *protocol_delimiter; 6832d25964dSJeff Cody int ret; 6848e6d58cdSKevin Wolf 6858e6d58cdSKevin Wolf 686a7cea2baSRichard W.M. Jones if (flags & BDRV_O_RDWR) { 6872a94fee3SPaolo Bonzini error_setg(errp, "curl block device does not support writes"); 688a7cea2baSRichard W.M. Jones return -EROFS; 689a7cea2baSRichard W.M. Jones } 690a7cea2baSRichard W.M. Jones 6912d25964dSJeff Cody if (!libcurl_initialized) { 6922d25964dSJeff Cody ret = curl_global_init(CURL_GLOBAL_ALL); 6932d25964dSJeff Cody if (ret) { 6942d25964dSJeff Cody error_setg(errp, "libcurl initialization failed with %d", ret); 6952d25964dSJeff Cody return -EIO; 6962d25964dSJeff Cody } 6972d25964dSJeff Cody libcurl_initialized = true; 6982d25964dSJeff Cody } 6992d25964dSJeff Cody 700456af346SPaolo Bonzini qemu_mutex_init(&s->mutex); 70187ea75d5SPeter Crosthwaite opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 7028e6d58cdSKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 70384d18f06SMarkus Armbruster if (local_err) { 7042a94fee3SPaolo Bonzini error_propagate(errp, local_err); 7058e6d58cdSKevin Wolf goto out_noclean; 7068e6d58cdSKevin Wolf } 7078e6d58cdSKevin Wolf 708e3542c67SMatthew Booth s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, 709e3542c67SMatthew Booth READ_AHEAD_DEFAULT); 710c76f4952SNolan if ((s->readahead_size & 0x1ff) != 0) { 7112a94fee3SPaolo Bonzini error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", 712c76f4952SNolan s->readahead_size); 713c76f4952SNolan goto out_noclean; 714c76f4952SNolan } 715c76f4952SNolan 716212aefaaSDaniel Henrique Barboza s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, 717212aefaaSDaniel Henrique Barboza CURL_TIMEOUT_DEFAULT); 718f76faedaSRichard W.M. Jones if (s->timeout > CURL_TIMEOUT_MAX) { 719f76faedaSRichard W.M. Jones error_setg(errp, "timeout parameter is too large or negative"); 720f76faedaSRichard W.M. Jones goto out_noclean; 721f76faedaSRichard W.M. Jones } 722212aefaaSDaniel Henrique Barboza 72397a3ea57SMatthew Booth s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true); 72497a3ea57SMatthew Booth 725a94f83d9SRichard W.M. Jones cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); 726327c8ebdSPeter Krempa cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET); 727327c8ebdSPeter Krempa 728327c8ebdSPeter Krempa if (cookie && cookie_secret) { 729327c8ebdSPeter Krempa error_setg(errp, 730327c8ebdSPeter Krempa "curl driver cannot handle both cookie and cookie secret"); 731327c8ebdSPeter Krempa goto out_noclean; 732327c8ebdSPeter Krempa } 733327c8ebdSPeter Krempa 734327c8ebdSPeter Krempa if (cookie_secret) { 735327c8ebdSPeter Krempa s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp); 736327c8ebdSPeter Krempa if (!s->cookie) { 737327c8ebdSPeter Krempa goto out_noclean; 738327c8ebdSPeter Krempa } 739327c8ebdSPeter Krempa } else { 740a94f83d9SRichard W.M. Jones s->cookie = g_strdup(cookie); 741327c8ebdSPeter Krempa } 742a94f83d9SRichard W.M. Jones 743e3542c67SMatthew Booth file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); 7448e6d58cdSKevin Wolf if (file == NULL) { 7452a94fee3SPaolo Bonzini error_setg(errp, "curl block driver requires an 'url' option"); 7468e6d58cdSKevin Wolf goto out_noclean; 7478e6d58cdSKevin Wolf } 7488e6d58cdSKevin Wolf 74934634ca2SMax Reitz if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) || 75034634ca2SMax Reitz !strstart(protocol_delimiter, "://", NULL)) 75134634ca2SMax Reitz { 75234634ca2SMax Reitz error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not " 75334634ca2SMax Reitz "start with '%s://')", bs->drv->protocol_name, file, 75434634ca2SMax Reitz bs->drv->protocol_name); 75534634ca2SMax Reitz goto out_noclean; 75634634ca2SMax Reitz } 75734634ca2SMax Reitz 7581bff9606SDaniel P. Berrange s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME)); 7591bff9606SDaniel P. Berrange secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET); 7601bff9606SDaniel P. Berrange 7611bff9606SDaniel P. Berrange if (secretid) { 7621bff9606SDaniel P. Berrange s->password = qcrypto_secret_lookup_as_utf8(secretid, errp); 7631bff9606SDaniel P. Berrange if (!s->password) { 7641bff9606SDaniel P. Berrange goto out_noclean; 7651bff9606SDaniel P. Berrange } 7661bff9606SDaniel P. Berrange } 7671bff9606SDaniel P. Berrange 7681bff9606SDaniel P. Berrange s->proxyusername = g_strdup( 7691bff9606SDaniel P. Berrange qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME)); 7701bff9606SDaniel P. Berrange secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET); 7711bff9606SDaniel P. Berrange if (secretid) { 7721bff9606SDaniel P. Berrange s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp); 7731bff9606SDaniel P. Berrange if (!s->proxypassword) { 7741bff9606SDaniel P. Berrange goto out_noclean; 7751bff9606SDaniel P. Berrange } 7761bff9606SDaniel P. Berrange } 7771bff9606SDaniel P. Berrange 778d0f2c4c6Smalc DPRINTF("CURL: Opening %s\n", file); 779709f2132SPaolo Bonzini qemu_co_queue_init(&s->free_state_waitq); 78063f0f45fSStefan Hajnoczi s->aio_context = bdrv_get_aio_context(bs); 7818e6d58cdSKevin Wolf s->url = g_strdup(file); 782456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 7833ce6a729SPaolo Bonzini state = curl_find_state(s); 784456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 7853ce6a729SPaolo Bonzini if (!state) { 786769ce76dSAlexander Graf goto out_noclean; 7873ce6a729SPaolo Bonzini } 788769ce76dSAlexander Graf 789769ce76dSAlexander Graf // Get file size 790769ce76dSAlexander Graf 7913ce6a729SPaolo Bonzini if (curl_init_state(s, state) < 0) { 7923ce6a729SPaolo Bonzini goto out; 7933ce6a729SPaolo Bonzini } 7943ce6a729SPaolo Bonzini 7953494d650SFam Zheng s->accept_range = false; 796769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); 7973494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, 7983494d650SFam Zheng curl_header_cb); 7993494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); 800769ce76dSAlexander Graf if (curl_easy_perform(state->curl)) 801769ce76dSAlexander Graf goto out; 802a41c4578STomáš Golembiovský if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { 803769ce76dSAlexander Graf goto out; 804a41c4578STomáš Golembiovský } 805a41c4578STomáš Golembiovský /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not 806a41c4578STomáš Golembiovský * know or the size is zero. From 7.19.4 CURL returns -1 if size is not 807a41c4578STomáš Golembiovský * known and zero if it is realy zero-length file. */ 808a41c4578STomáš Golembiovský #if LIBCURL_VERSION_NUM >= 0x071304 809a41c4578STomáš Golembiovský if (d < 0) { 810a41c4578STomáš Golembiovský pstrcpy(state->errmsg, CURL_ERROR_SIZE, 811a41c4578STomáš Golembiovský "Server didn't report file size."); 812a41c4578STomáš Golembiovský goto out; 813a41c4578STomáš Golembiovský } 814a41c4578STomáš Golembiovský #else 815a41c4578STomáš Golembiovský if (d <= 0) { 816a41c4578STomáš Golembiovský pstrcpy(state->errmsg, CURL_ERROR_SIZE, 817a41c4578STomáš Golembiovský "Unknown file size or zero-length file."); 818a41c4578STomáš Golembiovský goto out; 819a41c4578STomáš Golembiovský } 820a41c4578STomáš Golembiovský #endif 821a41c4578STomáš Golembiovský 8222125e5eaSPaolo Bonzini s->len = d; 823a41c4578STomáš Golembiovský 8243494d650SFam Zheng if ((!strncasecmp(s->url, "http://", strlen("http://")) 8253494d650SFam Zheng || !strncasecmp(s->url, "https://", strlen("https://"))) 8263494d650SFam Zheng && !s->accept_range) { 8273494d650SFam Zheng pstrcpy(state->errmsg, CURL_ERROR_SIZE, 8283494d650SFam Zheng "Server does not support 'range' (byte ranges)."); 8293494d650SFam Zheng goto out; 8303494d650SFam Zheng } 8312125e5eaSPaolo Bonzini DPRINTF("CURL: Size = %" PRIu64 "\n", s->len); 832769ce76dSAlexander Graf 833456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 834769ce76dSAlexander Graf curl_clean_state(state); 835456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 836769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 837769ce76dSAlexander Graf state->curl = NULL; 838769ce76dSAlexander Graf 83963f0f45fSStefan Hajnoczi curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); 840769ce76dSAlexander Graf 8418e6d58cdSKevin Wolf qemu_opts_del(opts); 842769ce76dSAlexander Graf return 0; 843769ce76dSAlexander Graf 844769ce76dSAlexander Graf out: 845acd7fdc6SMaria Kustova error_setg(errp, "CURL: Error opening file: %s", state->errmsg); 846769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 847769ce76dSAlexander Graf state->curl = NULL; 848769ce76dSAlexander Graf out_noclean: 849456af346SPaolo Bonzini qemu_mutex_destroy(&s->mutex); 850a94f83d9SRichard W.M. Jones g_free(s->cookie); 8518e6d58cdSKevin Wolf g_free(s->url); 852996922deSJeff Cody g_free(s->username); 853996922deSJeff Cody g_free(s->proxyusername); 854996922deSJeff Cody g_free(s->proxypassword); 8558e6d58cdSKevin Wolf qemu_opts_del(opts); 856769ce76dSAlexander Graf return -EINVAL; 857769ce76dSAlexander Graf } 858769ce76dSAlexander Graf 85928256d82SPaolo Bonzini static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) 860769ce76dSAlexander Graf { 861769ce76dSAlexander Graf CURLState *state; 862b69cdef8SMatthew Booth int running; 863769ce76dSAlexander Graf 8641919631eSPaolo Bonzini BDRVCURLState *s = bs->opaque; 865769ce76dSAlexander Graf 8662125e5eaSPaolo Bonzini uint64_t start = acb->offset; 8672125e5eaSPaolo Bonzini uint64_t end; 868769ce76dSAlexander Graf 869ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->mutex); 8701919631eSPaolo Bonzini 871769ce76dSAlexander Graf // In case we have the requested data already (e.g. read-ahead), 872769ce76dSAlexander Graf // we can just call the callback and be done. 87328256d82SPaolo Bonzini if (curl_find_buf(s, start, acb->bytes, acb)) { 87434db05e7SPaolo Bonzini goto out; 875769ce76dSAlexander Graf } 876769ce76dSAlexander Graf 877769ce76dSAlexander Graf // No cache found, so let's start a new request 8783ce6a729SPaolo Bonzini for (;;) { 8793ce6a729SPaolo Bonzini state = curl_find_state(s); 8803ce6a729SPaolo Bonzini if (state) { 8813ce6a729SPaolo Bonzini break; 8823ce6a729SPaolo Bonzini } 883709f2132SPaolo Bonzini qemu_co_queue_wait(&s->free_state_waitq, &s->mutex); 8843ce6a729SPaolo Bonzini } 8853ce6a729SPaolo Bonzini 8863ce6a729SPaolo Bonzini if (curl_init_state(s, state) < 0) { 8873ce6a729SPaolo Bonzini curl_clean_state(state); 88828256d82SPaolo Bonzini acb->ret = -EIO; 8891919631eSPaolo Bonzini goto out; 890363c3c85SNick Thomas } 891769ce76dSAlexander Graf 892769ce76dSAlexander Graf acb->start = 0; 8932125e5eaSPaolo Bonzini acb->end = MIN(acb->bytes, s->len - start); 894769ce76dSAlexander Graf 895769ce76dSAlexander Graf state->buf_off = 0; 8967267c094SAnthony Liguori g_free(state->orig_buf); 897769ce76dSAlexander Graf state->buf_start = start; 8984e504535SMax Reitz state->buf_len = MIN(acb->end + s->readahead_size, s->len - start); 8994e504535SMax Reitz end = start + state->buf_len - 1; 9008dc7a772SKevin Wolf state->orig_buf = g_try_malloc(state->buf_len); 9018dc7a772SKevin Wolf if (state->buf_len && state->orig_buf == NULL) { 9028dc7a772SKevin Wolf curl_clean_state(state); 90328256d82SPaolo Bonzini acb->ret = -ENOMEM; 9041919631eSPaolo Bonzini goto out; 9058dc7a772SKevin Wolf } 906769ce76dSAlexander Graf state->acb[0] = acb; 907769ce76dSAlexander Graf 9082125e5eaSPaolo Bonzini snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end); 9092125e5eaSPaolo Bonzini DPRINTF("CURL (AIO): Reading %" PRIu64 " at %" PRIu64 " (%s)\n", 9102125e5eaSPaolo Bonzini acb->bytes, start, state->range); 911769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); 912769ce76dSAlexander Graf 913769ce76dSAlexander Graf curl_multi_add_handle(s->multi, state->curl); 914769ce76dSAlexander Graf 915b69cdef8SMatthew Booth /* Tell curl it needs to kick things off */ 916b69cdef8SMatthew Booth curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 9171919631eSPaolo Bonzini 9181919631eSPaolo Bonzini out: 919ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 920363c3c85SNick Thomas } 921363c3c85SNick Thomas 92228256d82SPaolo Bonzini static int coroutine_fn curl_co_preadv(BlockDriverState *bs, 92328256d82SPaolo Bonzini uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) 924363c3c85SNick Thomas { 92528256d82SPaolo Bonzini CURLAIOCB acb = { 92628256d82SPaolo Bonzini .co = qemu_coroutine_self(), 92728256d82SPaolo Bonzini .ret = -EINPROGRESS, 92828256d82SPaolo Bonzini .qiov = qiov, 92928256d82SPaolo Bonzini .offset = offset, 93028256d82SPaolo Bonzini .bytes = bytes 93128256d82SPaolo Bonzini }; 932363c3c85SNick Thomas 93328256d82SPaolo Bonzini curl_setup_preadv(bs, &acb); 93428256d82SPaolo Bonzini while (acb.ret == -EINPROGRESS) { 93528256d82SPaolo Bonzini qemu_coroutine_yield(); 93628256d82SPaolo Bonzini } 93728256d82SPaolo Bonzini return acb.ret; 938769ce76dSAlexander Graf } 939769ce76dSAlexander Graf 940769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs) 941769ce76dSAlexander Graf { 942769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 943769ce76dSAlexander Graf 944d0f2c4c6Smalc DPRINTF("CURL: Close\n"); 94563f0f45fSStefan Hajnoczi curl_detach_aio_context(bs); 946ba3186c4SPaolo Bonzini qemu_mutex_destroy(&s->mutex); 947031fd1beSPeter Maydell 948a94f83d9SRichard W.M. Jones g_free(s->cookie); 94945724d6dSStefan Weil g_free(s->url); 950996922deSJeff Cody g_free(s->username); 951996922deSJeff Cody g_free(s->proxyusername); 952996922deSJeff Cody g_free(s->proxypassword); 953769ce76dSAlexander Graf } 954769ce76dSAlexander Graf 955769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs) 956769ce76dSAlexander Graf { 957769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 958769ce76dSAlexander Graf return s->len; 959769ce76dSAlexander Graf } 960769ce76dSAlexander Graf 961769ce76dSAlexander Graf static BlockDriver bdrv_http = { 962769ce76dSAlexander Graf .format_name = "http", 963769ce76dSAlexander Graf .protocol_name = "http", 964769ce76dSAlexander Graf 965769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 9668e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 96766f82ceeSKevin Wolf .bdrv_file_open = curl_open, 968769ce76dSAlexander Graf .bdrv_close = curl_close, 969769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 970769ce76dSAlexander Graf 97128256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 97263f0f45fSStefan Hajnoczi 97363f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 97463f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 975769ce76dSAlexander Graf }; 976769ce76dSAlexander Graf 977769ce76dSAlexander Graf static BlockDriver bdrv_https = { 978769ce76dSAlexander Graf .format_name = "https", 979769ce76dSAlexander Graf .protocol_name = "https", 980769ce76dSAlexander Graf 981769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 9828e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 98366f82ceeSKevin Wolf .bdrv_file_open = curl_open, 984769ce76dSAlexander Graf .bdrv_close = curl_close, 985769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 986769ce76dSAlexander Graf 98728256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 98863f0f45fSStefan Hajnoczi 98963f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 99063f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 991769ce76dSAlexander Graf }; 992769ce76dSAlexander Graf 993769ce76dSAlexander Graf static BlockDriver bdrv_ftp = { 994769ce76dSAlexander Graf .format_name = "ftp", 995769ce76dSAlexander Graf .protocol_name = "ftp", 996769ce76dSAlexander Graf 997769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 9988e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 99966f82ceeSKevin Wolf .bdrv_file_open = curl_open, 1000769ce76dSAlexander Graf .bdrv_close = curl_close, 1001769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 1002769ce76dSAlexander Graf 100328256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 100463f0f45fSStefan Hajnoczi 100563f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 100663f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 1007769ce76dSAlexander Graf }; 1008769ce76dSAlexander Graf 1009769ce76dSAlexander Graf static BlockDriver bdrv_ftps = { 1010769ce76dSAlexander Graf .format_name = "ftps", 1011769ce76dSAlexander Graf .protocol_name = "ftps", 1012769ce76dSAlexander Graf 1013769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 10148e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 101566f82ceeSKevin Wolf .bdrv_file_open = curl_open, 1016769ce76dSAlexander Graf .bdrv_close = curl_close, 1017769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 1018769ce76dSAlexander Graf 101928256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 102063f0f45fSStefan Hajnoczi 102163f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 102263f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 1023769ce76dSAlexander Graf }; 1024769ce76dSAlexander Graf 1025769ce76dSAlexander Graf static void curl_block_init(void) 1026769ce76dSAlexander Graf { 1027769ce76dSAlexander Graf bdrv_register(&bdrv_http); 1028769ce76dSAlexander Graf bdrv_register(&bdrv_https); 1029769ce76dSAlexander Graf bdrv_register(&bdrv_ftp); 1030769ce76dSAlexander Graf bdrv_register(&bdrv_ftps); 1031769ce76dSAlexander Graf } 1032769ce76dSAlexander Graf 1033769ce76dSAlexander Graf block_init(curl_block_init); 1034