From patchwork Wed Apr 7 21:02:36 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Shah X-Patchwork-Id: 49650 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 40614B7D1F for ; Thu, 8 Apr 2010 07:20:06 +1000 (EST) Received: from localhost ([127.0.0.1]:37714 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NzcfK-0000rS-Ok for incoming@patchwork.ozlabs.org; Wed, 07 Apr 2010 17:20:02 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NzcQV-0004GI-Au for qemu-devel@nongnu.org; Wed, 07 Apr 2010 17:04:43 -0400 Received: from [140.186.70.92] (port=35188 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NzcQT-0004FN-IH for qemu-devel@nongnu.org; Wed, 07 Apr 2010 17:04:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NzcQR-0003En-5G for qemu-devel@nongnu.org; Wed, 07 Apr 2010 17:04:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:8862) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NzcQQ-0003Eh-Lz for qemu-devel@nongnu.org; Wed, 07 Apr 2010 17:04:39 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o37L4V3C014003 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 7 Apr 2010 17:04:31 -0400 Received: from localhost (vpn-252-12.phx2.redhat.com [10.3.252.12]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o37L4TcG029598; Wed, 7 Apr 2010 17:04:30 -0400 From: Amit Shah To: qemu list Date: Thu, 8 Apr 2010 02:32:36 +0530 Message-Id: <1270674156-9345-9-git-send-email-amit.shah@redhat.com> In-Reply-To: <1270674156-9345-8-git-send-email-amit.shah@redhat.com> References: <1270674156-9345-1-git-send-email-amit.shah@redhat.com> <1270674156-9345-2-git-send-email-amit.shah@redhat.com> <1270674156-9345-3-git-send-email-amit.shah@redhat.com> <1270674156-9345-4-git-send-email-amit.shah@redhat.com> <1270674156-9345-5-git-send-email-amit.shah@redhat.com> <1270674156-9345-6-git-send-email-amit.shah@redhat.com> <1270674156-9345-7-git-send-email-amit.shah@redhat.com> <1270674156-9345-8-git-send-email-amit.shah@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: Amit Shah , Paul Brook , "Michael S. Tsirkin" , Gerd Hoffmann , Juan Quintela Subject: [Qemu-devel] [PATCH 8/8] virtio-console: Throttle virtio-serial-bus if we can't consume any more guest data X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org If the char device we're connected to is overwhelmed with data and it can't accept any more, signal to the virtio-serial-bus to stop sending us more data till we tell otherwise. If the current buffer being processed hasn't been completely written out to the char device, we have to keep it around and re-try sending it since the virtio-serial-bus code assumes we consume the entire buffer. Also register with savevm so that we save/restore such a buffer across migration. Signed-off-by: Amit Shah --- hw/virtio-console.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 126 insertions(+), 3 deletions(-) diff --git a/hw/virtio-console.c b/hw/virtio-console.c index d7fe68b..1418a97 100644 --- a/hw/virtio-console.c +++ b/hw/virtio-console.c @@ -13,18 +13,93 @@ #include "qemu-char.h" #include "virtio-serial.h" +typedef struct Buffer { + uint8_t *buf; + size_t rem_len; + size_t offset; +} Buffer; + typedef struct VirtConsole { VirtIOSerialPort port; CharDriverState *chr; + QEMUTimer *host_timer; + Buffer *unflushed_buf; } VirtConsole; +static void add_unflushed_buf(VirtConsole *vcon, const uint8_t *buf, size_t len) +{ + vcon->unflushed_buf = qemu_malloc(sizeof(Buffer)); + vcon->unflushed_buf->buf = qemu_malloc(len); + + memcpy(vcon->unflushed_buf->buf, buf, len); + vcon->unflushed_buf->rem_len = len; + vcon->unflushed_buf->offset = 0; +} + +static void free_unflushed_buf(VirtConsole *vcon) +{ + if (vcon->unflushed_buf) { + qemu_free(vcon->unflushed_buf->buf); + qemu_free(vcon->unflushed_buf); + vcon->unflushed_buf = NULL; + } +} + +static int buffered_write_to_chardev(VirtConsole *vcon, const uint8_t *buf, + size_t len) +{ + size_t written; + ssize_t ret; + + written = 0; + do { + ret = qemu_chr_write(vcon->chr, buf + written, len - written); + if (ret < 0) { + if (vcon->unflushed_buf) { + vcon->unflushed_buf->offset += written; + vcon->unflushed_buf->rem_len -= written; + } else { + virtio_serial_throttle_port(&vcon->port, true); + add_unflushed_buf(vcon, buf + written, len - written); + } + + qemu_mod_timer(vcon->host_timer, + qemu_get_clock(host_clock) + (int64_t) 100000); + return -EAGAIN; + } + + written += ret; + } while (written != len); + + return 0; +} + +/* Callback function when the timer expires */ +static void unthrottle_port(VirtConsole *vcon) +{ + if (vcon->unflushed_buf) { + int ret; + + ret = buffered_write_to_chardev(vcon, vcon->unflushed_buf->buf + + vcon->unflushed_buf->offset, + vcon->unflushed_buf->rem_len); + if (ret < 0) { + return; + } + free_unflushed_buf(vcon); + } + virtio_serial_throttle_port(&vcon->port, false); +} /* Callback function that's called when the guest sends us data */ static void flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len) { VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); - qemu_chr_write(vcon->chr, buf, len); + /* If a previous write was incomplete, we should've been throttled. */ + assert(!vcon->unflushed_buf); + + buffered_write_to_chardev(vcon, buf, len); } /* Readiness of the guest to accept data on a port */ @@ -48,16 +123,59 @@ static void chr_event(void *opaque, int event) VirtConsole *vcon = opaque; switch (event) { - case CHR_EVENT_OPENED: { + case CHR_EVENT_OPENED: virtio_serial_open(&vcon->port); break; - } + case CHR_EVENT_CLOSED: + if (vcon->unflushed_buf) { + qemu_del_timer(vcon->host_timer); + free_unflushed_buf(vcon); + } virtio_serial_close(&vcon->port); break; } } +static void virtio_console_port_save(QEMUFile *f, void *opaque) +{ + VirtConsole *vcon = opaque; + uint32_t have_buffer; + + have_buffer = vcon->unflushed_buf ? true : false; + + qemu_put_be32s(f, &have_buffer); + if (have_buffer) { + qemu_put_be64s(f, &vcon->unflushed_buf->rem_len); + qemu_put_buffer(f, vcon->unflushed_buf->buf + + vcon->unflushed_buf->offset, + vcon->unflushed_buf->rem_len); + } +} + +static int virtio_console_port_load(QEMUFile *f, void *opaque, int version_id) +{ + VirtConsole *vcon = opaque; + uint32_t have_buffer; + + if (version_id > 1) { + return -EINVAL; + } + + qemu_get_be32s(f, &have_buffer); + if (have_buffer) { + vcon->unflushed_buf = qemu_mallocz(sizeof(Buffer)); + + qemu_get_be64s(f, &vcon->unflushed_buf->rem_len); + vcon->unflushed_buf->buf = qemu_malloc(vcon->unflushed_buf->rem_len); + vcon->unflushed_buf->offset = 0; + + qemu_get_buffer(f, vcon->unflushed_buf->buf, + vcon->unflushed_buf->rem_len); + } + return 0; +} + static int generic_port_init(VirtConsole *vcon, VirtIOSerialDevice *dev) { vcon->port.info = dev->info; @@ -67,6 +185,9 @@ static int generic_port_init(VirtConsole *vcon, VirtIOSerialDevice *dev) vcon); vcon->port.info->have_data = flush_buf; } + vcon->host_timer = qemu_new_timer(host_clock, (void *)unthrottle_port, vcon); + register_savevm("virtio-console-ports", -1, 1, virtio_console_port_save, + virtio_console_port_load, vcon); return 0; } @@ -88,7 +209,9 @@ static int virtconsole_exitfn(VirtIOSerialDevice *dev) if (vcon->chr) { port->info->have_data = NULL; qemu_chr_close(vcon->chr); + free_unflushed_buf(vcon); } + qemu_free_timer(vcon->host_timer); return 0; }