From mboxrd@z Thu Jan 1 00:00:00 1970 From: Lepton Wu Subject: [PATCH] VSOCK: bind to random port for VMADDR_PORT_ANY Date: Mon, 10 Dec 2018 23:02:35 -0800 Message-ID: <20181211070235.35094-1-ytht.net@gmail.com> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Cc: stefanha@redhat.com, Lepton Wu To: netdev@vger.kernel.org Return-path: Received: from mail-pl1-f193.google.com ([209.85.214.193]:33453 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725923AbeLKHCm (ORCPT ); Tue, 11 Dec 2018 02:02:42 -0500 Received: by mail-pl1-f193.google.com with SMTP id z23so6505155plo.0 for ; Mon, 10 Dec 2018 23:02:41 -0800 (PST) Sender: netdev-owner@vger.kernel.org List-ID: The old code always starts from fixed port for VMADDR_PORT_ANY. Sometimes when VMM crashed, there is still orphaned vsock which is waiting for close timer, then it could cause connection time out for new started VM if they are trying to connect to same port with same guest cid since the new packets could hit that orphaned vsock. We could also fix this by doing more in vhost_vsock_reset_orphans, but any way, it should be better to start from a random local port instead of a fixed one. Signed-off-by: Lepton Wu --- net/vmw_vsock/af_vsock.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index ab27a2872935..73817e846a1f 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -107,6 +107,7 @@ #include #include #include +#include #include #include #include @@ -504,9 +505,12 @@ static void vsock_pending_work(struct work_struct *work) static int __vsock_bind_stream(struct vsock_sock *vsk, struct sockaddr_vm *addr) { - static u32 port = LAST_RESERVED_PORT + 1; + static u32 port = 0; struct sockaddr_vm new_addr; + if (!port) + port = prandom_u32(); + vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port); if (addr->svm_port == VMADDR_PORT_ANY) { -- 2.20.0.rc2.403.gdbc3b29805-goog