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" 280b8fa32fSMarkus Armbruster #include "qemu/module.h" 29922a01a0SMarkus Armbruster #include "qemu/option.h" 30737e150eSPaolo Bonzini #include "block/block_int.h" 31452fcdbcSMarkus 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" 36ed2a66deSLaurent Vivier #include "trace.h" 37769ce76dSAlexander Graf 38769ce76dSAlexander Graf // #define DEBUG_VERBOSE 39769ce76dSAlexander Graf 40031fd1beSPeter Maydell #if LIBCURL_VERSION_NUM >= 0x071000 41031fd1beSPeter Maydell /* The multi interface timer callback was introduced in 7.16.0 */ 42031fd1beSPeter Maydell #define NEED_CURL_TIMER_CALLBACK 439aedd5a5SMatthew Booth #define HAVE_SOCKET_ACTION 449aedd5a5SMatthew Booth #endif 459aedd5a5SMatthew Booth 469aedd5a5SMatthew Booth #ifndef HAVE_SOCKET_ACTION 479aedd5a5SMatthew Booth /* If curl_multi_socket_action isn't available, define it statically here in 489aedd5a5SMatthew Booth * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is 499aedd5a5SMatthew Booth * less efficient but still safe. */ 509aedd5a5SMatthew Booth static CURLMcode __curl_multi_socket_action(CURLM *multi_handle, 519aedd5a5SMatthew Booth curl_socket_t sockfd, 529aedd5a5SMatthew Booth int ev_bitmask, 539aedd5a5SMatthew Booth int *running_handles) 549aedd5a5SMatthew Booth { 559aedd5a5SMatthew Booth return curl_multi_socket(multi_handle, sockfd, running_handles); 569aedd5a5SMatthew Booth } 579aedd5a5SMatthew Booth #define curl_multi_socket_action __curl_multi_socket_action 58031fd1beSPeter Maydell #endif 59031fd1beSPeter Maydell 60fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ 6123dce387SMax Reitz CURLPROTO_FTP | CURLPROTO_FTPS) 62fb6d1bbdSStefan Hajnoczi 63769ce76dSAlexander Graf #define CURL_NUM_STATES 8 64769ce76dSAlexander Graf #define CURL_NUM_ACB 8 65f76faedaSRichard W.M. Jones #define CURL_TIMEOUT_MAX 10000 66769ce76dSAlexander Graf 67e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL "url" 68e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead" 6997a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" 70212aefaaSDaniel Henrique Barboza #define CURL_BLOCK_OPT_TIMEOUT "timeout" 71a94f83d9SRichard W.M. Jones #define CURL_BLOCK_OPT_COOKIE "cookie" 72327c8ebdSPeter Krempa #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret" 731bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_USERNAME "username" 741bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret" 751bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username" 761bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret" 77e3542c67SMatthew Booth 78712b64e8SMax Reitz #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024) 79712b64e8SMax Reitz #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true 80712b64e8SMax Reitz #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5 81712b64e8SMax Reitz 82769ce76dSAlexander Graf struct BDRVCURLState; 8304878616SMax Reitz struct CURLState; 84769ce76dSAlexander Graf 852d25964dSJeff Cody static bool libcurl_initialized; 862d25964dSJeff Cody 87769ce76dSAlexander Graf typedef struct CURLAIOCB { 8828256d82SPaolo Bonzini Coroutine *co; 89769ce76dSAlexander Graf QEMUIOVector *qiov; 90363c3c85SNick Thomas 912125e5eaSPaolo Bonzini uint64_t offset; 922125e5eaSPaolo Bonzini uint64_t bytes; 9328256d82SPaolo Bonzini int ret; 94363c3c85SNick Thomas 95769ce76dSAlexander Graf size_t start; 96769ce76dSAlexander Graf size_t end; 97769ce76dSAlexander Graf } CURLAIOCB; 98769ce76dSAlexander Graf 99ff5ca166SMax Reitz typedef struct CURLSocket { 100ff5ca166SMax Reitz int fd; 10104878616SMax Reitz struct CURLState *state; 102ff5ca166SMax Reitz QLIST_ENTRY(CURLSocket) next; 103ff5ca166SMax Reitz } CURLSocket; 104ff5ca166SMax Reitz 105769ce76dSAlexander Graf typedef struct CURLState 106769ce76dSAlexander Graf { 107769ce76dSAlexander Graf struct BDRVCURLState *s; 108769ce76dSAlexander Graf CURLAIOCB *acb[CURL_NUM_ACB]; 109769ce76dSAlexander Graf CURL *curl; 110ff5ca166SMax Reitz QLIST_HEAD(, CURLSocket) sockets; 111769ce76dSAlexander Graf char *orig_buf; 1122125e5eaSPaolo Bonzini uint64_t buf_start; 113769ce76dSAlexander Graf size_t buf_off; 114769ce76dSAlexander Graf size_t buf_len; 115769ce76dSAlexander Graf char range[128]; 116769ce76dSAlexander Graf char errmsg[CURL_ERROR_SIZE]; 117769ce76dSAlexander Graf char in_use; 118769ce76dSAlexander Graf } CURLState; 119769ce76dSAlexander Graf 120769ce76dSAlexander Graf typedef struct BDRVCURLState { 121769ce76dSAlexander Graf CURLM *multi; 122031fd1beSPeter Maydell QEMUTimer timer; 1232125e5eaSPaolo Bonzini uint64_t len; 124769ce76dSAlexander Graf CURLState states[CURL_NUM_STATES]; 125769ce76dSAlexander Graf char *url; 126c76f4952SNolan size_t readahead_size; 12797a3ea57SMatthew Booth bool sslverify; 128f76faedaSRichard W.M. Jones uint64_t timeout; 129a94f83d9SRichard W.M. Jones char *cookie; 1303494d650SFam Zheng bool accept_range; 13163f0f45fSStefan Hajnoczi AioContext *aio_context; 132ba3186c4SPaolo Bonzini QemuMutex mutex; 133709f2132SPaolo Bonzini CoQueue free_state_waitq; 1341bff9606SDaniel P. Berrange char *username; 1351bff9606SDaniel P. Berrange char *password; 1361bff9606SDaniel P. Berrange char *proxyusername; 1371bff9606SDaniel P. Berrange char *proxypassword; 138769ce76dSAlexander Graf } BDRVCURLState; 139769ce76dSAlexander Graf 140769ce76dSAlexander Graf static void curl_clean_state(CURLState *s); 141769ce76dSAlexander Graf static void curl_multi_do(void *arg); 142838ef602SMatthew Booth static void curl_multi_read(void *arg); 143769ce76dSAlexander Graf 144031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 14534db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 146031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) 147031fd1beSPeter Maydell { 148031fd1beSPeter Maydell BDRVCURLState *s = opaque; 149031fd1beSPeter Maydell 150ed2a66deSLaurent Vivier trace_curl_timer_cb(timeout_ms); 151031fd1beSPeter Maydell if (timeout_ms == -1) { 152031fd1beSPeter Maydell timer_del(&s->timer); 153031fd1beSPeter Maydell } else { 154031fd1beSPeter Maydell int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; 155031fd1beSPeter Maydell timer_mod(&s->timer, 156031fd1beSPeter Maydell qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); 157031fd1beSPeter Maydell } 158031fd1beSPeter Maydell return 0; 159031fd1beSPeter Maydell } 160031fd1beSPeter Maydell #endif 161031fd1beSPeter Maydell 16234db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 163769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, 16463f0f45fSStefan Hajnoczi void *userp, void *sp) 165769ce76dSAlexander Graf { 16663f0f45fSStefan Hajnoczi BDRVCURLState *s; 167838ef602SMatthew Booth CURLState *state = NULL; 168ff5ca166SMax Reitz CURLSocket *socket; 169ff5ca166SMax Reitz 170838ef602SMatthew Booth curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); 17163f0f45fSStefan Hajnoczi s = state->s; 172838ef602SMatthew Booth 173ff5ca166SMax Reitz QLIST_FOREACH(socket, &state->sockets, next) { 174ff5ca166SMax Reitz if (socket->fd == fd) { 175ff5ca166SMax Reitz break; 176ff5ca166SMax Reitz } 177ff5ca166SMax Reitz } 178ff5ca166SMax Reitz if (!socket) { 179ff5ca166SMax Reitz socket = g_new0(CURLSocket, 1); 180ff5ca166SMax Reitz socket->fd = fd; 18104878616SMax Reitz socket->state = state; 182ff5ca166SMax Reitz QLIST_INSERT_HEAD(&state->sockets, socket, next); 183ff5ca166SMax Reitz } 184ff5ca166SMax Reitz 185ed2a66deSLaurent Vivier trace_curl_sock_cb(action, (int)fd); 186769ce76dSAlexander Graf switch (action) { 187769ce76dSAlexander Graf case CURL_POLL_IN: 188dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 189f6a51c84SStefan Hajnoczi curl_multi_read, NULL, NULL, state); 190769ce76dSAlexander Graf break; 191769ce76dSAlexander Graf case CURL_POLL_OUT: 192dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 193f6a51c84SStefan Hajnoczi NULL, curl_multi_do, NULL, state); 194769ce76dSAlexander Graf break; 195769ce76dSAlexander Graf case CURL_POLL_INOUT: 196dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 197f6a51c84SStefan Hajnoczi curl_multi_read, curl_multi_do, NULL, state); 198769ce76dSAlexander Graf break; 199769ce76dSAlexander Graf case CURL_POLL_REMOVE: 200dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 201f6a51c84SStefan Hajnoczi NULL, NULL, NULL, NULL); 202769ce76dSAlexander Graf break; 203769ce76dSAlexander Graf } 204769ce76dSAlexander Graf 205*007f339bSMax Reitz if (action == CURL_POLL_REMOVE) { 206*007f339bSMax Reitz QLIST_REMOVE(socket, next); 207*007f339bSMax Reitz g_free(socket); 208*007f339bSMax Reitz } 209*007f339bSMax Reitz 210769ce76dSAlexander Graf return 0; 211769ce76dSAlexander Graf } 212769ce76dSAlexander Graf 21334db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 2143494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 215769ce76dSAlexander Graf { 2163494d650SFam Zheng BDRVCURLState *s = opaque; 217769ce76dSAlexander Graf size_t realsize = size * nmemb; 2183494d650SFam Zheng const char *accept_line = "Accept-Ranges: bytes"; 219769ce76dSAlexander Graf 2203494d650SFam Zheng if (realsize >= strlen(accept_line) 2213494d650SFam Zheng && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) { 2223494d650SFam Zheng s->accept_range = true; 2230bfcd599SBlue Swirl } 224769ce76dSAlexander Graf 225769ce76dSAlexander Graf return realsize; 226769ce76dSAlexander Graf } 227769ce76dSAlexander Graf 22834db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 229769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 230769ce76dSAlexander Graf { 231769ce76dSAlexander Graf CURLState *s = ((CURLState*)opaque); 232769ce76dSAlexander Graf size_t realsize = size * nmemb; 233769ce76dSAlexander Graf int i; 234769ce76dSAlexander Graf 235ed2a66deSLaurent Vivier trace_curl_read_cb(realsize); 236769ce76dSAlexander Graf 2374e767657SMax Reitz if (!s || !s->orig_buf) { 2384e767657SMax Reitz goto read_end; 2394e767657SMax Reitz } 240769ce76dSAlexander Graf 2416d4b9e55SFam Zheng if (s->buf_off >= s->buf_len) { 2426d4b9e55SFam Zheng /* buffer full, read nothing */ 2434e767657SMax Reitz goto read_end; 2446d4b9e55SFam Zheng } 2456d4b9e55SFam Zheng realsize = MIN(realsize, s->buf_len - s->buf_off); 246769ce76dSAlexander Graf memcpy(s->orig_buf + s->buf_off, ptr, realsize); 247769ce76dSAlexander Graf s->buf_off += realsize; 248769ce76dSAlexander Graf 249769ce76dSAlexander Graf for(i=0; i<CURL_NUM_ACB; i++) { 250769ce76dSAlexander Graf CURLAIOCB *acb = s->acb[i]; 251769ce76dSAlexander Graf 252769ce76dSAlexander Graf if (!acb) 253769ce76dSAlexander Graf continue; 254769ce76dSAlexander Graf 255769ce76dSAlexander Graf if ((s->buf_off >= acb->end)) { 2562125e5eaSPaolo Bonzini size_t request_length = acb->bytes; 2574e504535SMax Reitz 25803396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start, 259769ce76dSAlexander Graf acb->end - acb->start); 2604e504535SMax Reitz 2614e504535SMax Reitz if (acb->end - acb->start < request_length) { 2624e504535SMax Reitz size_t offset = acb->end - acb->start; 2634e504535SMax Reitz qemu_iovec_memset(acb->qiov, offset, 0, 2644e504535SMax Reitz request_length - offset); 2654e504535SMax Reitz } 2664e504535SMax Reitz 26728256d82SPaolo Bonzini acb->ret = 0; 268769ce76dSAlexander Graf s->acb[i] = NULL; 26928256d82SPaolo Bonzini qemu_mutex_unlock(&s->s->mutex); 27028256d82SPaolo Bonzini aio_co_wake(acb->co); 27128256d82SPaolo Bonzini qemu_mutex_lock(&s->s->mutex); 272769ce76dSAlexander Graf } 273769ce76dSAlexander Graf } 274769ce76dSAlexander Graf 2754e767657SMax Reitz read_end: 2764e767657SMax Reitz /* curl will error out if we do not return this value */ 2774e767657SMax Reitz return size * nmemb; 278769ce76dSAlexander Graf } 279769ce76dSAlexander Graf 280456af346SPaolo Bonzini /* Called with s->mutex held. */ 28128256d82SPaolo Bonzini static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len, 282769ce76dSAlexander Graf CURLAIOCB *acb) 283769ce76dSAlexander Graf { 284769ce76dSAlexander Graf int i; 2852125e5eaSPaolo Bonzini uint64_t end = start + len; 2862125e5eaSPaolo Bonzini uint64_t clamped_end = MIN(end, s->len); 2872125e5eaSPaolo Bonzini uint64_t clamped_len = clamped_end - start; 288769ce76dSAlexander Graf 289769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 290769ce76dSAlexander Graf CURLState *state = &s->states[i]; 2912125e5eaSPaolo Bonzini uint64_t buf_end = (state->buf_start + state->buf_off); 2922125e5eaSPaolo Bonzini uint64_t buf_fend = (state->buf_start + state->buf_len); 293769ce76dSAlexander Graf 294769ce76dSAlexander Graf if (!state->orig_buf) 295769ce76dSAlexander Graf continue; 296769ce76dSAlexander Graf if (!state->buf_off) 297769ce76dSAlexander Graf continue; 298769ce76dSAlexander Graf 299769ce76dSAlexander Graf // Does the existing buffer cover our section? 300769ce76dSAlexander Graf if ((start >= state->buf_start) && 301769ce76dSAlexander Graf (start <= buf_end) && 3024e504535SMax Reitz (clamped_end >= state->buf_start) && 3034e504535SMax Reitz (clamped_end <= buf_end)) 304769ce76dSAlexander Graf { 305769ce76dSAlexander Graf char *buf = state->orig_buf + (start - state->buf_start); 306769ce76dSAlexander Graf 3074e504535SMax Reitz qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len); 3084e504535SMax Reitz if (clamped_len < len) { 3094e504535SMax Reitz qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len); 3104e504535SMax Reitz } 31128256d82SPaolo Bonzini acb->ret = 0; 31228256d82SPaolo Bonzini return true; 313769ce76dSAlexander Graf } 314769ce76dSAlexander Graf 315769ce76dSAlexander Graf // Wait for unfinished chunks 316b7079df4SMatthew Booth if (state->in_use && 317b7079df4SMatthew Booth (start >= state->buf_start) && 318769ce76dSAlexander Graf (start <= buf_fend) && 3194e504535SMax Reitz (clamped_end >= state->buf_start) && 3204e504535SMax Reitz (clamped_end <= buf_fend)) 321769ce76dSAlexander Graf { 322769ce76dSAlexander Graf int j; 323769ce76dSAlexander Graf 324769ce76dSAlexander Graf acb->start = start - state->buf_start; 3254e504535SMax Reitz acb->end = acb->start + clamped_len; 326769ce76dSAlexander Graf 327769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) { 328769ce76dSAlexander Graf if (!state->acb[j]) { 329769ce76dSAlexander Graf state->acb[j] = acb; 33028256d82SPaolo Bonzini return true; 331769ce76dSAlexander Graf } 332769ce76dSAlexander Graf } 333769ce76dSAlexander Graf } 334769ce76dSAlexander Graf } 335769ce76dSAlexander Graf 33628256d82SPaolo Bonzini return false; 337769ce76dSAlexander Graf } 338769ce76dSAlexander Graf 339ba3186c4SPaolo Bonzini /* Called with s->mutex held. */ 340838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s) 341769ce76dSAlexander Graf { 342769ce76dSAlexander Graf int msgs_in_queue; 343769ce76dSAlexander Graf 344769ce76dSAlexander Graf /* Try to find done transfers, so we can free the easy 345769ce76dSAlexander Graf * handle again. */ 3461f2cead3SMatthew Booth for (;;) { 347769ce76dSAlexander Graf CURLMsg *msg; 348769ce76dSAlexander Graf msg = curl_multi_info_read(s->multi, &msgs_in_queue); 349769ce76dSAlexander Graf 3501f2cead3SMatthew Booth /* Quit when there are no more completions */ 351769ce76dSAlexander Graf if (!msg) 352769ce76dSAlexander Graf break; 353769ce76dSAlexander Graf 3541f2cead3SMatthew Booth if (msg->msg == CURLMSG_DONE) { 355769ce76dSAlexander Graf CURLState *state = NULL; 356f6246509SMatthew Booth curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, 357f6246509SMatthew Booth (char **)&state); 358f785a5aeSNicholas Thomas 359f785a5aeSNicholas Thomas /* ACBs for successful messages get completed in curl_read_cb */ 360f785a5aeSNicholas Thomas if (msg->data.result != CURLE_OK) { 361f785a5aeSNicholas Thomas int i; 362796a060bSRichard W.M. Jones static int errcount = 100; 363796a060bSRichard W.M. Jones 364796a060bSRichard W.M. Jones /* Don't lose the original error message from curl, since 365796a060bSRichard W.M. Jones * it contains extra data. 366796a060bSRichard W.M. Jones */ 367796a060bSRichard W.M. Jones if (errcount > 0) { 368796a060bSRichard W.M. Jones error_report("curl: %s", state->errmsg); 369796a060bSRichard W.M. Jones if (--errcount == 0) { 370796a060bSRichard W.M. Jones error_report("curl: further errors suppressed"); 371796a060bSRichard W.M. Jones } 372796a060bSRichard W.M. Jones } 373796a060bSRichard W.M. Jones 374f785a5aeSNicholas Thomas for (i = 0; i < CURL_NUM_ACB; i++) { 375f785a5aeSNicholas Thomas CURLAIOCB *acb = state->acb[i]; 376f785a5aeSNicholas Thomas 377f785a5aeSNicholas Thomas if (acb == NULL) { 378f785a5aeSNicholas Thomas continue; 379f785a5aeSNicholas Thomas } 380f785a5aeSNicholas Thomas 38128256d82SPaolo Bonzini acb->ret = -EIO; 382f785a5aeSNicholas Thomas state->acb[i] = NULL; 38328256d82SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 38428256d82SPaolo Bonzini aio_co_wake(acb->co); 38528256d82SPaolo Bonzini qemu_mutex_lock(&s->mutex); 386f785a5aeSNicholas Thomas } 387f785a5aeSNicholas Thomas } 388f785a5aeSNicholas Thomas 389769ce76dSAlexander Graf curl_clean_state(state); 390769ce76dSAlexander Graf break; 391769ce76dSAlexander Graf } 392769ce76dSAlexander Graf } 393769ce76dSAlexander Graf } 394769ce76dSAlexander Graf 395ba3186c4SPaolo Bonzini /* Called with s->mutex held. */ 3969d456654SPaolo Bonzini static void curl_multi_do_locked(CURLState *s) 397031fd1beSPeter Maydell { 398ff5ca166SMax Reitz CURLSocket *socket, *next_socket; 399031fd1beSPeter Maydell int running; 400031fd1beSPeter Maydell int r; 401031fd1beSPeter Maydell 402838ef602SMatthew Booth if (!s->s->multi) { 403031fd1beSPeter Maydell return; 404031fd1beSPeter Maydell } 405031fd1beSPeter Maydell 406ff5ca166SMax Reitz /* Need to use _SAFE because curl_multi_socket_action() may trigger 407ff5ca166SMax Reitz * curl_sock_cb() which might modify this list */ 408ff5ca166SMax Reitz QLIST_FOREACH_SAFE(socket, &s->sockets, next, next_socket) { 409031fd1beSPeter Maydell do { 410ff5ca166SMax Reitz r = curl_multi_socket_action(s->s->multi, socket->fd, 0, &running); 411031fd1beSPeter Maydell } while (r == CURLM_CALL_MULTI_PERFORM); 412ff5ca166SMax Reitz } 413838ef602SMatthew Booth } 414838ef602SMatthew Booth 4159d456654SPaolo Bonzini static void curl_multi_do(void *arg) 4169d456654SPaolo Bonzini { 4179d456654SPaolo Bonzini CURLState *s = (CURLState *)arg; 4189d456654SPaolo Bonzini 419ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->s->mutex); 4209d456654SPaolo Bonzini curl_multi_do_locked(s); 421ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->s->mutex); 4229d456654SPaolo Bonzini } 4239d456654SPaolo Bonzini 424838ef602SMatthew Booth static void curl_multi_read(void *arg) 425838ef602SMatthew Booth { 426838ef602SMatthew Booth CURLState *s = (CURLState *)arg; 427838ef602SMatthew Booth 428ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->s->mutex); 4299d456654SPaolo Bonzini curl_multi_do_locked(s); 430838ef602SMatthew Booth curl_multi_check_completion(s->s); 431ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->s->mutex); 432031fd1beSPeter Maydell } 433031fd1beSPeter Maydell 434031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg) 435031fd1beSPeter Maydell { 436031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 437031fd1beSPeter Maydell BDRVCURLState *s = (BDRVCURLState *)arg; 438031fd1beSPeter Maydell int running; 439031fd1beSPeter Maydell 440031fd1beSPeter Maydell if (!s->multi) { 441031fd1beSPeter Maydell return; 442031fd1beSPeter Maydell } 443031fd1beSPeter Maydell 444ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->mutex); 445031fd1beSPeter Maydell curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 446031fd1beSPeter Maydell 447838ef602SMatthew Booth curl_multi_check_completion(s); 448ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 449031fd1beSPeter Maydell #else 450031fd1beSPeter Maydell abort(); 451031fd1beSPeter Maydell #endif 452031fd1beSPeter Maydell } 453031fd1beSPeter Maydell 454456af346SPaolo Bonzini /* Called with s->mutex held. */ 4553ce6a729SPaolo Bonzini static CURLState *curl_find_state(BDRVCURLState *s) 456769ce76dSAlexander Graf { 457769ce76dSAlexander Graf CURLState *state = NULL; 4583ce6a729SPaolo Bonzini int i; 459769ce76dSAlexander Graf 460769ce76dSAlexander Graf for (i = 0; i < CURL_NUM_STATES; i++) { 4613ce6a729SPaolo Bonzini if (!s->states[i].in_use) { 462769ce76dSAlexander Graf state = &s->states[i]; 463769ce76dSAlexander Graf state->in_use = 1; 464769ce76dSAlexander Graf break; 465769ce76dSAlexander Graf } 466769ce76dSAlexander Graf } 4673ce6a729SPaolo Bonzini return state; 4683ce6a729SPaolo Bonzini } 469769ce76dSAlexander Graf 4703ce6a729SPaolo Bonzini static int curl_init_state(BDRVCURLState *s, CURLState *state) 4713ce6a729SPaolo Bonzini { 4729e550b32SMatthew Booth if (!state->curl) { 473769ce76dSAlexander Graf state->curl = curl_easy_init(); 4749e550b32SMatthew Booth if (!state->curl) { 4753ce6a729SPaolo Bonzini return -EIO; 4769e550b32SMatthew Booth } 477769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_URL, s->url); 47897a3ea57SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, 47997a3ea57SMatthew Booth (long) s->sslverify); 480637fa44aSRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST, 481637fa44aSRichard W.M. Jones s->sslverify ? 2L : 0L); 482a94f83d9SRichard W.M. Jones if (s->cookie) { 483a94f83d9SRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie); 484a94f83d9SRichard W.M. Jones } 485f76faedaSRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout); 4869e550b32SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, 4879e550b32SMatthew Booth (void *)curl_read_cb); 488769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); 489769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); 490769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); 491769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); 492769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); 493769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); 494f785a5aeSNicholas Thomas curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); 495769ce76dSAlexander Graf 4961bff9606SDaniel P. Berrange if (s->username) { 4971bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username); 4981bff9606SDaniel P. Berrange } 4991bff9606SDaniel P. Berrange if (s->password) { 5001bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password); 5011bff9606SDaniel P. Berrange } 5021bff9606SDaniel P. Berrange if (s->proxyusername) { 5031bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, 5041bff9606SDaniel P. Berrange CURLOPT_PROXYUSERNAME, s->proxyusername); 5051bff9606SDaniel P. Berrange } 5061bff9606SDaniel P. Berrange if (s->proxypassword) { 5071bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, 5081bff9606SDaniel P. Berrange CURLOPT_PROXYPASSWORD, s->proxypassword); 5091bff9606SDaniel P. Berrange } 5101bff9606SDaniel P. Berrange 511fb6d1bbdSStefan Hajnoczi /* Restrict supported protocols to avoid security issues in the more 512fb6d1bbdSStefan Hajnoczi * obscure protocols. For example, do not allow POP3/SMTP/IMAP see 513fb6d1bbdSStefan Hajnoczi * CVE-2013-0249. 5148a8f5840SStefan Hajnoczi * 5158a8f5840SStefan Hajnoczi * Restricting protocols is only supported from 7.19.4 upwards. 516fb6d1bbdSStefan Hajnoczi */ 5178a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304 518fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); 519fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); 5208a8f5840SStefan Hajnoczi #endif 521fb6d1bbdSStefan Hajnoczi 522769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE 523769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); 524769ce76dSAlexander Graf #endif 5259e550b32SMatthew Booth } 526769ce76dSAlexander Graf 527ff5ca166SMax Reitz QLIST_INIT(&state->sockets); 528769ce76dSAlexander Graf state->s = s; 529769ce76dSAlexander Graf 5303ce6a729SPaolo Bonzini return 0; 531769ce76dSAlexander Graf } 532769ce76dSAlexander Graf 533456af346SPaolo Bonzini /* Called with s->mutex held. */ 534769ce76dSAlexander Graf static void curl_clean_state(CURLState *s) 535769ce76dSAlexander Graf { 536675a7756SPaolo Bonzini int j; 537675a7756SPaolo Bonzini for (j = 0; j < CURL_NUM_ACB; j++) { 538675a7756SPaolo Bonzini assert(!s->acb[j]); 539675a7756SPaolo Bonzini } 540675a7756SPaolo Bonzini 541769ce76dSAlexander Graf if (s->s->multi) 542769ce76dSAlexander Graf curl_multi_remove_handle(s->s->multi, s->curl); 543ff5ca166SMax Reitz 544ff5ca166SMax Reitz while (!QLIST_EMPTY(&s->sockets)) { 545ff5ca166SMax Reitz CURLSocket *socket = QLIST_FIRST(&s->sockets); 546ff5ca166SMax Reitz 547ff5ca166SMax Reitz QLIST_REMOVE(socket, next); 548ff5ca166SMax Reitz g_free(socket); 549ff5ca166SMax Reitz } 550ff5ca166SMax Reitz 551769ce76dSAlexander Graf s->in_use = 0; 5522bb5c936SPaolo Bonzini 553709f2132SPaolo Bonzini qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex); 554769ce76dSAlexander Graf } 555769ce76dSAlexander Graf 5568e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options, 5578e6d58cdSKevin Wolf Error **errp) 558769ce76dSAlexander Graf { 55946f5ac20SEric Blake qdict_put_str(options, CURL_BLOCK_OPT_URL, filename); 5608e6d58cdSKevin Wolf } 5618e6d58cdSKevin Wolf 56263f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs) 56363f0f45fSStefan Hajnoczi { 56463f0f45fSStefan Hajnoczi BDRVCURLState *s = bs->opaque; 56563f0f45fSStefan Hajnoczi int i; 56663f0f45fSStefan Hajnoczi 567456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 56863f0f45fSStefan Hajnoczi for (i = 0; i < CURL_NUM_STATES; i++) { 56963f0f45fSStefan Hajnoczi if (s->states[i].in_use) { 57063f0f45fSStefan Hajnoczi curl_clean_state(&s->states[i]); 57163f0f45fSStefan Hajnoczi } 57263f0f45fSStefan Hajnoczi if (s->states[i].curl) { 57363f0f45fSStefan Hajnoczi curl_easy_cleanup(s->states[i].curl); 57463f0f45fSStefan Hajnoczi s->states[i].curl = NULL; 57563f0f45fSStefan Hajnoczi } 57663f0f45fSStefan Hajnoczi g_free(s->states[i].orig_buf); 57763f0f45fSStefan Hajnoczi s->states[i].orig_buf = NULL; 57863f0f45fSStefan Hajnoczi } 57963f0f45fSStefan Hajnoczi if (s->multi) { 58063f0f45fSStefan Hajnoczi curl_multi_cleanup(s->multi); 58163f0f45fSStefan Hajnoczi s->multi = NULL; 58263f0f45fSStefan Hajnoczi } 583456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 58463f0f45fSStefan Hajnoczi 58563f0f45fSStefan Hajnoczi timer_del(&s->timer); 58663f0f45fSStefan Hajnoczi } 58763f0f45fSStefan Hajnoczi 58863f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs, 58963f0f45fSStefan Hajnoczi AioContext *new_context) 59063f0f45fSStefan Hajnoczi { 59163f0f45fSStefan Hajnoczi BDRVCURLState *s = bs->opaque; 59263f0f45fSStefan Hajnoczi 59363f0f45fSStefan Hajnoczi aio_timer_init(new_context, &s->timer, 59463f0f45fSStefan Hajnoczi QEMU_CLOCK_REALTIME, SCALE_NS, 59563f0f45fSStefan Hajnoczi curl_multi_timeout_do, s); 59663f0f45fSStefan Hajnoczi 59763f0f45fSStefan Hajnoczi assert(!s->multi); 59863f0f45fSStefan Hajnoczi s->multi = curl_multi_init(); 59963f0f45fSStefan Hajnoczi s->aio_context = new_context; 60063f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); 60163f0f45fSStefan Hajnoczi #ifdef NEED_CURL_TIMER_CALLBACK 60263f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); 60363f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); 60463f0f45fSStefan Hajnoczi #endif 60563f0f45fSStefan Hajnoczi } 60663f0f45fSStefan Hajnoczi 6078e6d58cdSKevin Wolf static QemuOptsList runtime_opts = { 6088e6d58cdSKevin Wolf .name = "curl", 6098e6d58cdSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 6108e6d58cdSKevin Wolf .desc = { 6118e6d58cdSKevin Wolf { 612e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_URL, 6138e6d58cdSKevin Wolf .type = QEMU_OPT_STRING, 6148e6d58cdSKevin Wolf .help = "URL to open", 6158e6d58cdSKevin Wolf }, 6168e6d58cdSKevin Wolf { 617e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_READAHEAD, 6188e6d58cdSKevin Wolf .type = QEMU_OPT_SIZE, 6198e6d58cdSKevin Wolf .help = "Readahead size", 6208e6d58cdSKevin Wolf }, 62197a3ea57SMatthew Booth { 62297a3ea57SMatthew Booth .name = CURL_BLOCK_OPT_SSLVERIFY, 62397a3ea57SMatthew Booth .type = QEMU_OPT_BOOL, 62497a3ea57SMatthew Booth .help = "Verify SSL certificate" 62597a3ea57SMatthew Booth }, 626212aefaaSDaniel Henrique Barboza { 627212aefaaSDaniel Henrique Barboza .name = CURL_BLOCK_OPT_TIMEOUT, 628212aefaaSDaniel Henrique Barboza .type = QEMU_OPT_NUMBER, 629212aefaaSDaniel Henrique Barboza .help = "Curl timeout" 630212aefaaSDaniel Henrique Barboza }, 631a94f83d9SRichard W.M. Jones { 632a94f83d9SRichard W.M. Jones .name = CURL_BLOCK_OPT_COOKIE, 633a94f83d9SRichard W.M. Jones .type = QEMU_OPT_STRING, 634a94f83d9SRichard W.M. Jones .help = "Pass the cookie or list of cookies with each request" 635a94f83d9SRichard W.M. Jones }, 6361bff9606SDaniel P. Berrange { 637327c8ebdSPeter Krempa .name = CURL_BLOCK_OPT_COOKIE_SECRET, 638327c8ebdSPeter Krempa .type = QEMU_OPT_STRING, 639327c8ebdSPeter Krempa .help = "ID of secret used as cookie passed with each request" 640327c8ebdSPeter Krempa }, 641327c8ebdSPeter Krempa { 6421bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_USERNAME, 6431bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6441bff9606SDaniel P. Berrange .help = "Username for HTTP auth" 6451bff9606SDaniel P. Berrange }, 6461bff9606SDaniel P. Berrange { 6471bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PASSWORD_SECRET, 6481bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6491bff9606SDaniel P. Berrange .help = "ID of secret used as password for HTTP auth", 6501bff9606SDaniel P. Berrange }, 6511bff9606SDaniel P. Berrange { 6521bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PROXY_USERNAME, 6531bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6541bff9606SDaniel P. Berrange .help = "Username for HTTP proxy auth" 6551bff9606SDaniel P. Berrange }, 6561bff9606SDaniel P. Berrange { 6571bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, 6581bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6591bff9606SDaniel P. Berrange .help = "ID of secret used as password for HTTP proxy auth", 6601bff9606SDaniel P. Berrange }, 6618e6d58cdSKevin Wolf { /* end of list */ } 6628e6d58cdSKevin Wolf }, 6638e6d58cdSKevin Wolf }; 6648e6d58cdSKevin Wolf 6651bff9606SDaniel P. Berrange 666015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags, 667015a1036SMax Reitz Error **errp) 6688e6d58cdSKevin Wolf { 6698e6d58cdSKevin Wolf BDRVCURLState *s = bs->opaque; 6708e6d58cdSKevin Wolf CURLState *state = NULL; 6718e6d58cdSKevin Wolf QemuOpts *opts; 6728e6d58cdSKevin Wolf Error *local_err = NULL; 6738e6d58cdSKevin Wolf const char *file; 674a94f83d9SRichard W.M. Jones const char *cookie; 675327c8ebdSPeter Krempa const char *cookie_secret; 6768e6d58cdSKevin Wolf double d; 6771bff9606SDaniel P. Berrange const char *secretid; 67834634ca2SMax Reitz const char *protocol_delimiter; 6792d25964dSJeff Cody int ret; 6808e6d58cdSKevin Wolf 6816ceef36aSKevin Wolf ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes", 6826ceef36aSKevin Wolf errp); 6836ceef36aSKevin Wolf if (ret < 0) { 6846ceef36aSKevin Wolf return ret; 685a7cea2baSRichard W.M. Jones } 686a7cea2baSRichard W.M. Jones 6872d25964dSJeff Cody if (!libcurl_initialized) { 6882d25964dSJeff Cody ret = curl_global_init(CURL_GLOBAL_ALL); 6892d25964dSJeff Cody if (ret) { 6902d25964dSJeff Cody error_setg(errp, "libcurl initialization failed with %d", ret); 6912d25964dSJeff Cody return -EIO; 6922d25964dSJeff Cody } 6932d25964dSJeff Cody libcurl_initialized = true; 6942d25964dSJeff Cody } 6952d25964dSJeff Cody 696456af346SPaolo Bonzini qemu_mutex_init(&s->mutex); 69787ea75d5SPeter Crosthwaite opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 6988e6d58cdSKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 69984d18f06SMarkus Armbruster if (local_err) { 7002a94fee3SPaolo Bonzini error_propagate(errp, local_err); 7018e6d58cdSKevin Wolf goto out_noclean; 7028e6d58cdSKevin Wolf } 7038e6d58cdSKevin Wolf 704e3542c67SMatthew Booth s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, 705712b64e8SMax Reitz CURL_BLOCK_OPT_READAHEAD_DEFAULT); 706c76f4952SNolan if ((s->readahead_size & 0x1ff) != 0) { 7072a94fee3SPaolo Bonzini error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", 708c76f4952SNolan s->readahead_size); 709c76f4952SNolan goto out_noclean; 710c76f4952SNolan } 711c76f4952SNolan 712212aefaaSDaniel Henrique Barboza s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, 713712b64e8SMax Reitz CURL_BLOCK_OPT_TIMEOUT_DEFAULT); 714f76faedaSRichard W.M. Jones if (s->timeout > CURL_TIMEOUT_MAX) { 715f76faedaSRichard W.M. Jones error_setg(errp, "timeout parameter is too large or negative"); 716f76faedaSRichard W.M. Jones goto out_noclean; 717f76faedaSRichard W.M. Jones } 718212aefaaSDaniel Henrique Barboza 719712b64e8SMax Reitz s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, 720712b64e8SMax Reitz CURL_BLOCK_OPT_SSLVERIFY_DEFAULT); 72197a3ea57SMatthew Booth 722a94f83d9SRichard W.M. Jones cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); 723327c8ebdSPeter Krempa cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET); 724327c8ebdSPeter Krempa 725327c8ebdSPeter Krempa if (cookie && cookie_secret) { 726327c8ebdSPeter Krempa error_setg(errp, 727327c8ebdSPeter Krempa "curl driver cannot handle both cookie and cookie secret"); 728327c8ebdSPeter Krempa goto out_noclean; 729327c8ebdSPeter Krempa } 730327c8ebdSPeter Krempa 731327c8ebdSPeter Krempa if (cookie_secret) { 732327c8ebdSPeter Krempa s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp); 733327c8ebdSPeter Krempa if (!s->cookie) { 734327c8ebdSPeter Krempa goto out_noclean; 735327c8ebdSPeter Krempa } 736327c8ebdSPeter Krempa } else { 737a94f83d9SRichard W.M. Jones s->cookie = g_strdup(cookie); 738327c8ebdSPeter Krempa } 739a94f83d9SRichard W.M. Jones 740e3542c67SMatthew Booth file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); 7418e6d58cdSKevin Wolf if (file == NULL) { 7422a94fee3SPaolo Bonzini error_setg(errp, "curl block driver requires an 'url' option"); 7438e6d58cdSKevin Wolf goto out_noclean; 7448e6d58cdSKevin Wolf } 7458e6d58cdSKevin Wolf 74634634ca2SMax Reitz if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) || 74734634ca2SMax Reitz !strstart(protocol_delimiter, "://", NULL)) 74834634ca2SMax Reitz { 74934634ca2SMax Reitz error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not " 75034634ca2SMax Reitz "start with '%s://')", bs->drv->protocol_name, file, 75134634ca2SMax Reitz bs->drv->protocol_name); 75234634ca2SMax Reitz goto out_noclean; 75334634ca2SMax Reitz } 75434634ca2SMax Reitz 7551bff9606SDaniel P. Berrange s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME)); 7561bff9606SDaniel P. Berrange secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET); 7571bff9606SDaniel P. Berrange 7581bff9606SDaniel P. Berrange if (secretid) { 7591bff9606SDaniel P. Berrange s->password = qcrypto_secret_lookup_as_utf8(secretid, errp); 7601bff9606SDaniel P. Berrange if (!s->password) { 7611bff9606SDaniel P. Berrange goto out_noclean; 7621bff9606SDaniel P. Berrange } 7631bff9606SDaniel P. Berrange } 7641bff9606SDaniel P. Berrange 7651bff9606SDaniel P. Berrange s->proxyusername = g_strdup( 7661bff9606SDaniel P. Berrange qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME)); 7671bff9606SDaniel P. Berrange secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET); 7681bff9606SDaniel P. Berrange if (secretid) { 7691bff9606SDaniel P. Berrange s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp); 7701bff9606SDaniel P. Berrange if (!s->proxypassword) { 7711bff9606SDaniel P. Berrange goto out_noclean; 7721bff9606SDaniel P. Berrange } 7731bff9606SDaniel P. Berrange } 7741bff9606SDaniel P. Berrange 775ed2a66deSLaurent Vivier trace_curl_open(file); 776709f2132SPaolo Bonzini qemu_co_queue_init(&s->free_state_waitq); 77763f0f45fSStefan Hajnoczi s->aio_context = bdrv_get_aio_context(bs); 7788e6d58cdSKevin Wolf s->url = g_strdup(file); 779456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 7803ce6a729SPaolo Bonzini state = curl_find_state(s); 781456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 7823ce6a729SPaolo Bonzini if (!state) { 783769ce76dSAlexander Graf goto out_noclean; 7843ce6a729SPaolo Bonzini } 785769ce76dSAlexander Graf 786769ce76dSAlexander Graf // Get file size 787769ce76dSAlexander Graf 7883ce6a729SPaolo Bonzini if (curl_init_state(s, state) < 0) { 7893ce6a729SPaolo Bonzini goto out; 7903ce6a729SPaolo Bonzini } 7913ce6a729SPaolo Bonzini 7923494d650SFam Zheng s->accept_range = false; 793769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); 7943494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, 7953494d650SFam Zheng curl_header_cb); 7963494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); 797769ce76dSAlexander Graf if (curl_easy_perform(state->curl)) 798769ce76dSAlexander Graf goto out; 799a41c4578STomáš Golembiovský if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { 800769ce76dSAlexander Graf goto out; 801a41c4578STomáš Golembiovský } 802a41c4578STomáš Golembiovský /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not 803a41c4578STomáš Golembiovský * know or the size is zero. From 7.19.4 CURL returns -1 if size is not 80450d6a8a3SStefan Weil * known and zero if it is really zero-length file. */ 805a41c4578STomáš Golembiovský #if LIBCURL_VERSION_NUM >= 0x071304 806a41c4578STomáš Golembiovský if (d < 0) { 807a41c4578STomáš Golembiovský pstrcpy(state->errmsg, CURL_ERROR_SIZE, 808a41c4578STomáš Golembiovský "Server didn't report file size."); 809a41c4578STomáš Golembiovský goto out; 810a41c4578STomáš Golembiovský } 811a41c4578STomáš Golembiovský #else 812a41c4578STomáš Golembiovský if (d <= 0) { 813a41c4578STomáš Golembiovský pstrcpy(state->errmsg, CURL_ERROR_SIZE, 814a41c4578STomáš Golembiovský "Unknown file size or zero-length file."); 815a41c4578STomáš Golembiovský goto out; 816a41c4578STomáš Golembiovský } 817a41c4578STomáš Golembiovský #endif 818a41c4578STomáš Golembiovský 8192125e5eaSPaolo Bonzini s->len = d; 820a41c4578STomáš Golembiovský 8213494d650SFam Zheng if ((!strncasecmp(s->url, "http://", strlen("http://")) 8223494d650SFam Zheng || !strncasecmp(s->url, "https://", strlen("https://"))) 8233494d650SFam Zheng && !s->accept_range) { 8243494d650SFam Zheng pstrcpy(state->errmsg, CURL_ERROR_SIZE, 8253494d650SFam Zheng "Server does not support 'range' (byte ranges)."); 8263494d650SFam Zheng goto out; 8273494d650SFam Zheng } 828ed2a66deSLaurent Vivier trace_curl_open_size(s->len); 829769ce76dSAlexander Graf 830456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 831769ce76dSAlexander Graf curl_clean_state(state); 832456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 833769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 834769ce76dSAlexander Graf state->curl = NULL; 835769ce76dSAlexander Graf 83663f0f45fSStefan Hajnoczi curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); 837769ce76dSAlexander Graf 8388e6d58cdSKevin Wolf qemu_opts_del(opts); 839769ce76dSAlexander Graf return 0; 840769ce76dSAlexander Graf 841769ce76dSAlexander Graf out: 842acd7fdc6SMaria Kustova error_setg(errp, "CURL: Error opening file: %s", state->errmsg); 843769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 844769ce76dSAlexander Graf state->curl = NULL; 845769ce76dSAlexander Graf out_noclean: 846456af346SPaolo Bonzini qemu_mutex_destroy(&s->mutex); 847a94f83d9SRichard W.M. Jones g_free(s->cookie); 8488e6d58cdSKevin Wolf g_free(s->url); 849996922deSJeff Cody g_free(s->username); 850996922deSJeff Cody g_free(s->proxyusername); 851996922deSJeff Cody g_free(s->proxypassword); 8528e6d58cdSKevin Wolf qemu_opts_del(opts); 853769ce76dSAlexander Graf return -EINVAL; 854769ce76dSAlexander Graf } 855769ce76dSAlexander Graf 85628256d82SPaolo Bonzini static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) 857769ce76dSAlexander Graf { 858769ce76dSAlexander Graf CURLState *state; 859b69cdef8SMatthew Booth int running; 860769ce76dSAlexander Graf 8611919631eSPaolo Bonzini BDRVCURLState *s = bs->opaque; 862769ce76dSAlexander Graf 8632125e5eaSPaolo Bonzini uint64_t start = acb->offset; 8642125e5eaSPaolo Bonzini uint64_t end; 865769ce76dSAlexander Graf 866ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->mutex); 8671919631eSPaolo Bonzini 868769ce76dSAlexander Graf // In case we have the requested data already (e.g. read-ahead), 869769ce76dSAlexander Graf // we can just call the callback and be done. 87028256d82SPaolo Bonzini if (curl_find_buf(s, start, acb->bytes, acb)) { 87134db05e7SPaolo Bonzini goto out; 872769ce76dSAlexander Graf } 873769ce76dSAlexander Graf 874769ce76dSAlexander Graf // No cache found, so let's start a new request 8753ce6a729SPaolo Bonzini for (;;) { 8763ce6a729SPaolo Bonzini state = curl_find_state(s); 8773ce6a729SPaolo Bonzini if (state) { 8783ce6a729SPaolo Bonzini break; 8793ce6a729SPaolo Bonzini } 880709f2132SPaolo Bonzini qemu_co_queue_wait(&s->free_state_waitq, &s->mutex); 8813ce6a729SPaolo Bonzini } 8823ce6a729SPaolo Bonzini 8833ce6a729SPaolo Bonzini if (curl_init_state(s, state) < 0) { 8843ce6a729SPaolo Bonzini curl_clean_state(state); 88528256d82SPaolo Bonzini acb->ret = -EIO; 8861919631eSPaolo Bonzini goto out; 887363c3c85SNick Thomas } 888769ce76dSAlexander Graf 889769ce76dSAlexander Graf acb->start = 0; 8902125e5eaSPaolo Bonzini acb->end = MIN(acb->bytes, s->len - start); 891769ce76dSAlexander Graf 892769ce76dSAlexander Graf state->buf_off = 0; 8937267c094SAnthony Liguori g_free(state->orig_buf); 894769ce76dSAlexander Graf state->buf_start = start; 8954e504535SMax Reitz state->buf_len = MIN(acb->end + s->readahead_size, s->len - start); 8964e504535SMax Reitz end = start + state->buf_len - 1; 8978dc7a772SKevin Wolf state->orig_buf = g_try_malloc(state->buf_len); 8988dc7a772SKevin Wolf if (state->buf_len && state->orig_buf == NULL) { 8998dc7a772SKevin Wolf curl_clean_state(state); 90028256d82SPaolo Bonzini acb->ret = -ENOMEM; 9011919631eSPaolo Bonzini goto out; 9028dc7a772SKevin Wolf } 903769ce76dSAlexander Graf state->acb[0] = acb; 904769ce76dSAlexander Graf 9052125e5eaSPaolo Bonzini snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end); 906ed2a66deSLaurent Vivier trace_curl_setup_preadv(acb->bytes, start, state->range); 907769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); 908769ce76dSAlexander Graf 909769ce76dSAlexander Graf curl_multi_add_handle(s->multi, state->curl); 910769ce76dSAlexander Graf 911b69cdef8SMatthew Booth /* Tell curl it needs to kick things off */ 912b69cdef8SMatthew Booth curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 9131919631eSPaolo Bonzini 9141919631eSPaolo Bonzini out: 915ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 916363c3c85SNick Thomas } 917363c3c85SNick Thomas 91828256d82SPaolo Bonzini static int coroutine_fn curl_co_preadv(BlockDriverState *bs, 91928256d82SPaolo Bonzini uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) 920363c3c85SNick Thomas { 92128256d82SPaolo Bonzini CURLAIOCB acb = { 92228256d82SPaolo Bonzini .co = qemu_coroutine_self(), 92328256d82SPaolo Bonzini .ret = -EINPROGRESS, 92428256d82SPaolo Bonzini .qiov = qiov, 92528256d82SPaolo Bonzini .offset = offset, 92628256d82SPaolo Bonzini .bytes = bytes 92728256d82SPaolo Bonzini }; 928363c3c85SNick Thomas 92928256d82SPaolo Bonzini curl_setup_preadv(bs, &acb); 93028256d82SPaolo Bonzini while (acb.ret == -EINPROGRESS) { 93128256d82SPaolo Bonzini qemu_coroutine_yield(); 93228256d82SPaolo Bonzini } 93328256d82SPaolo Bonzini return acb.ret; 934769ce76dSAlexander Graf } 935769ce76dSAlexander Graf 936769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs) 937769ce76dSAlexander Graf { 938769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 939769ce76dSAlexander Graf 940ed2a66deSLaurent Vivier trace_curl_close(); 94163f0f45fSStefan Hajnoczi curl_detach_aio_context(bs); 942ba3186c4SPaolo Bonzini qemu_mutex_destroy(&s->mutex); 943031fd1beSPeter Maydell 944a94f83d9SRichard W.M. Jones g_free(s->cookie); 94545724d6dSStefan Weil g_free(s->url); 946996922deSJeff Cody g_free(s->username); 947996922deSJeff Cody g_free(s->proxyusername); 948996922deSJeff Cody g_free(s->proxypassword); 949769ce76dSAlexander Graf } 950769ce76dSAlexander Graf 951769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs) 952769ce76dSAlexander Graf { 953769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 954769ce76dSAlexander Graf return s->len; 955769ce76dSAlexander Graf } 956769ce76dSAlexander Graf 957937c007bSMax Reitz static void curl_refresh_filename(BlockDriverState *bs) 958937c007bSMax Reitz { 959937c007bSMax Reitz BDRVCURLState *s = bs->opaque; 960937c007bSMax Reitz 961937c007bSMax Reitz /* "readahead" and "timeout" do not change the guest-visible data, 962937c007bSMax Reitz * so ignore them */ 963937c007bSMax Reitz if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT || 964937c007bSMax Reitz s->cookie || s->username || s->password || s->proxyusername || 965937c007bSMax Reitz s->proxypassword) 966937c007bSMax Reitz { 967937c007bSMax Reitz return; 968937c007bSMax Reitz } 969937c007bSMax Reitz 970937c007bSMax Reitz pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url); 971937c007bSMax Reitz } 972937c007bSMax Reitz 973937c007bSMax Reitz 9742654267cSMax Reitz static const char *const curl_strong_runtime_opts[] = { 9752654267cSMax Reitz CURL_BLOCK_OPT_URL, 9762654267cSMax Reitz CURL_BLOCK_OPT_SSLVERIFY, 9772654267cSMax Reitz CURL_BLOCK_OPT_COOKIE, 9782654267cSMax Reitz CURL_BLOCK_OPT_COOKIE_SECRET, 9792654267cSMax Reitz CURL_BLOCK_OPT_USERNAME, 9802654267cSMax Reitz CURL_BLOCK_OPT_PASSWORD_SECRET, 9812654267cSMax Reitz CURL_BLOCK_OPT_PROXY_USERNAME, 9822654267cSMax Reitz CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, 9832654267cSMax Reitz 9842654267cSMax Reitz NULL 9852654267cSMax Reitz }; 9862654267cSMax Reitz 987769ce76dSAlexander Graf static BlockDriver bdrv_http = { 988769ce76dSAlexander Graf .format_name = "http", 989769ce76dSAlexander Graf .protocol_name = "http", 990769ce76dSAlexander Graf 991769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 9928e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 99366f82ceeSKevin Wolf .bdrv_file_open = curl_open, 994769ce76dSAlexander Graf .bdrv_close = curl_close, 995769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 996769ce76dSAlexander Graf 99728256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 99863f0f45fSStefan Hajnoczi 99963f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 100063f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 10012654267cSMax Reitz 1002937c007bSMax Reitz .bdrv_refresh_filename = curl_refresh_filename, 10032654267cSMax Reitz .strong_runtime_opts = curl_strong_runtime_opts, 1004769ce76dSAlexander Graf }; 1005769ce76dSAlexander Graf 1006769ce76dSAlexander Graf static BlockDriver bdrv_https = { 1007769ce76dSAlexander Graf .format_name = "https", 1008769ce76dSAlexander Graf .protocol_name = "https", 1009769ce76dSAlexander Graf 1010769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 10118e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 101266f82ceeSKevin Wolf .bdrv_file_open = curl_open, 1013769ce76dSAlexander Graf .bdrv_close = curl_close, 1014769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 1015769ce76dSAlexander Graf 101628256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 101763f0f45fSStefan Hajnoczi 101863f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 101963f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 10202654267cSMax Reitz 1021937c007bSMax Reitz .bdrv_refresh_filename = curl_refresh_filename, 10222654267cSMax Reitz .strong_runtime_opts = curl_strong_runtime_opts, 1023769ce76dSAlexander Graf }; 1024769ce76dSAlexander Graf 1025769ce76dSAlexander Graf static BlockDriver bdrv_ftp = { 1026769ce76dSAlexander Graf .format_name = "ftp", 1027769ce76dSAlexander Graf .protocol_name = "ftp", 1028769ce76dSAlexander Graf 1029769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 10308e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 103166f82ceeSKevin Wolf .bdrv_file_open = curl_open, 1032769ce76dSAlexander Graf .bdrv_close = curl_close, 1033769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 1034769ce76dSAlexander Graf 103528256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 103663f0f45fSStefan Hajnoczi 103763f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 103863f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 10392654267cSMax Reitz 1040937c007bSMax Reitz .bdrv_refresh_filename = curl_refresh_filename, 10412654267cSMax Reitz .strong_runtime_opts = curl_strong_runtime_opts, 1042769ce76dSAlexander Graf }; 1043769ce76dSAlexander Graf 1044769ce76dSAlexander Graf static BlockDriver bdrv_ftps = { 1045769ce76dSAlexander Graf .format_name = "ftps", 1046769ce76dSAlexander Graf .protocol_name = "ftps", 1047769ce76dSAlexander Graf 1048769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 10498e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 105066f82ceeSKevin Wolf .bdrv_file_open = curl_open, 1051769ce76dSAlexander Graf .bdrv_close = curl_close, 1052769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 1053769ce76dSAlexander Graf 105428256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 105563f0f45fSStefan Hajnoczi 105663f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 105763f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 10582654267cSMax Reitz 1059937c007bSMax Reitz .bdrv_refresh_filename = curl_refresh_filename, 10602654267cSMax Reitz .strong_runtime_opts = curl_strong_runtime_opts, 1061769ce76dSAlexander Graf }; 1062769ce76dSAlexander Graf 1063769ce76dSAlexander Graf static void curl_block_init(void) 1064769ce76dSAlexander Graf { 1065769ce76dSAlexander Graf bdrv_register(&bdrv_http); 1066769ce76dSAlexander Graf bdrv_register(&bdrv_https); 1067769ce76dSAlexander Graf bdrv_register(&bdrv_ftp); 1068769ce76dSAlexander Graf bdrv_register(&bdrv_ftps); 1069769ce76dSAlexander Graf } 1070769ce76dSAlexander Graf 1071769ce76dSAlexander Graf block_init(curl_block_init); 1072