From patchwork Wed Mar 31 07:34:07 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Shah X-Patchwork-Id: 49097 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 9F091B7C67 for ; Wed, 31 Mar 2010 19:26:24 +1100 (EST) Received: from localhost ([127.0.0.1]:47403 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NwtFl-00013r-C0 for incoming@patchwork.ozlabs.org; Wed, 31 Mar 2010 04:26:21 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NwsTm-0006S9-9u for qemu-devel@nongnu.org; Wed, 31 Mar 2010 03:36:46 -0400 Received: from [140.186.70.92] (port=33387 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NwsTf-0006Oe-QY for qemu-devel@nongnu.org; Wed, 31 Mar 2010 03:36:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NwsTd-0007GF-It for qemu-devel@nongnu.org; Wed, 31 Mar 2010 03:36:39 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44376) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NwsTb-0007FU-NK for qemu-devel@nongnu.org; Wed, 31 Mar 2010 03:36:36 -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 o2V7aX8Z015766 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 31 Mar 2010 03:36:33 -0400 Received: from localhost (vpn-235-197.phx2.redhat.com [10.3.235.197]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o2V7aVmI031816; Wed, 31 Mar 2010 03:36:32 -0400 From: Amit Shah To: qemu list Date: Wed, 31 Mar 2010 13:04:07 +0530 Message-Id: <1270020848-15526-17-git-send-email-amit.shah@redhat.com> In-Reply-To: <1270020848-15526-16-git-send-email-amit.shah@redhat.com> References: <1270020848-15526-1-git-send-email-amit.shah@redhat.com> <1270020848-15526-2-git-send-email-amit.shah@redhat.com> <1270020848-15526-3-git-send-email-amit.shah@redhat.com> <1270020848-15526-4-git-send-email-amit.shah@redhat.com> <1270020848-15526-5-git-send-email-amit.shah@redhat.com> <1270020848-15526-6-git-send-email-amit.shah@redhat.com> <1270020848-15526-7-git-send-email-amit.shah@redhat.com> <1270020848-15526-8-git-send-email-amit.shah@redhat.com> <1270020848-15526-9-git-send-email-amit.shah@redhat.com> <1270020848-15526-10-git-send-email-amit.shah@redhat.com> <1270020848-15526-11-git-send-email-amit.shah@redhat.com> <1270020848-15526-12-git-send-email-amit.shah@redhat.com> <1270020848-15526-13-git-send-email-amit.shah@redhat.com> <1270020848-15526-14-git-send-email-amit.shah@redhat.com> <1270020848-15526-15-git-send-email-amit.shah@redhat.com> <1270020848-15526-16-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 , "Michael S. Tsirkin" , Gerd Hoffmann , Juan Quintela Subject: [Qemu-devel] [PATCH 16/17] virtio-serial: Discard data that guest sends us when ports aren't connected 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 Before the earlier patch, we relied on incorrect virtio api usage to signal to the guest that a particular buffer wasn't consumed by the host. After fixing that, we now just discard the data the guest sends us while a host port is disconnected or doesn't have a handler registered for consuming data. This commit really doesn't change anything from the current behaviour, just makes the code slightly better by spinning off data handling to ports in another function. Signed-off-by: Amit Shah --- hw/virtio-serial-bus.c | 68 ++++++++++++++++++++++-------------------------- 1 files changed, 31 insertions(+), 37 deletions(-) diff --git a/hw/virtio-serial-bus.c b/hw/virtio-serial-bus.c index 7ac46f5..757de7c 100644 --- a/hw/virtio-serial-bus.c +++ b/hw/virtio-serial-bus.c @@ -111,6 +111,29 @@ static size_t write_to_port(VirtIOSerialPort *port, return offset; } +static void flush_queued_data(VirtIOSerialPort *port, bool discard) +{ + VirtQueue *vq; + VirtQueueElement elem; + + vq = port->ovq; + while (virtqueue_pop(vq, &elem)) { + uint8_t *buf; + size_t ret, buf_size; + + if (!discard) { + buf_size = iov_size(elem.out_sg, elem.out_num); + buf = qemu_malloc(buf_size); + ret = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, buf_size); + + port->info->have_data(port, buf, ret); + qemu_free(buf); + } + virtqueue_push(vq, &elem, 0); + } + virtio_notify(&port->vser->vdev, vq); +} + static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len) { VirtQueueElement elem; @@ -348,47 +371,18 @@ static void control_out(VirtIODevice *vdev, VirtQueue *vq) static void handle_output(VirtIODevice *vdev, VirtQueue *vq) { VirtIOSerial *vser; - VirtQueueElement elem; + VirtIOSerialPort *port; + bool discard; vser = DO_UPCAST(VirtIOSerial, vdev, vdev); + port = find_port_by_vq(vser, vq); - while (virtqueue_pop(vq, &elem)) { - VirtIOSerialPort *port; - uint8_t *buf; - size_t ret, buf_size; - - port = find_port_by_vq(vser, vq); - if (!port) { - ret = 0; - goto next_buf; - } - - if (!port->host_connected) { - ret = 0; - goto next_buf; - } - - /* - * A port may not have any handler registered for consuming the - * data that the guest sends or it may not have a chardev associated - * with it. Just ignore the data in that case. - */ - if (!port->info->have_data) { - ret = 0; - goto next_buf; - } - - buf_size = iov_size(elem.out_sg, elem.out_num); - buf = qemu_malloc(buf_size); - ret = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, buf_size); - - port->info->have_data(port, buf, ret); - qemu_free(buf); - - next_buf: - virtqueue_push(vq, &elem, 0); + discard = false; + if (!port || !port->host_connected || !port->info->have_data) { + discard = true; } - virtio_notify(vdev, vq); + + flush_queued_data(port, discard); } static void handle_input(VirtIODevice *vdev, VirtQueue *vq)