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 */ 24*452fcdbcSMarkus Armbruster 2580c71a24SPeter Maydell #include "qemu/osdep.h" 26da34e65cSMarkus Armbruster #include "qapi/error.h" 27769ce76dSAlexander Graf #include "qemu-common.h" 28796a060bSRichard W.M. Jones #include "qemu/error-report.h" 29737e150eSPaolo Bonzini #include "block/block_int.h" 3097a3ea57SMatthew Booth #include "qapi/qmp/qbool.h" 31*452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 32d49b6836SMarkus Armbruster #include "qapi/qmp/qstring.h" 331bff9606SDaniel P. Berrange #include "crypto/secret.h" 34769ce76dSAlexander Graf #include <curl/curl.h> 35f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 36769ce76dSAlexander Graf 3741c23467SRichard W.M. Jones // #define DEBUG_CURL 38769ce76dSAlexander Graf // #define DEBUG_VERBOSE 39769ce76dSAlexander Graf 40769ce76dSAlexander Graf #ifdef DEBUG_CURL 41ed79f37dSZhou Jie #define DEBUG_CURL_PRINT 1 42769ce76dSAlexander Graf #else 43ed79f37dSZhou Jie #define DEBUG_CURL_PRINT 0 44769ce76dSAlexander Graf #endif 45ed79f37dSZhou Jie #define DPRINTF(fmt, ...) \ 46ed79f37dSZhou Jie do { \ 47ed79f37dSZhou Jie if (DEBUG_CURL_PRINT) { \ 48ed79f37dSZhou Jie fprintf(stderr, fmt, ## __VA_ARGS__); \ 49ed79f37dSZhou Jie } \ 50ed79f37dSZhou Jie } while (0) 51769ce76dSAlexander Graf 52031fd1beSPeter Maydell #if LIBCURL_VERSION_NUM >= 0x071000 53031fd1beSPeter Maydell /* The multi interface timer callback was introduced in 7.16.0 */ 54031fd1beSPeter Maydell #define NEED_CURL_TIMER_CALLBACK 559aedd5a5SMatthew Booth #define HAVE_SOCKET_ACTION 569aedd5a5SMatthew Booth #endif 579aedd5a5SMatthew Booth 589aedd5a5SMatthew Booth #ifndef HAVE_SOCKET_ACTION 599aedd5a5SMatthew Booth /* If curl_multi_socket_action isn't available, define it statically here in 609aedd5a5SMatthew Booth * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is 619aedd5a5SMatthew Booth * less efficient but still safe. */ 629aedd5a5SMatthew Booth static CURLMcode __curl_multi_socket_action(CURLM *multi_handle, 639aedd5a5SMatthew Booth curl_socket_t sockfd, 649aedd5a5SMatthew Booth int ev_bitmask, 659aedd5a5SMatthew Booth int *running_handles) 669aedd5a5SMatthew Booth { 679aedd5a5SMatthew Booth return curl_multi_socket(multi_handle, sockfd, running_handles); 689aedd5a5SMatthew Booth } 699aedd5a5SMatthew Booth #define curl_multi_socket_action __curl_multi_socket_action 70031fd1beSPeter Maydell #endif 71031fd1beSPeter Maydell 72fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ 7323dce387SMax Reitz CURLPROTO_FTP | CURLPROTO_FTPS) 74fb6d1bbdSStefan Hajnoczi 75769ce76dSAlexander Graf #define CURL_NUM_STATES 8 76769ce76dSAlexander Graf #define CURL_NUM_ACB 8 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 81e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL "url" 82e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead" 8397a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" 84212aefaaSDaniel Henrique Barboza #define CURL_BLOCK_OPT_TIMEOUT "timeout" 85a94f83d9SRichard W.M. Jones #define CURL_BLOCK_OPT_COOKIE "cookie" 86327c8ebdSPeter Krempa #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret" 871bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_USERNAME "username" 881bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret" 891bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username" 901bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret" 91e3542c67SMatthew Booth 92769ce76dSAlexander Graf struct BDRVCURLState; 93769ce76dSAlexander Graf 942d25964dSJeff Cody static bool libcurl_initialized; 952d25964dSJeff Cody 96769ce76dSAlexander Graf typedef struct CURLAIOCB { 9728256d82SPaolo Bonzini Coroutine *co; 98769ce76dSAlexander Graf QEMUIOVector *qiov; 99363c3c85SNick Thomas 1002125e5eaSPaolo Bonzini uint64_t offset; 1012125e5eaSPaolo Bonzini uint64_t bytes; 10228256d82SPaolo Bonzini int ret; 103363c3c85SNick Thomas 104769ce76dSAlexander Graf size_t start; 105769ce76dSAlexander Graf size_t end; 106769ce76dSAlexander Graf } CURLAIOCB; 107769ce76dSAlexander Graf 108ff5ca166SMax Reitz typedef struct CURLSocket { 109ff5ca166SMax Reitz int fd; 110ff5ca166SMax Reitz QLIST_ENTRY(CURLSocket) next; 111ff5ca166SMax Reitz } CURLSocket; 112ff5ca166SMax Reitz 113769ce76dSAlexander Graf typedef struct CURLState 114769ce76dSAlexander Graf { 115769ce76dSAlexander Graf struct BDRVCURLState *s; 116769ce76dSAlexander Graf CURLAIOCB *acb[CURL_NUM_ACB]; 117769ce76dSAlexander Graf CURL *curl; 118ff5ca166SMax Reitz QLIST_HEAD(, CURLSocket) sockets; 119769ce76dSAlexander Graf char *orig_buf; 1202125e5eaSPaolo Bonzini uint64_t buf_start; 121769ce76dSAlexander Graf size_t buf_off; 122769ce76dSAlexander Graf size_t buf_len; 123769ce76dSAlexander Graf char range[128]; 124769ce76dSAlexander Graf char errmsg[CURL_ERROR_SIZE]; 125769ce76dSAlexander Graf char in_use; 126769ce76dSAlexander Graf } CURLState; 127769ce76dSAlexander Graf 128769ce76dSAlexander Graf typedef struct BDRVCURLState { 129769ce76dSAlexander Graf CURLM *multi; 130031fd1beSPeter Maydell QEMUTimer timer; 1312125e5eaSPaolo Bonzini uint64_t len; 132769ce76dSAlexander Graf CURLState states[CURL_NUM_STATES]; 133769ce76dSAlexander Graf char *url; 134c76f4952SNolan size_t readahead_size; 13597a3ea57SMatthew Booth bool sslverify; 136f76faedaSRichard W.M. Jones uint64_t timeout; 137a94f83d9SRichard W.M. Jones char *cookie; 1383494d650SFam Zheng bool accept_range; 13963f0f45fSStefan Hajnoczi AioContext *aio_context; 140ba3186c4SPaolo Bonzini QemuMutex mutex; 141709f2132SPaolo Bonzini CoQueue free_state_waitq; 1421bff9606SDaniel P. Berrange char *username; 1431bff9606SDaniel P. Berrange char *password; 1441bff9606SDaniel P. Berrange char *proxyusername; 1451bff9606SDaniel P. Berrange char *proxypassword; 146769ce76dSAlexander Graf } BDRVCURLState; 147769ce76dSAlexander Graf 148769ce76dSAlexander Graf static void curl_clean_state(CURLState *s); 149769ce76dSAlexander Graf static void curl_multi_do(void *arg); 150838ef602SMatthew Booth static void curl_multi_read(void *arg); 151769ce76dSAlexander Graf 152031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 15334db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 154031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) 155031fd1beSPeter Maydell { 156031fd1beSPeter Maydell BDRVCURLState *s = opaque; 157031fd1beSPeter Maydell 158031fd1beSPeter Maydell DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms); 159031fd1beSPeter Maydell if (timeout_ms == -1) { 160031fd1beSPeter Maydell timer_del(&s->timer); 161031fd1beSPeter Maydell } else { 162031fd1beSPeter Maydell int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; 163031fd1beSPeter Maydell timer_mod(&s->timer, 164031fd1beSPeter Maydell qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); 165031fd1beSPeter Maydell } 166031fd1beSPeter Maydell return 0; 167031fd1beSPeter Maydell } 168031fd1beSPeter Maydell #endif 169031fd1beSPeter Maydell 17034db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 171769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, 17263f0f45fSStefan Hajnoczi void *userp, void *sp) 173769ce76dSAlexander Graf { 17463f0f45fSStefan Hajnoczi BDRVCURLState *s; 175838ef602SMatthew Booth CURLState *state = NULL; 176ff5ca166SMax Reitz CURLSocket *socket; 177ff5ca166SMax Reitz 178838ef602SMatthew Booth curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); 17963f0f45fSStefan Hajnoczi s = state->s; 180838ef602SMatthew Booth 181ff5ca166SMax Reitz QLIST_FOREACH(socket, &state->sockets, next) { 182ff5ca166SMax Reitz if (socket->fd == fd) { 183ff5ca166SMax Reitz if (action == CURL_POLL_REMOVE) { 184ff5ca166SMax Reitz QLIST_REMOVE(socket, next); 185ff5ca166SMax Reitz g_free(socket); 186ff5ca166SMax Reitz } 187ff5ca166SMax Reitz break; 188ff5ca166SMax Reitz } 189ff5ca166SMax Reitz } 190ff5ca166SMax Reitz if (!socket) { 191ff5ca166SMax Reitz socket = g_new0(CURLSocket, 1); 192ff5ca166SMax Reitz socket->fd = fd; 193ff5ca166SMax Reitz QLIST_INSERT_HEAD(&state->sockets, socket, next); 194ff5ca166SMax Reitz } 195ff5ca166SMax Reitz socket = NULL; 196ff5ca166SMax Reitz 19792b6a160SFam Zheng DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, (int)fd); 198769ce76dSAlexander Graf switch (action) { 199769ce76dSAlexander Graf case CURL_POLL_IN: 200dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 201f6a51c84SStefan Hajnoczi curl_multi_read, NULL, NULL, state); 202769ce76dSAlexander Graf break; 203769ce76dSAlexander Graf case CURL_POLL_OUT: 204dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 205f6a51c84SStefan Hajnoczi NULL, curl_multi_do, NULL, state); 206769ce76dSAlexander Graf break; 207769ce76dSAlexander Graf case CURL_POLL_INOUT: 208dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 209f6a51c84SStefan Hajnoczi curl_multi_read, curl_multi_do, NULL, state); 210769ce76dSAlexander Graf break; 211769ce76dSAlexander Graf case CURL_POLL_REMOVE: 212dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 213f6a51c84SStefan Hajnoczi NULL, NULL, NULL, NULL); 214769ce76dSAlexander Graf break; 215769ce76dSAlexander Graf } 216769ce76dSAlexander Graf 217769ce76dSAlexander Graf return 0; 218769ce76dSAlexander Graf } 219769ce76dSAlexander Graf 22034db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 2213494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 222769ce76dSAlexander Graf { 2233494d650SFam Zheng BDRVCURLState *s = opaque; 224769ce76dSAlexander Graf size_t realsize = size * nmemb; 2253494d650SFam Zheng const char *accept_line = "Accept-Ranges: bytes"; 226769ce76dSAlexander Graf 2273494d650SFam Zheng if (realsize >= strlen(accept_line) 2283494d650SFam Zheng && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) { 2293494d650SFam Zheng s->accept_range = true; 2300bfcd599SBlue Swirl } 231769ce76dSAlexander Graf 232769ce76dSAlexander Graf return realsize; 233769ce76dSAlexander Graf } 234769ce76dSAlexander Graf 23534db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 236769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 237769ce76dSAlexander Graf { 238769ce76dSAlexander Graf CURLState *s = ((CURLState*)opaque); 239769ce76dSAlexander Graf size_t realsize = size * nmemb; 240769ce76dSAlexander Graf int i; 241769ce76dSAlexander Graf 2420bfcd599SBlue Swirl DPRINTF("CURL: Just reading %zd bytes\n", realsize); 243769ce76dSAlexander Graf 2444e767657SMax Reitz if (!s || !s->orig_buf) { 2454e767657SMax Reitz goto read_end; 2464e767657SMax Reitz } 247769ce76dSAlexander Graf 2486d4b9e55SFam Zheng if (s->buf_off >= s->buf_len) { 2496d4b9e55SFam Zheng /* buffer full, read nothing */ 2504e767657SMax Reitz goto read_end; 2516d4b9e55SFam Zheng } 2526d4b9e55SFam Zheng realsize = MIN(realsize, s->buf_len - s->buf_off); 253769ce76dSAlexander Graf memcpy(s->orig_buf + s->buf_off, ptr, realsize); 254769ce76dSAlexander Graf s->buf_off += realsize; 255769ce76dSAlexander Graf 256769ce76dSAlexander Graf for(i=0; i<CURL_NUM_ACB; i++) { 257769ce76dSAlexander Graf CURLAIOCB *acb = s->acb[i]; 258769ce76dSAlexander Graf 259769ce76dSAlexander Graf if (!acb) 260769ce76dSAlexander Graf continue; 261769ce76dSAlexander Graf 262769ce76dSAlexander Graf if ((s->buf_off >= acb->end)) { 2632125e5eaSPaolo Bonzini size_t request_length = acb->bytes; 2644e504535SMax Reitz 26503396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start, 266769ce76dSAlexander Graf acb->end - acb->start); 2674e504535SMax Reitz 2684e504535SMax Reitz if (acb->end - acb->start < request_length) { 2694e504535SMax Reitz size_t offset = acb->end - acb->start; 2704e504535SMax Reitz qemu_iovec_memset(acb->qiov, offset, 0, 2714e504535SMax Reitz request_length - offset); 2724e504535SMax Reitz } 2734e504535SMax Reitz 27428256d82SPaolo Bonzini acb->ret = 0; 275769ce76dSAlexander Graf s->acb[i] = NULL; 27628256d82SPaolo Bonzini qemu_mutex_unlock(&s->s->mutex); 27728256d82SPaolo Bonzini aio_co_wake(acb->co); 27828256d82SPaolo Bonzini qemu_mutex_lock(&s->s->mutex); 279769ce76dSAlexander Graf } 280769ce76dSAlexander Graf } 281769ce76dSAlexander Graf 2824e767657SMax Reitz read_end: 2834e767657SMax Reitz /* curl will error out if we do not return this value */ 2844e767657SMax Reitz return size * nmemb; 285769ce76dSAlexander Graf } 286769ce76dSAlexander Graf 287456af346SPaolo Bonzini /* Called with s->mutex held. */ 28828256d82SPaolo Bonzini static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len, 289769ce76dSAlexander Graf CURLAIOCB *acb) 290769ce76dSAlexander Graf { 291769ce76dSAlexander Graf int i; 2922125e5eaSPaolo Bonzini uint64_t end = start + len; 2932125e5eaSPaolo Bonzini uint64_t clamped_end = MIN(end, s->len); 2942125e5eaSPaolo Bonzini uint64_t clamped_len = clamped_end - start; 295769ce76dSAlexander Graf 296769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 297769ce76dSAlexander Graf CURLState *state = &s->states[i]; 2982125e5eaSPaolo Bonzini uint64_t buf_end = (state->buf_start + state->buf_off); 2992125e5eaSPaolo Bonzini uint64_t buf_fend = (state->buf_start + state->buf_len); 300769ce76dSAlexander Graf 301769ce76dSAlexander Graf if (!state->orig_buf) 302769ce76dSAlexander Graf continue; 303769ce76dSAlexander Graf if (!state->buf_off) 304769ce76dSAlexander Graf continue; 305769ce76dSAlexander Graf 306769ce76dSAlexander Graf // Does the existing buffer cover our section? 307769ce76dSAlexander Graf if ((start >= state->buf_start) && 308769ce76dSAlexander Graf (start <= buf_end) && 3094e504535SMax Reitz (clamped_end >= state->buf_start) && 3104e504535SMax Reitz (clamped_end <= buf_end)) 311769ce76dSAlexander Graf { 312769ce76dSAlexander Graf char *buf = state->orig_buf + (start - state->buf_start); 313769ce76dSAlexander Graf 3144e504535SMax Reitz qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len); 3154e504535SMax Reitz if (clamped_len < len) { 3164e504535SMax Reitz qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len); 3174e504535SMax Reitz } 31828256d82SPaolo Bonzini acb->ret = 0; 31928256d82SPaolo Bonzini return true; 320769ce76dSAlexander Graf } 321769ce76dSAlexander Graf 322769ce76dSAlexander Graf // Wait for unfinished chunks 323b7079df4SMatthew Booth if (state->in_use && 324b7079df4SMatthew Booth (start >= state->buf_start) && 325769ce76dSAlexander Graf (start <= buf_fend) && 3264e504535SMax Reitz (clamped_end >= state->buf_start) && 3274e504535SMax Reitz (clamped_end <= buf_fend)) 328769ce76dSAlexander Graf { 329769ce76dSAlexander Graf int j; 330769ce76dSAlexander Graf 331769ce76dSAlexander Graf acb->start = start - state->buf_start; 3324e504535SMax Reitz acb->end = acb->start + clamped_len; 333769ce76dSAlexander Graf 334769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) { 335769ce76dSAlexander Graf if (!state->acb[j]) { 336769ce76dSAlexander Graf state->acb[j] = acb; 33728256d82SPaolo Bonzini return true; 338769ce76dSAlexander Graf } 339769ce76dSAlexander Graf } 340769ce76dSAlexander Graf } 341769ce76dSAlexander Graf } 342769ce76dSAlexander Graf 34328256d82SPaolo Bonzini return false; 344769ce76dSAlexander Graf } 345769ce76dSAlexander Graf 346ba3186c4SPaolo Bonzini /* Called with s->mutex held. */ 347838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s) 348769ce76dSAlexander Graf { 349769ce76dSAlexander Graf int msgs_in_queue; 350769ce76dSAlexander Graf 351769ce76dSAlexander Graf /* Try to find done transfers, so we can free the easy 352769ce76dSAlexander Graf * handle again. */ 3531f2cead3SMatthew Booth for (;;) { 354769ce76dSAlexander Graf CURLMsg *msg; 355769ce76dSAlexander Graf msg = curl_multi_info_read(s->multi, &msgs_in_queue); 356769ce76dSAlexander Graf 3571f2cead3SMatthew Booth /* Quit when there are no more completions */ 358769ce76dSAlexander Graf if (!msg) 359769ce76dSAlexander Graf break; 360769ce76dSAlexander Graf 3611f2cead3SMatthew Booth if (msg->msg == CURLMSG_DONE) { 362769ce76dSAlexander Graf CURLState *state = NULL; 363f6246509SMatthew Booth curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, 364f6246509SMatthew Booth (char **)&state); 365f785a5aeSNicholas Thomas 366f785a5aeSNicholas Thomas /* ACBs for successful messages get completed in curl_read_cb */ 367f785a5aeSNicholas Thomas if (msg->data.result != CURLE_OK) { 368f785a5aeSNicholas Thomas int i; 369796a060bSRichard W.M. Jones static int errcount = 100; 370796a060bSRichard W.M. Jones 371796a060bSRichard W.M. Jones /* Don't lose the original error message from curl, since 372796a060bSRichard W.M. Jones * it contains extra data. 373796a060bSRichard W.M. Jones */ 374796a060bSRichard W.M. Jones if (errcount > 0) { 375796a060bSRichard W.M. Jones error_report("curl: %s", state->errmsg); 376796a060bSRichard W.M. Jones if (--errcount == 0) { 377796a060bSRichard W.M. Jones error_report("curl: further errors suppressed"); 378796a060bSRichard W.M. Jones } 379796a060bSRichard W.M. Jones } 380796a060bSRichard W.M. Jones 381f785a5aeSNicholas Thomas for (i = 0; i < CURL_NUM_ACB; i++) { 382f785a5aeSNicholas Thomas CURLAIOCB *acb = state->acb[i]; 383f785a5aeSNicholas Thomas 384f785a5aeSNicholas Thomas if (acb == NULL) { 385f785a5aeSNicholas Thomas continue; 386f785a5aeSNicholas Thomas } 387f785a5aeSNicholas Thomas 38828256d82SPaolo Bonzini acb->ret = -EIO; 389f785a5aeSNicholas Thomas state->acb[i] = NULL; 39028256d82SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 39128256d82SPaolo Bonzini aio_co_wake(acb->co); 39228256d82SPaolo Bonzini qemu_mutex_lock(&s->mutex); 393f785a5aeSNicholas Thomas } 394f785a5aeSNicholas Thomas } 395f785a5aeSNicholas Thomas 396769ce76dSAlexander Graf curl_clean_state(state); 397769ce76dSAlexander Graf break; 398769ce76dSAlexander Graf } 399769ce76dSAlexander Graf } 400769ce76dSAlexander Graf } 401769ce76dSAlexander Graf 402ba3186c4SPaolo Bonzini /* Called with s->mutex held. */ 4039d456654SPaolo Bonzini static void curl_multi_do_locked(CURLState *s) 404031fd1beSPeter Maydell { 405ff5ca166SMax Reitz CURLSocket *socket, *next_socket; 406031fd1beSPeter Maydell int running; 407031fd1beSPeter Maydell int r; 408031fd1beSPeter Maydell 409838ef602SMatthew Booth if (!s->s->multi) { 410031fd1beSPeter Maydell return; 411031fd1beSPeter Maydell } 412031fd1beSPeter Maydell 413ff5ca166SMax Reitz /* Need to use _SAFE because curl_multi_socket_action() may trigger 414ff5ca166SMax Reitz * curl_sock_cb() which might modify this list */ 415ff5ca166SMax Reitz QLIST_FOREACH_SAFE(socket, &s->sockets, next, next_socket) { 416031fd1beSPeter Maydell do { 417ff5ca166SMax Reitz r = curl_multi_socket_action(s->s->multi, socket->fd, 0, &running); 418031fd1beSPeter Maydell } while (r == CURLM_CALL_MULTI_PERFORM); 419ff5ca166SMax Reitz } 420838ef602SMatthew Booth } 421838ef602SMatthew Booth 4229d456654SPaolo Bonzini static void curl_multi_do(void *arg) 4239d456654SPaolo Bonzini { 4249d456654SPaolo Bonzini CURLState *s = (CURLState *)arg; 4259d456654SPaolo Bonzini 426ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->s->mutex); 4279d456654SPaolo Bonzini curl_multi_do_locked(s); 428ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->s->mutex); 4299d456654SPaolo Bonzini } 4309d456654SPaolo Bonzini 431838ef602SMatthew Booth static void curl_multi_read(void *arg) 432838ef602SMatthew Booth { 433838ef602SMatthew Booth CURLState *s = (CURLState *)arg; 434838ef602SMatthew Booth 435ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->s->mutex); 4369d456654SPaolo Bonzini curl_multi_do_locked(s); 437838ef602SMatthew Booth curl_multi_check_completion(s->s); 438ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->s->mutex); 439031fd1beSPeter Maydell } 440031fd1beSPeter Maydell 441031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg) 442031fd1beSPeter Maydell { 443031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 444031fd1beSPeter Maydell BDRVCURLState *s = (BDRVCURLState *)arg; 445031fd1beSPeter Maydell int running; 446031fd1beSPeter Maydell 447031fd1beSPeter Maydell if (!s->multi) { 448031fd1beSPeter Maydell return; 449031fd1beSPeter Maydell } 450031fd1beSPeter Maydell 451ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->mutex); 452031fd1beSPeter Maydell curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 453031fd1beSPeter Maydell 454838ef602SMatthew Booth curl_multi_check_completion(s); 455ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 456031fd1beSPeter Maydell #else 457031fd1beSPeter Maydell abort(); 458031fd1beSPeter Maydell #endif 459031fd1beSPeter Maydell } 460031fd1beSPeter Maydell 461456af346SPaolo Bonzini /* Called with s->mutex held. */ 4623ce6a729SPaolo Bonzini static CURLState *curl_find_state(BDRVCURLState *s) 463769ce76dSAlexander Graf { 464769ce76dSAlexander Graf CURLState *state = NULL; 4653ce6a729SPaolo Bonzini int i; 466769ce76dSAlexander Graf 467769ce76dSAlexander Graf for (i = 0; i < CURL_NUM_STATES; i++) { 4683ce6a729SPaolo Bonzini if (!s->states[i].in_use) { 469769ce76dSAlexander Graf state = &s->states[i]; 470769ce76dSAlexander Graf state->in_use = 1; 471769ce76dSAlexander Graf break; 472769ce76dSAlexander Graf } 473769ce76dSAlexander Graf } 4743ce6a729SPaolo Bonzini return state; 4753ce6a729SPaolo Bonzini } 476769ce76dSAlexander Graf 4773ce6a729SPaolo Bonzini static int curl_init_state(BDRVCURLState *s, CURLState *state) 4783ce6a729SPaolo Bonzini { 4799e550b32SMatthew Booth if (!state->curl) { 480769ce76dSAlexander Graf state->curl = curl_easy_init(); 4819e550b32SMatthew Booth if (!state->curl) { 4823ce6a729SPaolo Bonzini return -EIO; 4839e550b32SMatthew Booth } 484769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_URL, s->url); 48597a3ea57SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, 48697a3ea57SMatthew Booth (long) s->sslverify); 487a94f83d9SRichard W.M. Jones if (s->cookie) { 488a94f83d9SRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie); 489a94f83d9SRichard W.M. Jones } 490f76faedaSRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout); 4919e550b32SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, 4929e550b32SMatthew Booth (void *)curl_read_cb); 493769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); 494769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); 495769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); 496769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); 497769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); 498769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); 499f785a5aeSNicholas Thomas curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); 500769ce76dSAlexander Graf 5011bff9606SDaniel P. Berrange if (s->username) { 5021bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username); 5031bff9606SDaniel P. Berrange } 5041bff9606SDaniel P. Berrange if (s->password) { 5051bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password); 5061bff9606SDaniel P. Berrange } 5071bff9606SDaniel P. Berrange if (s->proxyusername) { 5081bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, 5091bff9606SDaniel P. Berrange CURLOPT_PROXYUSERNAME, s->proxyusername); 5101bff9606SDaniel P. Berrange } 5111bff9606SDaniel P. Berrange if (s->proxypassword) { 5121bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, 5131bff9606SDaniel P. Berrange CURLOPT_PROXYPASSWORD, s->proxypassword); 5141bff9606SDaniel P. Berrange } 5151bff9606SDaniel P. Berrange 516fb6d1bbdSStefan Hajnoczi /* Restrict supported protocols to avoid security issues in the more 517fb6d1bbdSStefan Hajnoczi * obscure protocols. For example, do not allow POP3/SMTP/IMAP see 518fb6d1bbdSStefan Hajnoczi * CVE-2013-0249. 5198a8f5840SStefan Hajnoczi * 5208a8f5840SStefan Hajnoczi * Restricting protocols is only supported from 7.19.4 upwards. 521fb6d1bbdSStefan Hajnoczi */ 5228a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304 523fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); 524fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); 5258a8f5840SStefan Hajnoczi #endif 526fb6d1bbdSStefan Hajnoczi 527769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE 528769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); 529769ce76dSAlexander Graf #endif 5309e550b32SMatthew Booth } 531769ce76dSAlexander Graf 532ff5ca166SMax Reitz QLIST_INIT(&state->sockets); 533769ce76dSAlexander Graf state->s = s; 534769ce76dSAlexander Graf 5353ce6a729SPaolo Bonzini return 0; 536769ce76dSAlexander Graf } 537769ce76dSAlexander Graf 538456af346SPaolo Bonzini /* Called with s->mutex held. */ 539769ce76dSAlexander Graf static void curl_clean_state(CURLState *s) 540769ce76dSAlexander Graf { 541675a7756SPaolo Bonzini int j; 542675a7756SPaolo Bonzini for (j = 0; j < CURL_NUM_ACB; j++) { 543675a7756SPaolo Bonzini assert(!s->acb[j]); 544675a7756SPaolo Bonzini } 545675a7756SPaolo Bonzini 546769ce76dSAlexander Graf if (s->s->multi) 547769ce76dSAlexander Graf curl_multi_remove_handle(s->s->multi, s->curl); 548ff5ca166SMax Reitz 549ff5ca166SMax Reitz while (!QLIST_EMPTY(&s->sockets)) { 550ff5ca166SMax Reitz CURLSocket *socket = QLIST_FIRST(&s->sockets); 551ff5ca166SMax Reitz 552ff5ca166SMax Reitz QLIST_REMOVE(socket, next); 553ff5ca166SMax Reitz g_free(socket); 554ff5ca166SMax Reitz } 555ff5ca166SMax Reitz 556769ce76dSAlexander Graf s->in_use = 0; 5572bb5c936SPaolo Bonzini 558709f2132SPaolo Bonzini qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex); 559769ce76dSAlexander Graf } 560769ce76dSAlexander Graf 5618e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options, 5628e6d58cdSKevin Wolf Error **errp) 563769ce76dSAlexander Graf { 56446f5ac20SEric Blake qdict_put_str(options, CURL_BLOCK_OPT_URL, filename); 5658e6d58cdSKevin Wolf } 5668e6d58cdSKevin Wolf 56763f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs) 56863f0f45fSStefan Hajnoczi { 56963f0f45fSStefan Hajnoczi BDRVCURLState *s = bs->opaque; 57063f0f45fSStefan Hajnoczi int i; 57163f0f45fSStefan Hajnoczi 572456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 57363f0f45fSStefan Hajnoczi for (i = 0; i < CURL_NUM_STATES; i++) { 57463f0f45fSStefan Hajnoczi if (s->states[i].in_use) { 57563f0f45fSStefan Hajnoczi curl_clean_state(&s->states[i]); 57663f0f45fSStefan Hajnoczi } 57763f0f45fSStefan Hajnoczi if (s->states[i].curl) { 57863f0f45fSStefan Hajnoczi curl_easy_cleanup(s->states[i].curl); 57963f0f45fSStefan Hajnoczi s->states[i].curl = NULL; 58063f0f45fSStefan Hajnoczi } 58163f0f45fSStefan Hajnoczi g_free(s->states[i].orig_buf); 58263f0f45fSStefan Hajnoczi s->states[i].orig_buf = NULL; 58363f0f45fSStefan Hajnoczi } 58463f0f45fSStefan Hajnoczi if (s->multi) { 58563f0f45fSStefan Hajnoczi curl_multi_cleanup(s->multi); 58663f0f45fSStefan Hajnoczi s->multi = NULL; 58763f0f45fSStefan Hajnoczi } 588456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 58963f0f45fSStefan Hajnoczi 59063f0f45fSStefan Hajnoczi timer_del(&s->timer); 59163f0f45fSStefan Hajnoczi } 59263f0f45fSStefan Hajnoczi 59363f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs, 59463f0f45fSStefan Hajnoczi AioContext *new_context) 59563f0f45fSStefan Hajnoczi { 59663f0f45fSStefan Hajnoczi BDRVCURLState *s = bs->opaque; 59763f0f45fSStefan Hajnoczi 59863f0f45fSStefan Hajnoczi aio_timer_init(new_context, &s->timer, 59963f0f45fSStefan Hajnoczi QEMU_CLOCK_REALTIME, SCALE_NS, 60063f0f45fSStefan Hajnoczi curl_multi_timeout_do, s); 60163f0f45fSStefan Hajnoczi 60263f0f45fSStefan Hajnoczi assert(!s->multi); 60363f0f45fSStefan Hajnoczi s->multi = curl_multi_init(); 60463f0f45fSStefan Hajnoczi s->aio_context = new_context; 60563f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); 60663f0f45fSStefan Hajnoczi #ifdef NEED_CURL_TIMER_CALLBACK 60763f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); 60863f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); 60963f0f45fSStefan Hajnoczi #endif 61063f0f45fSStefan Hajnoczi } 61163f0f45fSStefan Hajnoczi 6128e6d58cdSKevin Wolf static QemuOptsList runtime_opts = { 6138e6d58cdSKevin Wolf .name = "curl", 6148e6d58cdSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 6158e6d58cdSKevin Wolf .desc = { 6168e6d58cdSKevin Wolf { 617e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_URL, 6188e6d58cdSKevin Wolf .type = QEMU_OPT_STRING, 6198e6d58cdSKevin Wolf .help = "URL to open", 6208e6d58cdSKevin Wolf }, 6218e6d58cdSKevin Wolf { 622e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_READAHEAD, 6238e6d58cdSKevin Wolf .type = QEMU_OPT_SIZE, 6248e6d58cdSKevin Wolf .help = "Readahead size", 6258e6d58cdSKevin Wolf }, 62697a3ea57SMatthew Booth { 62797a3ea57SMatthew Booth .name = CURL_BLOCK_OPT_SSLVERIFY, 62897a3ea57SMatthew Booth .type = QEMU_OPT_BOOL, 62997a3ea57SMatthew Booth .help = "Verify SSL certificate" 63097a3ea57SMatthew Booth }, 631212aefaaSDaniel Henrique Barboza { 632212aefaaSDaniel Henrique Barboza .name = CURL_BLOCK_OPT_TIMEOUT, 633212aefaaSDaniel Henrique Barboza .type = QEMU_OPT_NUMBER, 634212aefaaSDaniel Henrique Barboza .help = "Curl timeout" 635212aefaaSDaniel Henrique Barboza }, 636a94f83d9SRichard W.M. Jones { 637a94f83d9SRichard W.M. Jones .name = CURL_BLOCK_OPT_COOKIE, 638a94f83d9SRichard W.M. Jones .type = QEMU_OPT_STRING, 639a94f83d9SRichard W.M. Jones .help = "Pass the cookie or list of cookies with each request" 640a94f83d9SRichard W.M. Jones }, 6411bff9606SDaniel P. Berrange { 642327c8ebdSPeter Krempa .name = CURL_BLOCK_OPT_COOKIE_SECRET, 643327c8ebdSPeter Krempa .type = QEMU_OPT_STRING, 644327c8ebdSPeter Krempa .help = "ID of secret used as cookie passed with each request" 645327c8ebdSPeter Krempa }, 646327c8ebdSPeter Krempa { 6471bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_USERNAME, 6481bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6491bff9606SDaniel P. Berrange .help = "Username for HTTP auth" 6501bff9606SDaniel P. Berrange }, 6511bff9606SDaniel P. Berrange { 6521bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PASSWORD_SECRET, 6531bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6541bff9606SDaniel P. Berrange .help = "ID of secret used as password for HTTP auth", 6551bff9606SDaniel P. Berrange }, 6561bff9606SDaniel P. Berrange { 6571bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PROXY_USERNAME, 6581bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6591bff9606SDaniel P. Berrange .help = "Username for HTTP proxy auth" 6601bff9606SDaniel P. Berrange }, 6611bff9606SDaniel P. Berrange { 6621bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, 6631bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6641bff9606SDaniel P. Berrange .help = "ID of secret used as password for HTTP proxy auth", 6651bff9606SDaniel P. Berrange }, 6668e6d58cdSKevin Wolf { /* end of list */ } 6678e6d58cdSKevin Wolf }, 6688e6d58cdSKevin Wolf }; 6698e6d58cdSKevin Wolf 6701bff9606SDaniel P. Berrange 671015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags, 672015a1036SMax Reitz Error **errp) 6738e6d58cdSKevin Wolf { 6748e6d58cdSKevin Wolf BDRVCURLState *s = bs->opaque; 6758e6d58cdSKevin Wolf CURLState *state = NULL; 6768e6d58cdSKevin Wolf QemuOpts *opts; 6778e6d58cdSKevin Wolf Error *local_err = NULL; 6788e6d58cdSKevin Wolf const char *file; 679a94f83d9SRichard W.M. Jones const char *cookie; 680327c8ebdSPeter Krempa const char *cookie_secret; 6818e6d58cdSKevin Wolf double d; 6821bff9606SDaniel P. Berrange const char *secretid; 68334634ca2SMax Reitz const char *protocol_delimiter; 6842d25964dSJeff Cody int ret; 6858e6d58cdSKevin Wolf 6868e6d58cdSKevin Wolf 687a7cea2baSRichard W.M. Jones if (flags & BDRV_O_RDWR) { 6882a94fee3SPaolo Bonzini error_setg(errp, "curl block device does not support writes"); 689a7cea2baSRichard W.M. Jones return -EROFS; 690a7cea2baSRichard W.M. Jones } 691a7cea2baSRichard W.M. Jones 6922d25964dSJeff Cody if (!libcurl_initialized) { 6932d25964dSJeff Cody ret = curl_global_init(CURL_GLOBAL_ALL); 6942d25964dSJeff Cody if (ret) { 6952d25964dSJeff Cody error_setg(errp, "libcurl initialization failed with %d", ret); 6962d25964dSJeff Cody return -EIO; 6972d25964dSJeff Cody } 6982d25964dSJeff Cody libcurl_initialized = true; 6992d25964dSJeff Cody } 7002d25964dSJeff Cody 701456af346SPaolo Bonzini qemu_mutex_init(&s->mutex); 70287ea75d5SPeter Crosthwaite opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 7038e6d58cdSKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 70484d18f06SMarkus Armbruster if (local_err) { 7052a94fee3SPaolo Bonzini error_propagate(errp, local_err); 7068e6d58cdSKevin Wolf goto out_noclean; 7078e6d58cdSKevin Wolf } 7088e6d58cdSKevin Wolf 709e3542c67SMatthew Booth s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, 710e3542c67SMatthew Booth READ_AHEAD_DEFAULT); 711c76f4952SNolan if ((s->readahead_size & 0x1ff) != 0) { 7122a94fee3SPaolo Bonzini error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", 713c76f4952SNolan s->readahead_size); 714c76f4952SNolan goto out_noclean; 715c76f4952SNolan } 716c76f4952SNolan 717212aefaaSDaniel Henrique Barboza s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, 718212aefaaSDaniel Henrique Barboza CURL_TIMEOUT_DEFAULT); 719f76faedaSRichard W.M. Jones if (s->timeout > CURL_TIMEOUT_MAX) { 720f76faedaSRichard W.M. Jones error_setg(errp, "timeout parameter is too large or negative"); 721f76faedaSRichard W.M. Jones goto out_noclean; 722f76faedaSRichard W.M. Jones } 723212aefaaSDaniel Henrique Barboza 72497a3ea57SMatthew Booth s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true); 72597a3ea57SMatthew Booth 726a94f83d9SRichard W.M. Jones cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); 727327c8ebdSPeter Krempa cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET); 728327c8ebdSPeter Krempa 729327c8ebdSPeter Krempa if (cookie && cookie_secret) { 730327c8ebdSPeter Krempa error_setg(errp, 731327c8ebdSPeter Krempa "curl driver cannot handle both cookie and cookie secret"); 732327c8ebdSPeter Krempa goto out_noclean; 733327c8ebdSPeter Krempa } 734327c8ebdSPeter Krempa 735327c8ebdSPeter Krempa if (cookie_secret) { 736327c8ebdSPeter Krempa s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp); 737327c8ebdSPeter Krempa if (!s->cookie) { 738327c8ebdSPeter Krempa goto out_noclean; 739327c8ebdSPeter Krempa } 740327c8ebdSPeter Krempa } else { 741a94f83d9SRichard W.M. Jones s->cookie = g_strdup(cookie); 742327c8ebdSPeter Krempa } 743a94f83d9SRichard W.M. Jones 744e3542c67SMatthew Booth file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); 7458e6d58cdSKevin Wolf if (file == NULL) { 7462a94fee3SPaolo Bonzini error_setg(errp, "curl block driver requires an 'url' option"); 7478e6d58cdSKevin Wolf goto out_noclean; 7488e6d58cdSKevin Wolf } 7498e6d58cdSKevin Wolf 75034634ca2SMax Reitz if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) || 75134634ca2SMax Reitz !strstart(protocol_delimiter, "://", NULL)) 75234634ca2SMax Reitz { 75334634ca2SMax Reitz error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not " 75434634ca2SMax Reitz "start with '%s://')", bs->drv->protocol_name, file, 75534634ca2SMax Reitz bs->drv->protocol_name); 75634634ca2SMax Reitz goto out_noclean; 75734634ca2SMax Reitz } 75834634ca2SMax Reitz 7591bff9606SDaniel P. Berrange s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME)); 7601bff9606SDaniel P. Berrange secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET); 7611bff9606SDaniel P. Berrange 7621bff9606SDaniel P. Berrange if (secretid) { 7631bff9606SDaniel P. Berrange s->password = qcrypto_secret_lookup_as_utf8(secretid, errp); 7641bff9606SDaniel P. Berrange if (!s->password) { 7651bff9606SDaniel P. Berrange goto out_noclean; 7661bff9606SDaniel P. Berrange } 7671bff9606SDaniel P. Berrange } 7681bff9606SDaniel P. Berrange 7691bff9606SDaniel P. Berrange s->proxyusername = g_strdup( 7701bff9606SDaniel P. Berrange qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME)); 7711bff9606SDaniel P. Berrange secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET); 7721bff9606SDaniel P. Berrange if (secretid) { 7731bff9606SDaniel P. Berrange s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp); 7741bff9606SDaniel P. Berrange if (!s->proxypassword) { 7751bff9606SDaniel P. Berrange goto out_noclean; 7761bff9606SDaniel P. Berrange } 7771bff9606SDaniel P. Berrange } 7781bff9606SDaniel P. Berrange 779d0f2c4c6Smalc DPRINTF("CURL: Opening %s\n", file); 780709f2132SPaolo Bonzini qemu_co_queue_init(&s->free_state_waitq); 78163f0f45fSStefan Hajnoczi s->aio_context = bdrv_get_aio_context(bs); 7828e6d58cdSKevin Wolf s->url = g_strdup(file); 783456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 7843ce6a729SPaolo Bonzini state = curl_find_state(s); 785456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 7863ce6a729SPaolo Bonzini if (!state) { 787769ce76dSAlexander Graf goto out_noclean; 7883ce6a729SPaolo Bonzini } 789769ce76dSAlexander Graf 790769ce76dSAlexander Graf // Get file size 791769ce76dSAlexander Graf 7923ce6a729SPaolo Bonzini if (curl_init_state(s, state) < 0) { 7933ce6a729SPaolo Bonzini goto out; 7943ce6a729SPaolo Bonzini } 7953ce6a729SPaolo Bonzini 7963494d650SFam Zheng s->accept_range = false; 797769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); 7983494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, 7993494d650SFam Zheng curl_header_cb); 8003494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); 801769ce76dSAlexander Graf if (curl_easy_perform(state->curl)) 802769ce76dSAlexander Graf goto out; 803a41c4578STomáš Golembiovský if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { 804769ce76dSAlexander Graf goto out; 805a41c4578STomáš Golembiovský } 806a41c4578STomáš Golembiovský /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not 807a41c4578STomáš Golembiovský * know or the size is zero. From 7.19.4 CURL returns -1 if size is not 808a41c4578STomáš Golembiovský * known and zero if it is realy zero-length file. */ 809a41c4578STomáš Golembiovský #if LIBCURL_VERSION_NUM >= 0x071304 810a41c4578STomáš Golembiovský if (d < 0) { 811a41c4578STomáš Golembiovský pstrcpy(state->errmsg, CURL_ERROR_SIZE, 812a41c4578STomáš Golembiovský "Server didn't report file size."); 813a41c4578STomáš Golembiovský goto out; 814a41c4578STomáš Golembiovský } 815a41c4578STomáš Golembiovský #else 816a41c4578STomáš Golembiovský if (d <= 0) { 817a41c4578STomáš Golembiovský pstrcpy(state->errmsg, CURL_ERROR_SIZE, 818a41c4578STomáš Golembiovský "Unknown file size or zero-length file."); 819a41c4578STomáš Golembiovský goto out; 820a41c4578STomáš Golembiovský } 821a41c4578STomáš Golembiovský #endif 822a41c4578STomáš Golembiovský 8232125e5eaSPaolo Bonzini s->len = d; 824a41c4578STomáš Golembiovský 8253494d650SFam Zheng if ((!strncasecmp(s->url, "http://", strlen("http://")) 8263494d650SFam Zheng || !strncasecmp(s->url, "https://", strlen("https://"))) 8273494d650SFam Zheng && !s->accept_range) { 8283494d650SFam Zheng pstrcpy(state->errmsg, CURL_ERROR_SIZE, 8293494d650SFam Zheng "Server does not support 'range' (byte ranges)."); 8303494d650SFam Zheng goto out; 8313494d650SFam Zheng } 8322125e5eaSPaolo Bonzini DPRINTF("CURL: Size = %" PRIu64 "\n", s->len); 833769ce76dSAlexander Graf 834456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 835769ce76dSAlexander Graf curl_clean_state(state); 836456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 837769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 838769ce76dSAlexander Graf state->curl = NULL; 839769ce76dSAlexander Graf 84063f0f45fSStefan Hajnoczi curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); 841769ce76dSAlexander Graf 8428e6d58cdSKevin Wolf qemu_opts_del(opts); 843769ce76dSAlexander Graf return 0; 844769ce76dSAlexander Graf 845769ce76dSAlexander Graf out: 846acd7fdc6SMaria Kustova error_setg(errp, "CURL: Error opening file: %s", state->errmsg); 847769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 848769ce76dSAlexander Graf state->curl = NULL; 849769ce76dSAlexander Graf out_noclean: 850456af346SPaolo Bonzini qemu_mutex_destroy(&s->mutex); 851a94f83d9SRichard W.M. Jones g_free(s->cookie); 8528e6d58cdSKevin Wolf g_free(s->url); 853996922deSJeff Cody g_free(s->username); 854996922deSJeff Cody g_free(s->proxyusername); 855996922deSJeff Cody g_free(s->proxypassword); 8568e6d58cdSKevin Wolf qemu_opts_del(opts); 857769ce76dSAlexander Graf return -EINVAL; 858769ce76dSAlexander Graf } 859769ce76dSAlexander Graf 86028256d82SPaolo Bonzini static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) 861769ce76dSAlexander Graf { 862769ce76dSAlexander Graf CURLState *state; 863b69cdef8SMatthew Booth int running; 864769ce76dSAlexander Graf 8651919631eSPaolo Bonzini BDRVCURLState *s = bs->opaque; 866769ce76dSAlexander Graf 8672125e5eaSPaolo Bonzini uint64_t start = acb->offset; 8682125e5eaSPaolo Bonzini uint64_t end; 869769ce76dSAlexander Graf 870ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->mutex); 8711919631eSPaolo Bonzini 872769ce76dSAlexander Graf // In case we have the requested data already (e.g. read-ahead), 873769ce76dSAlexander Graf // we can just call the callback and be done. 87428256d82SPaolo Bonzini if (curl_find_buf(s, start, acb->bytes, acb)) { 87534db05e7SPaolo Bonzini goto out; 876769ce76dSAlexander Graf } 877769ce76dSAlexander Graf 878769ce76dSAlexander Graf // No cache found, so let's start a new request 8793ce6a729SPaolo Bonzini for (;;) { 8803ce6a729SPaolo Bonzini state = curl_find_state(s); 8813ce6a729SPaolo Bonzini if (state) { 8823ce6a729SPaolo Bonzini break; 8833ce6a729SPaolo Bonzini } 884709f2132SPaolo Bonzini qemu_co_queue_wait(&s->free_state_waitq, &s->mutex); 8853ce6a729SPaolo Bonzini } 8863ce6a729SPaolo Bonzini 8873ce6a729SPaolo Bonzini if (curl_init_state(s, state) < 0) { 8883ce6a729SPaolo Bonzini curl_clean_state(state); 88928256d82SPaolo Bonzini acb->ret = -EIO; 8901919631eSPaolo Bonzini goto out; 891363c3c85SNick Thomas } 892769ce76dSAlexander Graf 893769ce76dSAlexander Graf acb->start = 0; 8942125e5eaSPaolo Bonzini acb->end = MIN(acb->bytes, s->len - start); 895769ce76dSAlexander Graf 896769ce76dSAlexander Graf state->buf_off = 0; 8977267c094SAnthony Liguori g_free(state->orig_buf); 898769ce76dSAlexander Graf state->buf_start = start; 8994e504535SMax Reitz state->buf_len = MIN(acb->end + s->readahead_size, s->len - start); 9004e504535SMax Reitz end = start + state->buf_len - 1; 9018dc7a772SKevin Wolf state->orig_buf = g_try_malloc(state->buf_len); 9028dc7a772SKevin Wolf if (state->buf_len && state->orig_buf == NULL) { 9038dc7a772SKevin Wolf curl_clean_state(state); 90428256d82SPaolo Bonzini acb->ret = -ENOMEM; 9051919631eSPaolo Bonzini goto out; 9068dc7a772SKevin Wolf } 907769ce76dSAlexander Graf state->acb[0] = acb; 908769ce76dSAlexander Graf 9092125e5eaSPaolo Bonzini snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end); 9102125e5eaSPaolo Bonzini DPRINTF("CURL (AIO): Reading %" PRIu64 " at %" PRIu64 " (%s)\n", 9112125e5eaSPaolo Bonzini acb->bytes, start, state->range); 912769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); 913769ce76dSAlexander Graf 914769ce76dSAlexander Graf curl_multi_add_handle(s->multi, state->curl); 915769ce76dSAlexander Graf 916b69cdef8SMatthew Booth /* Tell curl it needs to kick things off */ 917b69cdef8SMatthew Booth curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 9181919631eSPaolo Bonzini 9191919631eSPaolo Bonzini out: 920ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 921363c3c85SNick Thomas } 922363c3c85SNick Thomas 92328256d82SPaolo Bonzini static int coroutine_fn curl_co_preadv(BlockDriverState *bs, 92428256d82SPaolo Bonzini uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) 925363c3c85SNick Thomas { 92628256d82SPaolo Bonzini CURLAIOCB acb = { 92728256d82SPaolo Bonzini .co = qemu_coroutine_self(), 92828256d82SPaolo Bonzini .ret = -EINPROGRESS, 92928256d82SPaolo Bonzini .qiov = qiov, 93028256d82SPaolo Bonzini .offset = offset, 93128256d82SPaolo Bonzini .bytes = bytes 93228256d82SPaolo Bonzini }; 933363c3c85SNick Thomas 93428256d82SPaolo Bonzini curl_setup_preadv(bs, &acb); 93528256d82SPaolo Bonzini while (acb.ret == -EINPROGRESS) { 93628256d82SPaolo Bonzini qemu_coroutine_yield(); 93728256d82SPaolo Bonzini } 93828256d82SPaolo Bonzini return acb.ret; 939769ce76dSAlexander Graf } 940769ce76dSAlexander Graf 941769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs) 942769ce76dSAlexander Graf { 943769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 944769ce76dSAlexander Graf 945d0f2c4c6Smalc DPRINTF("CURL: Close\n"); 94663f0f45fSStefan Hajnoczi curl_detach_aio_context(bs); 947ba3186c4SPaolo Bonzini qemu_mutex_destroy(&s->mutex); 948031fd1beSPeter Maydell 949a94f83d9SRichard W.M. Jones g_free(s->cookie); 95045724d6dSStefan Weil g_free(s->url); 951996922deSJeff Cody g_free(s->username); 952996922deSJeff Cody g_free(s->proxyusername); 953996922deSJeff Cody g_free(s->proxypassword); 954769ce76dSAlexander Graf } 955769ce76dSAlexander Graf 956769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs) 957769ce76dSAlexander Graf { 958769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 959769ce76dSAlexander Graf return s->len; 960769ce76dSAlexander Graf } 961769ce76dSAlexander Graf 962769ce76dSAlexander Graf static BlockDriver bdrv_http = { 963769ce76dSAlexander Graf .format_name = "http", 964769ce76dSAlexander Graf .protocol_name = "http", 965769ce76dSAlexander Graf 966769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 9678e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 96866f82ceeSKevin Wolf .bdrv_file_open = curl_open, 969769ce76dSAlexander Graf .bdrv_close = curl_close, 970769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 971769ce76dSAlexander Graf 97228256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 97363f0f45fSStefan Hajnoczi 97463f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 97563f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 976769ce76dSAlexander Graf }; 977769ce76dSAlexander Graf 978769ce76dSAlexander Graf static BlockDriver bdrv_https = { 979769ce76dSAlexander Graf .format_name = "https", 980769ce76dSAlexander Graf .protocol_name = "https", 981769ce76dSAlexander Graf 982769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 9838e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 98466f82ceeSKevin Wolf .bdrv_file_open = curl_open, 985769ce76dSAlexander Graf .bdrv_close = curl_close, 986769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 987769ce76dSAlexander Graf 98828256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 98963f0f45fSStefan Hajnoczi 99063f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 99163f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 992769ce76dSAlexander Graf }; 993769ce76dSAlexander Graf 994769ce76dSAlexander Graf static BlockDriver bdrv_ftp = { 995769ce76dSAlexander Graf .format_name = "ftp", 996769ce76dSAlexander Graf .protocol_name = "ftp", 997769ce76dSAlexander Graf 998769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 9998e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 100066f82ceeSKevin Wolf .bdrv_file_open = curl_open, 1001769ce76dSAlexander Graf .bdrv_close = curl_close, 1002769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 1003769ce76dSAlexander Graf 100428256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 100563f0f45fSStefan Hajnoczi 100663f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 100763f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 1008769ce76dSAlexander Graf }; 1009769ce76dSAlexander Graf 1010769ce76dSAlexander Graf static BlockDriver bdrv_ftps = { 1011769ce76dSAlexander Graf .format_name = "ftps", 1012769ce76dSAlexander Graf .protocol_name = "ftps", 1013769ce76dSAlexander Graf 1014769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 10158e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 101666f82ceeSKevin Wolf .bdrv_file_open = curl_open, 1017769ce76dSAlexander Graf .bdrv_close = curl_close, 1018769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 1019769ce76dSAlexander Graf 102028256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 102163f0f45fSStefan Hajnoczi 102263f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 102363f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 1024769ce76dSAlexander Graf }; 1025769ce76dSAlexander Graf 1026769ce76dSAlexander Graf static void curl_block_init(void) 1027769ce76dSAlexander Graf { 1028769ce76dSAlexander Graf bdrv_register(&bdrv_http); 1029769ce76dSAlexander Graf bdrv_register(&bdrv_https); 1030769ce76dSAlexander Graf bdrv_register(&bdrv_ftp); 1031769ce76dSAlexander Graf bdrv_register(&bdrv_ftps); 1032769ce76dSAlexander Graf } 1033769ce76dSAlexander Graf 1034769ce76dSAlexander Graf block_init(curl_block_init); 1035