From patchwork Mon Sep 10 09:55:36 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 183825 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 550142C0084 for ; Fri, 14 Sep 2012 18:01:58 +1000 (EST) Received: from localhost ([::1]:50492 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TCQPn-0006hu-Sz for incoming@patchwork.ozlabs.org; Fri, 14 Sep 2012 03:34:15 -0400 Received: from eggs.gnu.org ([208.118.235.92]:57990) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TCQPO-000632-Fj for qemu-devel@nongnu.org; Fri, 14 Sep 2012 03:33:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TCQPJ-0000uM-Dd for qemu-devel@nongnu.org; Fri, 14 Sep 2012 03:33:50 -0400 Received: from mail-we0-f173.google.com ([74.125.82.173]:43417) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TCQPJ-0000tb-7Y for qemu-devel@nongnu.org; Fri, 14 Sep 2012 03:33:45 -0400 Received: by mail-we0-f173.google.com with SMTP id z53so2146710wey.4 for ; Fri, 14 Sep 2012 00:33:44 -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=mTOAo5hZjFEqc0N2Iv8P/NHUPwnl2jVZREek6hn1Mwc=; b=LF7hpeIVOpGKocR2kZYrKldtVTJnDznaWuvEqCZivRJ6gQtiIw/XKWqbXQN2ZIc5Gj MXHg+7+gzKAI7L35JMxJmBDO/zS8YL4N+kTXypGTwCgX10+Stab1P/3qBJ26e+yNDSzk ZdsA/VyubKzkT/mbPhKSKL1Ak3CnjuAdFWMoSSBf+q3OdgKU8nucP4zQunw84QcmM+UJ il0VU2stp1wZGaFHGI+1EnkS0JXT/QUoBSpgPcxyig28wXdeGIFDccc6YbCyuS+GVUUI 6ocWv3B/o0xRtf4p1reV9X0fD4T9jbybdNLhumJhoYqgOvW3Tpabd5GfmfDPd4a3N7NK mBqQ== Received: by 10.216.139.17 with SMTP id b17mr1105469wej.8.1347608024833; Fri, 14 Sep 2012 00:33:44 -0700 (PDT) Received: from localhost ([109.224.133.37]) by mx.google.com with ESMTPS id eu4sm17606258wib.2.2012.09.14.00.33.43 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 14 Sep 2012 00:33:44 -0700 (PDT) From: Stefan Hajnoczi To: Anthony Liguori Date: Mon, 10 Sep 2012 10:55:36 +0100 Message-Id: <1347270936-30610-14-git-send-email-stefanha@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1347270936-30610-1-git-send-email-stefanha@gmail.com> References: <1347270936-30610-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 13/13] net: EAGAIN handling for net/socket.c TCP 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 Replace spinning send_all() with a proper non-blocking send. When the socket write buffer limit is reached, we should stop trying to send and wait for the socket to become writable again. Non-blocking TCP sockets can return in two different ways when the write buffer limit is reached: 1. ret = -1 and errno = EAGAIN/EWOULDBLOCK. No data has been written. 2. ret < total_size. Short write, only part of the message was transmitted. Handle both cases and keep track of how many bytes have been written in s->send_index. (This includes the 'length' header before the actual payload buffer.) Signed-off-by: Stefan Hajnoczi --- net/socket.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/net/socket.c b/net/socket.c index e5e4e8d..c3e55b8 100644 --- a/net/socket.c +++ b/net/socket.c @@ -32,6 +32,7 @@ #include "qemu-error.h" #include "qemu-option.h" #include "qemu_socket.h" +#include "iov.h" typedef struct NetSocketState { NetClientState nc; @@ -40,6 +41,7 @@ typedef struct NetSocketState { int state; /* 0 = getting length, 1 = getting data */ unsigned int index; unsigned int packet_len; + unsigned int send_index; /* number of bytes sent (only SOCK_STREAM) */ uint8_t buf[4096]; struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ IOHandler *send_fn; /* differs between SOCK_STREAM/SOCK_DGRAM */ @@ -88,15 +90,39 @@ static void net_socket_writable(void *opaque) qemu_flush_queued_packets(&s->nc); } -/* XXX: we consider we can send the whole packet without blocking */ static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size) { NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); - uint32_t len; - len = htonl(size); + uint32_t len = htonl(size); + struct iovec iov[] = { + { + .iov_base = &len, + .iov_len = sizeof(len), + }, { + .iov_base = (void *)buf, + .iov_len = size, + }, + }; + size_t remaining; + ssize_t ret; + + remaining = iov_size(iov, 2) - s->send_index; + ret = iov_send(s->fd, iov, 2, s->send_index, remaining); - send_all(s->fd, (const uint8_t *)&len, sizeof(len)); - return send_all(s->fd, buf, size); + if (ret == -1 && errno == EAGAIN) { + ret = 0; /* handled further down */ + } + if (ret == -1) { + s->send_index = 0; + return -errno; + } + if (ret < (ssize_t)remaining) { + s->send_index += ret; + net_socket_write_poll(s, true); + return 0; + } + s->send_index = 0; + return size; } static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)