From: Lepton Wu <ytht.net@gmail.com>
To: netdev@vger.kernel.org
Cc: stefanha@redhat.com, decui@microsoft.com, jhansen@vmware.com,
Lepton Wu <ytht.net@gmail.com>
Subject: [PATCH v2] VSOCK: bind to random port for VMADDR_PORT_ANY
Date: Tue, 11 Dec 2018 11:12:55 -0800 [thread overview]
Message-ID: <20181211191255.80025-1-ytht.net@gmail.com> (raw)
In-Reply-To: <MWHPR05MB3376CF814038C99584DA78FFDAA60@MWHPR05MB3376.namprd05.prod.outlook.com>
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 <ytht.net@gmail.com>
---
net/vmw_vsock/af_vsock.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index ab27a2872935..43a1dec08825 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -107,6 +107,7 @@
#include <linux/mutex.h>
#include <linux/net.h>
#include <linux/poll.h>
+#include <linux/random.h>
#include <linux/skbuff.h>
#include <linux/smp.h>
#include <linux/socket.h>
@@ -504,9 +505,13 @@ 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 = LAST_RESERVED_PORT + 1 +
+ prandom_u32_max(U32_MAX - LAST_RESERVED_PORT);
+
vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port);
if (addr->svm_port == VMADDR_PORT_ANY) {
--
2.20.0.405.gbc1bbc6f85-goog
next prev parent reply other threads:[~2018-12-11 19:13 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-11 7:02 [PATCH] VSOCK: bind to random port for VMADDR_PORT_ANY Lepton Wu
2018-12-11 16:26 ` Stefan Hajnoczi
2018-12-11 18:53 ` Jorgen S. Hansen
2018-12-11 19:12 ` Lepton Wu [this message]
2018-12-12 13:22 ` [PATCH v2] " Jorgen S. Hansen
2018-12-14 22:40 ` David Miller
2018-12-12 8:23 ` [PATCH] " Dexuan Cui
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181211191255.80025-1-ytht.net@gmail.com \
--to=ytht.net@gmail.com \
--cc=decui@microsoft.com \
--cc=jhansen@vmware.com \
--cc=netdev@vger.kernel.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).