From patchwork Fri Sep 14 08:46:56 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 183869 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 564692C0085 for ; Fri, 14 Sep 2012 20:18:01 +1000 (EST) Received: from localhost ([::1]:42907 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TCRZY-0006Eu-Gc for incoming@patchwork.ozlabs.org; Fri, 14 Sep 2012 04:48:24 -0400 Received: from eggs.gnu.org ([208.118.235.92]:50271) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TCRYn-0004oU-Dk for qemu-devel@nongnu.org; Fri, 14 Sep 2012 04:47:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TCRYj-0000vU-Ac for qemu-devel@nongnu.org; Fri, 14 Sep 2012 04:47:37 -0400 Received: from mail-we0-f173.google.com ([74.125.82.173]:47609) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TCRYj-0000sO-5C for qemu-devel@nongnu.org; Fri, 14 Sep 2012 04:47:33 -0400 Received: by mail-we0-f173.google.com with SMTP id z53so2182085wey.4 for ; Fri, 14 Sep 2012 01:47:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=dk9HKs+0L5gdTjXkrkPAtixeMQqzIIGWsqXMRw7IAFQ=; b=KT7sbelIkXHQzWYudbhhWQQD8kZkMdomHa9wdvFFYASpPnF4ydYmlhJL+eM/AL9on3 8k2S/entuEc/tGLEuvmVYTwm3LIvV4EjNtzz49RLH7B84ljEpGnMw3giqy/N+3hL1ogG inOaHVQ6vDGsi6Zn9GuMYFeasMuI4RY8NAPppZcppD96zmMEePtTxWk0qaNn5RfYksZ2 DgkMYcwzI7Mbuvk0ZTThIrAfQAAgbiHQ4UidHtsMlaCd/XVH7oyVSmJaq3gpQFEHOZrp 7T/UUT5LVWbEGJuC0Dzg83llY1pJeffHWBYikFBeLrzA2sikmjz1vunvZAh8ijyTDW+v LclQ== Received: by 10.180.74.133 with SMTP id t5mr46565357wiv.2.1347612452775; Fri, 14 Sep 2012 01:47:32 -0700 (PDT) Received: from localhost ([109.224.133.37]) by mx.google.com with ESMTPS id l6sm17863260wiz.4.2012.09.14.01.47.31 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 14 Sep 2012 01:47:32 -0700 (PDT) From: Stefan Hajnoczi To: Anthony Liguori Date: Fri, 14 Sep 2012 09:46:56 +0100 Message-Id: <1347612420-5704-10-git-send-email-stefanha@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1347612420-5704-1-git-send-email-stefanha@gmail.com> References: <1347612420-5704-1-git-send-email-stefanha@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 74.125.82.173 Cc: qemu-devel@nongnu.org, Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 09/13] net: fix usbnet_receive() packet drops 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 From: Stefan Hajnoczi The USB network interface has a single buffer which the guest reads from. This patch prevents multiple calls to usbnet_receive() from clobbering the input buffer. Instead we queue packets until buffer space becomes available again. This is inspired by virtio-net and e1000 rxbuf handling. Signed-off-by: Stefan Hajnoczi --- hw/usb/dev-network.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/hw/usb/dev-network.c b/hw/usb/dev-network.c index 0b5cb71..e4a4359 100644 --- a/hw/usb/dev-network.c +++ b/hw/usb/dev-network.c @@ -1001,6 +1001,13 @@ static int rndis_keepalive_response(USBNetState *s, return 0; } +/* Prepare to receive the next packet */ +static void usb_net_reset_in_buf(USBNetState *s) +{ + s->in_ptr = s->in_len = 0; + qemu_flush_queued_packets(&s->nic->nc); +} + static int rndis_parse(USBNetState *s, uint8_t *data, int length) { uint32_t msg_type; @@ -1025,7 +1032,8 @@ static int rndis_parse(USBNetState *s, uint8_t *data, int length) case RNDIS_RESET_MSG: rndis_clear_responsequeue(s); - s->out_ptr = s->in_ptr = s->in_len = 0; + s->out_ptr = 0; + usb_net_reset_in_buf(s); return rndis_reset_response(s, (rndis_reset_msg_type *) data); case RNDIS_KEEPALIVE_MSG: @@ -1135,7 +1143,7 @@ static int usb_net_handle_datain(USBNetState *s, USBPacket *p) int ret = USB_RET_NAK; if (s->in_ptr > s->in_len) { - s->in_ptr = s->in_len = 0; + usb_net_reset_in_buf(s); ret = USB_RET_NAK; return ret; } @@ -1152,7 +1160,7 @@ static int usb_net_handle_datain(USBNetState *s, USBPacket *p) if (s->in_ptr >= s->in_len && (is_rndis(s) || (s->in_len & (64 - 1)) || !ret)) { /* no short packet necessary */ - s->in_ptr = s->in_len = 0; + usb_net_reset_in_buf(s); } #ifdef TRAFFIC_DEBUG @@ -1263,6 +1271,11 @@ static ssize_t usbnet_receive(NetClientState *nc, const uint8_t *buf, size_t siz return -1; } + /* Only accept packet if input buffer is empty */ + if (s->in_len > 0) { + return 0; + } + if (is_rndis(s)) { struct rndis_packet_msg_type *msg;