From patchwork Thu Jun 6 06:25:51 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 249281 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 88D412C0087 for ; Thu, 6 Jun 2013 16:29:07 +1000 (EST) Received: from localhost ([::1]:37055 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkTh3-0004Wo-Ig for incoming@patchwork.ozlabs.org; Thu, 06 Jun 2013 02:29:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41633) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkTfA-0001e4-JZ for qemu-devel@nongnu.org; Thu, 06 Jun 2013 02:27:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UkTf4-0004P8-7h for qemu-devel@nongnu.org; Thu, 06 Jun 2013 02:27:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56210) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkTf3-0004Ou-Ux for qemu-devel@nongnu.org; Thu, 06 Jun 2013 02:27:02 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r566R14L023125 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 6 Jun 2013 02:27:01 -0400 Received: from localhost.nay.redhat.com ([10.66.7.14]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r566Qh5L009870; Thu, 6 Jun 2013 02:26:58 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Thu, 6 Jun 2013 14:25:51 +0800 Message-Id: <1370499959-8916-6-git-send-email-famz@redhat.com> In-Reply-To: <1370499959-8916-1-git-send-email-famz@redhat.com> References: <1370499959-8916-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, jcody@redhat.com, Fam Zheng , rjones@redhat.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH v7 05/13] curl: add timer to BDRVCURLState X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org libcurl uses timer to manage ongoing sockets, it needs us to supply timer. This patch introduce QEMUTimer to BDRVCURLState and handles timeouts as libcurl expects (curl_multi_timer_cb sets given timeout value on the timer and curl_timer_cb calls curl_multi_socket_action on triggered). Signed-off-by: Fam Zheng --- block/curl.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/block/curl.c b/block/curl.c index d238489..bb46c8f 100644 --- a/block/curl.c +++ b/block/curl.c @@ -89,6 +89,7 @@ typedef struct BDRVCURLState { QLIST_HEAD(, CURLSockInfo) socks; char *url; size_t readahead_size; + QEMUTimer *timer; /* Whether http server accept range in header */ bool accept_range; } BDRVCURLState; @@ -148,6 +149,38 @@ static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) return realsize; } +static void curl_timer_cb(void *opaque) +{ + int running; + BDRVCURLState *s = opaque; + DPRINTF("curl timeout!\n"); + curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); +} + +/* Call back for curl_multi interface */ +static int curl_multi_timer_cb(CURLM *multi, long timeout_ms, void *s_) +{ + BDRVCURLState *s = s_; + DPRINTF("curl multi timer cb, timeout: %ld (ms)\n", timeout_ms); + if (timeout_ms < 0) { + if (s->timer) { + qemu_del_timer(s->timer); + qemu_free_timer(s->timer); + s->timer = NULL; + } + } else if (timeout_ms == 0) { + curl_timer_cb(s); + } else { + if (!s->timer) { + s->timer = qemu_new_timer_ms(host_clock, curl_timer_cb, s); + assert(s->timer); + } + qemu_mod_timer(s->timer, qemu_get_clock_ms(host_clock) + timeout_ms); + } + + return 0; +} + static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) { CURLState *s = ((CURLState*)opaque); @@ -509,6 +542,8 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags) } curl_multi_setopt(s->multi, CURLMOPT_SOCKETDATA, s); curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); + curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); + curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_multi_timer_cb); curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); qemu_opts_del(opts); @@ -634,6 +669,13 @@ static void curl_close(BlockDriverState *bs) int i; DPRINTF("CURL: Close\n"); + + if (s->timer) { + qemu_del_timer(s->timer); + qemu_free_timer(s->timer); + s->timer = NULL; + } + for (i=0; istates[i].in_use) curl_clean_state(&s->states[i]);