From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MHLJ2-0006WR-L2 for qemu-devel@nongnu.org; Thu, 18 Jun 2009 13:21:44 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MHLIx-0006OP-7O for qemu-devel@nongnu.org; Thu, 18 Jun 2009 13:21:43 -0400 Received: from [199.232.76.173] (port=43806 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MHLIw-0006Ny-HE for qemu-devel@nongnu.org; Thu, 18 Jun 2009 13:21:38 -0400 Received: from mail11.svc.cra.dublin.eircom.net ([159.134.118.27]:35093) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1MHLIw-0008SW-1P for qemu-devel@nongnu.org; Thu, 18 Jun 2009 13:21:38 -0400 From: Mark McLoughlin Date: Thu, 18 Jun 2009 18:21:32 +0100 Message-Id: <1245345696-20915-5-git-send-email-markmc@redhat.com> In-Reply-To: <1245345696-20915-4-git-send-email-markmc@redhat.com> References: <1245345696-20915-1-git-send-email-markmc@redhat.com> <1245345696-20915-2-git-send-email-markmc@redhat.com> <1245345696-20915-3-git-send-email-markmc@redhat.com> <1245345696-20915-4-git-send-email-markmc@redhat.com> Subject: [Qemu-devel] [PATCH 4/8] net: handle EAGAIN from tapfd write() List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Mark McLoughlin If a write() on tapfd returns EAGAIN, return zero so that the packet gets queued (in the case of async send) and enable polling tapfd for writing. When tapfd becomes writable, disable write polling and flush any queued packets. Signed-off-by: Mark McLoughlin --- net.c | 27 +++++++++++++++++++++++++-- 1 files changed, 25 insertions(+), 2 deletions(-) diff --git a/net.c b/net.c index 9005822..e2e73ef 100644 --- a/net.c +++ b/net.c @@ -1044,19 +1044,21 @@ typedef struct TAPState { char down_script_arg[128]; uint8_t buf[4096]; unsigned int read_poll : 1; + unsigned int write_poll : 1; } TAPState; static int launch_script(const char *setup_script, const char *ifname, int fd); static int tap_can_send(void *opaque); static void tap_send(void *opaque); +static void tap_writable(void *opaque); static void tap_update_fd_handler(TAPState *s) { qemu_set_fd_handler2(s->fd, s->read_poll ? tap_can_send : NULL, s->read_poll ? tap_send : NULL, - NULL, + s->write_poll ? tap_writable : NULL, s); } @@ -1066,6 +1068,21 @@ static void tap_read_poll(TAPState *s, int enable) tap_update_fd_handler(s); } +static void tap_write_poll(TAPState *s, int enable) +{ + s->write_poll = !!enable; + tap_update_fd_handler(s); +} + +static void tap_writable(void *opaque) +{ + TAPState *s = opaque; + + tap_write_poll(s, 0); + + qemu_flush_queued_packets(s->vc); +} + static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov, int iovcnt) { @@ -1074,7 +1091,12 @@ static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov, do { len = writev(s->fd, iov, iovcnt); - } while (len == -1 && (errno == EINTR || errno == EAGAIN)); + } while (len == -1 && errno == EINTR); + + if (len == -1 && errno == EAGAIN) { + tap_write_poll(s, 1); + return 0; + } return len; } @@ -1150,6 +1172,7 @@ static void tap_cleanup(VLANClientState *vc) launch_script(s->down_script, s->down_script_arg, s->fd); tap_read_poll(s, 0); + tap_write_poll(s, 0); close(s->fd); qemu_free(s); } -- 1.6.0.6