From patchwork Mon Sep 16 08:25:03 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ottlik X-Patchwork-Id: 275148 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 36D8D2C00F6 for ; Mon, 16 Sep 2013 18:28:10 +1000 (EST) Received: from localhost ([::1]:60570 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VLUAC-0006Os-2z for incoming@patchwork.ozlabs.org; Mon, 16 Sep 2013 04:28:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49617) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VLU7c-0003C0-F9 for qemu-devel@nongnu.org; Mon, 16 Sep 2013 04:25:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VLU7U-0004xY-Ni for qemu-devel@nongnu.org; Mon, 16 Sep 2013 04:25:28 -0400 Received: from ex-e-2.perimeter.fzi.de ([141.21.8.251]:53557) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VLU7U-0004w8-HG for qemu-devel@nongnu.org; Mon, 16 Sep 2013 04:25:20 -0400 Received: from ex-ca-ht-1.fzi.de (141.21.32.98) by ex-e-2.perimeter.fzi.de (141.21.8.251) with Microsoft SMTP Server (TLS) id 14.3.158.1; Mon, 16 Sep 2013 10:25:09 +0200 Received: from orcrist.fzi.de (141.21.45.20) by ex-ca-ht-1.fzi.de (141.21.32.98) with Microsoft SMTP Server id 14.3.158.1; Mon, 16 Sep 2013 10:25:12 +0200 Received: by orcrist.fzi.de (Postfix, from userid 330838105) id 33BF01C05E2; Mon, 16 Sep 2013 10:25:12 +0200 (CEST) From: Sebastian Ottlik To: Date: Mon, 16 Sep 2013 10:25:03 +0200 Message-ID: <1379319907-14950-2-git-send-email-ottlik@fzi.de> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1379319907-14950-1-git-send-email-ottlik@fzi.de> References: <1379319907-14950-1-git-send-email-ottlik@fzi.de> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 141.21.8.251 Cc: Jan Kiszka , Anthony Liguori , Sebastian Ottlik , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v4 1/5] util: add socket_set_fast_reuse function which will replace setting SO_REUSEADDR 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 If a socket is closed it remains in TIME_WAIT state for some time. On operating systems using BSD sockets the endpoint of the socket may not be reused while in this state unless SO_REUSEADDR was set on the socket. On windows on the other hand the default behaviour is to allow reuse (i.e. identical to SO_REUSEADDR on other operating systems) and setting SO_REUSEADDR on a socket allows it to be bound to a endpoint even if the endpoint is already used by another socket independently of the other sockets state. This can even result in undefined behaviour. Many sockets used by QEMU should not block the use of their endpoint after being closed while they are still in TIME_WAIT state. Currently QEMU sets SO_REUSEADDR for such sockets, which can lead to problems on Windows. This patch introduces the function socket_set_fast_reuse that should be used instead of setting SO_REUSEADDR when fast socket reuse is desired and behaves correctly on all operating systems. Leaving the existing error reporting to the call site of socket_set_fast_reuse would result in bad code or a need to change the way errors are reported, so error reporting has been moved to socket_set_fast_reuse. As some places setting SO_REUSEADDR do not report errors, this behaviour can be controlled with the silent flag. Signed-off-by: Sebastian Ottlik --- include/qemu/sockets.h | 1 + util/oslib-posix.c | 14 ++++++++++++++ util/oslib-win32.c | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index c5174d7..fcbdb2d 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -39,6 +39,7 @@ int socket_set_cork(int fd, int v); int socket_set_nodelay(int fd); void qemu_set_block(int fd); void qemu_set_nonblock(int fd); +int socket_set_fast_reuse(int fd, bool silent); int send_all(int fd, const void *buf, int len1); int recv_all(int fd, void *buf, int len1, bool single_read); diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 3dc8b1b..3beb341 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -159,6 +159,20 @@ void qemu_set_nonblock(int fd) fcntl(fd, F_SETFL, f | O_NONBLOCK); } +int socket_set_fast_reuse(int fd, bool silent) +{ + int val = 1, ret; + + ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, + (const char *)&val, sizeof(val)); + + if (ret < 0 && !silent) { + perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); + } + + return ret; +} + void qemu_set_cloexec(int fd) { int f; diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 961fbf5..4e13aba 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -127,6 +127,16 @@ void qemu_set_nonblock(int fd) qemu_fd_register(fd); } +int socket_set_fast_reuse(int fd, bool silent) +{ + /* Enabling the reuse of an endpoint that was used by a socket still in + * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows + * fast reuse is the default and SO_REUSEADDR does strange things. So we + * don't have to do anything here. More info can be found at: + * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */ + return 0; +} + int inet_aton(const char *cp, struct in_addr *ia) { uint32_t addr = inet_addr(cp);