Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: kostja@tarantool.org
Subject: [tarantool-patches] [PATCH 3/7] swim: do not rebind when new 'port' is 0
Date: Wed, 15 May 2019 02:06:21 +0300	[thread overview]
Message-ID: <22d5641734c11687a5adf489d7e49dc1de544239.1557875116.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1557875116.git.v.shpilevoy@tarantool.org>

SWIM internally tries to avoid unnecessary close+socket+bind
calls on reconfiguration if a new URI is the same as an old one.
SWIM transport compares <IP, port> couples and if they are
equal, does nothing.

But if a port is 0, it is not a real port, but a sign to the
kernel to find any free port on the IP address. In such a case
SWIM transport after bind() retrieves and saves a real port.

When the same URI is specified again, the transport compares two
addresses: old <IP, auto found port>, new <IP, 0>, sees they are
'different', and rebinds. It is not necessary, obviously, because
the new URI covers the old one.

This commit avoids rebind, when new IP == old IP, and new port is
0.

Part of #3234
---
 src/lib/swim/swim_transport_udp.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/lib/swim/swim_transport_udp.c b/src/lib/swim/swim_transport_udp.c
index f017eeaf0..c0317a20b 100644
--- a/src/lib/swim/swim_transport_udp.c
+++ b/src/lib/swim/swim_transport_udp.c
@@ -64,11 +64,21 @@ swim_transport_bind(struct swim_transport *transport,
 	const struct sockaddr_in *new_addr = (const struct sockaddr_in *) addr;
 	const struct sockaddr_in *old_addr = &transport->addr;
 	assert(addr_len == sizeof(*new_addr));
+	bool is_new_port_any = new_addr->sin_port == 0;
 
 	if (transport->fd != -1 &&
 	    new_addr->sin_addr.s_addr == old_addr->sin_addr.s_addr &&
-	    new_addr->sin_port == old_addr->sin_port)
+	    (new_addr->sin_port == old_addr->sin_port || is_new_port_any)) {
+		/*
+		 * Note, that new port == 0 means that any port is
+		 * ok. If at the same time old and new IP
+		 * addresses are the same and the socket is
+		 * already bound (fd != -1), then the existing
+		 * socket 'matches' the new URI and rebind is not
+		 * needed.
+		 */
 		return 0;
+	}
 
 	int fd = sio_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 	if (fd < 0)
@@ -81,7 +91,7 @@ swim_transport_bind(struct swim_transport *transport,
 		return -1;
 	}
 	int real_port = new_addr->sin_port;
-	if (new_addr->sin_port == 0) {
+	if (is_new_port_any) {
 		struct sockaddr_in real_addr;
 		addr_len = sizeof(real_addr);
 		if (sio_getsockname(fd, (struct sockaddr *) &real_addr,
-- 
2.20.1 (Apple Git-117)

  parent reply	other threads:[~2019-05-14 23:06 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-14 23:06 [tarantool-patches] [PATCH 0/7] swim lua preparation, again Vladislav Shpilevoy
2019-05-14 23:06 ` [tarantool-patches] [PATCH 1/7] swim: drop swim_info() function Vladislav Shpilevoy
2019-05-15  2:00   ` [tarantool-patches] " Konstantin Osipov
2019-05-14 23:06 ` [tarantool-patches] [PATCH 2/7] swim: encapsulate 'uint16' payload size inside swim.c Vladislav Shpilevoy
2019-05-15  2:02   ` [tarantool-patches] " Konstantin Osipov
2019-05-14 23:06 ` Vladislav Shpilevoy [this message]
2019-05-15  2:02   ` [tarantool-patches] Re: [PATCH 3/7] swim: do not rebind when new 'port' is 0 Konstantin Osipov
2019-05-14 23:06 ` [tarantool-patches] [PATCH 4/7] swim: set 'left' status in self on swim_quit() Vladislav Shpilevoy
2019-05-15  2:03   ` [tarantool-patches] " Konstantin Osipov
2019-05-14 23:06 ` [tarantool-patches] [PATCH 5/7] msgpack: allow to pass 'struct ibuf *' into encode() Vladislav Shpilevoy
2019-05-15  2:05   ` [tarantool-patches] " Konstantin Osipov
2019-05-14 23:06 ` [tarantool-patches] [PATCH 6/7] msgpack: allow to pass 'const char *' into decode() Vladislav Shpilevoy
2019-05-15  2:05   ` [tarantool-patches] " Konstantin Osipov
2019-05-14 23:06 ` [tarantool-patches] [PATCH 7/7] Drop an unused function and class Vladislav Shpilevoy
2019-05-15  2:06   ` [tarantool-patches] " Konstantin Osipov
2019-05-15 10:02 ` [tarantool-patches] Re: [PATCH 0/7] swim lua preparation, again Vladislav Shpilevoy

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=22d5641734c11687a5adf489d7e49dc1de544239.1557875116.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH 3/7] swim: do not rebind when new '\''port'\'' is 0' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox