From patchwork Thu Feb 13 12:03:13 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonios Motakis X-Patchwork-Id: 319987 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)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 29DC22C00B5 for ; Thu, 13 Feb 2014 23:06:35 +1100 (EST) Received: from localhost ([::1]:45728 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WDv3o-0002By-UI for incoming@patchwork.ozlabs.org; Thu, 13 Feb 2014 07:06:32 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39587) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WDv1K-0007OD-Lc for qemu-devel@nongnu.org; Thu, 13 Feb 2014 07:04:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WDv1D-00011f-64 for qemu-devel@nongnu.org; Thu, 13 Feb 2014 07:03:58 -0500 Received: from mail-wi0-f178.google.com ([209.85.212.178]:56614) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WDv1C-00011G-Tg for qemu-devel@nongnu.org; Thu, 13 Feb 2014 07:03:51 -0500 Received: by mail-wi0-f178.google.com with SMTP id cc10so8422485wib.17 for ; Thu, 13 Feb 2014 04:03:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=85KgDuch98rT4WTvj3KIvMU2/crf456lmAzD6WqX/ks=; b=hjtoSoFQyHPFX068y/wcnUW5OCiX5mMVulslxPUbLoPetC4BS3VGTmaiukrTq17OuG GzRGl+BELZTCff4thBABbS6UaTAIL55QRmun6+/hVP0EupHprbEWMbJCMVMhKvtj/NKb r3TwH/ATMuAMB0VqitRWCtCFNniiC/eiVOHE5LfJSOoRosfaxc9M9DGm3SBtOvoincS0 jDa3ih6AFJz5Jwpibw2Sgm8r6ccnXEWU7JDr84m0iwB5tOUOcQhEI7q/+nEcrGZRU0Hv EGK/T+h3ywGH0Ur0veY8bS40PffijNGbNeLu8UK7dZdsx+H/zBppJRNg/U6L7cE0hE9Q Zw7Q== X-Gm-Message-State: ALoCoQlge0SrfHTtcsjn4/JR+XEL/hxv4YcOgSYcGU1IrguAhfy49wigzLy+vEF31PCa3IZAp9/C X-Received: by 10.195.12.200 with SMTP id es8mr918782wjd.77.1392293030145; Thu, 13 Feb 2014 04:03:50 -0800 (PST) Received: from localhost.localdomain (home.tvelocity.eu. [82.67.68.96]) by mx.google.com with ESMTPSA id bj3sm4039419wjb.14.2014.02.13.04.03.47 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 13 Feb 2014 04:03:49 -0800 (PST) From: Antonios Motakis To: qemu-devel@nongnu.org, snabb-devel@googlegroups.com Date: Thu, 13 Feb 2014 13:03:13 +0100 Message-Id: <1392293009-13812-3-git-send-email-a.motakis@virtualopensystems.com> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1392293009-13812-1-git-send-email-a.motakis@virtualopensystems.com> References: <1392293009-13812-1-git-send-email-a.motakis@virtualopensystems.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.85.212.178 Cc: mst@redhat.com, Amit Shah , Michael Roth , n.nikolaev@virtualopensystems.com, Hans de Goede , Gerd Hoffmann , Anthony Liguori , lukego@gmail.com, Antonios Motakis , tech@virtualopensystems.com Subject: [Qemu-devel] [PATCH v8 02/17] Add chardev API qemu_chr_fe_read_all 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 This function will attempt to read data from the chardev trying to fill the buffer up to the given length. Add tcp_chr_disconnect to reuse disconnection code where needed. Signed-off-by: Antonios Motakis Signed-off-by: Nikolay Nikolaev --- include/sysemu/char.h | 14 +++++++++ qemu-char.c | 83 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/include/sysemu/char.h b/include/sysemu/char.h index b81a6ff..9981a6a 100644 --- a/include/sysemu/char.h +++ b/include/sysemu/char.h @@ -56,6 +56,8 @@ typedef void IOEventHandler(void *opaque, int event); struct CharDriverState { void (*init)(struct CharDriverState *s); int (*chr_write)(struct CharDriverState *s, const uint8_t *buf, int len); + int (*chr_sync_read)(struct CharDriverState *s, + const uint8_t *buf, int len); GSource *(*chr_add_watch)(struct CharDriverState *s, GIOCondition cond); void (*chr_update_read_handler)(struct CharDriverState *s); int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg); @@ -189,6 +191,18 @@ int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len); int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len); /** + * @qemu_chr_fe_read_all: + * + * Read data to a buffer from the back end. + * + * @buf the data buffer + * @len the number of bytes to read + * + * Returns: the number of bytes read + */ +int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len); + +/** * @qemu_chr_fe_ioctl: * * Issue a device specific ioctl to a backend. diff --git a/qemu-char.c b/qemu-char.c index 30c5a6a..fe0636f 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -84,6 +84,7 @@ #include "ui/qemu-spice.h" #define READ_BUF_LEN 4096 +#define READ_RETRIES 10 /***********************************************************/ /* character device */ @@ -145,6 +146,41 @@ int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len) return offset; } +int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len) +{ + int offset = 0, counter = 10; + int res; + + if (!s->chr_sync_read) { + return 0; + } + + while (offset < len) { + do { + res = s->chr_sync_read(s, buf + offset, len - offset); + if (res == -1 && errno == EAGAIN) { + g_usleep(100); + } + } while (res == -1 && errno == EAGAIN); + + if (res == 0) { + break; + } + + if (res < 0) { + return res; + } + + offset += res; + + if (!counter--) { + break; + } + } + + return offset; +} + int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg) { if (!s->chr_ioctl) @@ -2453,6 +2489,23 @@ static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond) return g_io_create_watch(s->chan, cond); } +static void tcp_chr_disconnect(CharDriverState *chr) +{ + TCPCharDriver *s = chr->opaque; + + s->connected = 0; + if (s->listen_chan) { + s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, + tcp_chr_accept, chr); + } + remove_fd_in_watch(chr); + g_io_channel_unref(s->chan); + s->chan = NULL; + closesocket(s->fd); + s->fd = -1; + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); +} + static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) { CharDriverState *chr = opaque; @@ -2469,16 +2522,7 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) size = tcp_chr_recv(chr, (void *)buf, len); if (size == 0) { /* connection closed */ - s->connected = 0; - if (s->listen_chan) { - s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr); - } - remove_fd_in_watch(chr); - g_io_channel_unref(s->chan); - s->chan = NULL; - closesocket(s->fd); - s->fd = -1; - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); + tcp_chr_disconnect(chr); } else if (size > 0) { if (s->do_telnetopt) tcp_chr_process_IAC_bytes(chr, s, buf, &size); @@ -2489,6 +2533,24 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) return TRUE; } +static int tcp_chr_sync_read(CharDriverState *chr, const uint8_t *buf, int len) +{ + TCPCharDriver *s = chr->opaque; + int size; + + if (!s->connected) { + return 0; + } + + size = tcp_chr_recv(chr, (void *) buf, len); + if (size == 0) { + /* connection closed */ + tcp_chr_disconnect(chr); + } + + return size; +} + #ifndef _WIN32 CharDriverState *qemu_chr_open_eventfd(int eventfd) { @@ -2660,6 +2722,7 @@ static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay, chr->opaque = s; chr->chr_write = tcp_chr_write; + chr->chr_sync_read = tcp_chr_sync_read; chr->chr_close = tcp_chr_close; chr->get_msgfd = tcp_get_msgfd; chr->chr_add_client = tcp_chr_add_client;