public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 1/3] tcp: Use common helper for rebinding inbound and outbound ports
Date: Wed, 15 Nov 2023 16:25:32 +1100	[thread overview]
Message-ID: <20231115052534.1826261-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20231115052534.1826261-1-david@gibson.dropbear.id.au>

tcp_port_rebind() has two cases with almost but not quite identical code.
Simplify things a bit by factoring this out into a single parameterised
helper, tcp_port_do_rebind().

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 tcp.c | 92 +++++++++++++++++++++++++++++------------------------------
 1 file changed, 45 insertions(+), 47 deletions(-)

diff --git a/tcp.c b/tcp.c
index f51d27a..6bc040c 100644
--- a/tcp.c
+++ b/tcp.c
@@ -3155,6 +3155,49 @@ int tcp_init(struct ctx *c)
 	return 0;
 }
 
+/**
+ * tcp_port_do_rebind() - Rebind ports to match forward maps
+ * @c:		Execution context
+ * @outbound:	True to remap outbound forwards, otherwise inbound
+ *
+ * Must be called in namespace context if @outbound is true.
+ */
+static void tcp_port_do_rebind(struct ctx *c, bool outbound)
+{
+	const uint8_t *fmap = outbound ? c->tcp.fwd_out.map : c->tcp.fwd_in.map;
+	const uint8_t *rmap = outbound ? c->tcp.fwd_in.map : c->tcp.fwd_out.map;
+	int (*socks)[IP_VERSIONS] = outbound ? tcp_sock_ns : tcp_sock_init_ext;
+	unsigned port;
+
+	for (port = 0; port < NUM_PORTS; port++) {
+		if (!bitmap_isset(fmap, port)) {
+			if (socks[port][V4] >= 0) {
+				close(socks[port][V4]);
+				socks[port][V4] = -1;
+			}
+
+			if (socks[port][V6] >= 0) {
+				close(socks[port][V6]);
+				socks[port][V6] = -1;
+			}
+
+			continue;
+		}
+
+		/* Don't loop back our own ports */
+		if (bitmap_isset(rmap, port))
+			continue;
+
+		if ((c->ifi4 && socks[port][V4] == -1) ||
+		    (c->ifi6 && socks[port][V6] == -1)) {
+			if (outbound)
+				tcp_ns_sock_init(c, port);
+			else
+				tcp_sock_init(c, AF_UNSPEC, NULL, NULL, port);
+		}
+	}
+}
+
 /**
  * struct tcp_port_rebind_arg - Arguments for tcp_port_rebind()
  * @c:			Execution context
@@ -3174,58 +3217,13 @@ struct tcp_port_rebind_arg {
 static int tcp_port_rebind(void *arg)
 {
 	struct tcp_port_rebind_arg *a = (struct tcp_port_rebind_arg *)arg;
-	unsigned port;
 
 	if (a->bind_in_ns) {
 		ns_enter(a->c);
 
-		for (port = 0; port < NUM_PORTS; port++) {
-			if (!bitmap_isset(a->c->tcp.fwd_out.map, port)) {
-				if (tcp_sock_ns[port][V4] >= 0) {
-					close(tcp_sock_ns[port][V4]);
-					tcp_sock_ns[port][V4] = -1;
-				}
-
-				if (tcp_sock_ns[port][V6] >= 0) {
-					close(tcp_sock_ns[port][V6]);
-					tcp_sock_ns[port][V6] = -1;
-				}
-
-				continue;
-			}
-
-			/* Don't loop back our own ports */
-			if (bitmap_isset(a->c->tcp.fwd_in.map, port))
-				continue;
-
-			if ((a->c->ifi4 && tcp_sock_ns[port][V4] == -1) ||
-			    (a->c->ifi6 && tcp_sock_ns[port][V6] == -1))
-				tcp_ns_sock_init(a->c, port);
-		}
+		tcp_port_do_rebind(a->c, true);
 	} else {
-		for (port = 0; port < NUM_PORTS; port++) {
-			if (!bitmap_isset(a->c->tcp.fwd_in.map, port)) {
-				if (tcp_sock_init_ext[port][V4] >= 0) {
-					close(tcp_sock_init_ext[port][V4]);
-					tcp_sock_init_ext[port][V4] = -1;
-				}
-
-				if (tcp_sock_init_ext[port][V6] >= 0) {
-					close(tcp_sock_init_ext[port][V6]);
-					tcp_sock_init_ext[port][V6] = -1;
-				}
-				continue;
-			}
-
-			/* Don't loop back our own ports */
-			if (bitmap_isset(a->c->tcp.fwd_out.map, port))
-				continue;
-
-			if ((a->c->ifi4 && tcp_sock_init_ext[port][V4] == -1) ||
-			    (a->c->ifi6 && tcp_sock_init_ext[port][V6] == -1))
-				tcp_sock_init(a->c, AF_UNSPEC, NULL, NULL,
-					      port);
-		}
+		tcp_port_do_rebind(a->c, false);
 	}
 
 	return 0;
-- 
@@ -3155,6 +3155,49 @@ int tcp_init(struct ctx *c)
 	return 0;
 }
 
+/**
+ * tcp_port_do_rebind() - Rebind ports to match forward maps
+ * @c:		Execution context
+ * @outbound:	True to remap outbound forwards, otherwise inbound
+ *
+ * Must be called in namespace context if @outbound is true.
+ */
+static void tcp_port_do_rebind(struct ctx *c, bool outbound)
+{
+	const uint8_t *fmap = outbound ? c->tcp.fwd_out.map : c->tcp.fwd_in.map;
+	const uint8_t *rmap = outbound ? c->tcp.fwd_in.map : c->tcp.fwd_out.map;
+	int (*socks)[IP_VERSIONS] = outbound ? tcp_sock_ns : tcp_sock_init_ext;
+	unsigned port;
+
+	for (port = 0; port < NUM_PORTS; port++) {
+		if (!bitmap_isset(fmap, port)) {
+			if (socks[port][V4] >= 0) {
+				close(socks[port][V4]);
+				socks[port][V4] = -1;
+			}
+
+			if (socks[port][V6] >= 0) {
+				close(socks[port][V6]);
+				socks[port][V6] = -1;
+			}
+
+			continue;
+		}
+
+		/* Don't loop back our own ports */
+		if (bitmap_isset(rmap, port))
+			continue;
+
+		if ((c->ifi4 && socks[port][V4] == -1) ||
+		    (c->ifi6 && socks[port][V6] == -1)) {
+			if (outbound)
+				tcp_ns_sock_init(c, port);
+			else
+				tcp_sock_init(c, AF_UNSPEC, NULL, NULL, port);
+		}
+	}
+}
+
 /**
  * struct tcp_port_rebind_arg - Arguments for tcp_port_rebind()
  * @c:			Execution context
@@ -3174,58 +3217,13 @@ struct tcp_port_rebind_arg {
 static int tcp_port_rebind(void *arg)
 {
 	struct tcp_port_rebind_arg *a = (struct tcp_port_rebind_arg *)arg;
-	unsigned port;
 
 	if (a->bind_in_ns) {
 		ns_enter(a->c);
 
-		for (port = 0; port < NUM_PORTS; port++) {
-			if (!bitmap_isset(a->c->tcp.fwd_out.map, port)) {
-				if (tcp_sock_ns[port][V4] >= 0) {
-					close(tcp_sock_ns[port][V4]);
-					tcp_sock_ns[port][V4] = -1;
-				}
-
-				if (tcp_sock_ns[port][V6] >= 0) {
-					close(tcp_sock_ns[port][V6]);
-					tcp_sock_ns[port][V6] = -1;
-				}
-
-				continue;
-			}
-
-			/* Don't loop back our own ports */
-			if (bitmap_isset(a->c->tcp.fwd_in.map, port))
-				continue;
-
-			if ((a->c->ifi4 && tcp_sock_ns[port][V4] == -1) ||
-			    (a->c->ifi6 && tcp_sock_ns[port][V6] == -1))
-				tcp_ns_sock_init(a->c, port);
-		}
+		tcp_port_do_rebind(a->c, true);
 	} else {
-		for (port = 0; port < NUM_PORTS; port++) {
-			if (!bitmap_isset(a->c->tcp.fwd_in.map, port)) {
-				if (tcp_sock_init_ext[port][V4] >= 0) {
-					close(tcp_sock_init_ext[port][V4]);
-					tcp_sock_init_ext[port][V4] = -1;
-				}
-
-				if (tcp_sock_init_ext[port][V6] >= 0) {
-					close(tcp_sock_init_ext[port][V6]);
-					tcp_sock_init_ext[port][V6] = -1;
-				}
-				continue;
-			}
-
-			/* Don't loop back our own ports */
-			if (bitmap_isset(a->c->tcp.fwd_out.map, port))
-				continue;
-
-			if ((a->c->ifi4 && tcp_sock_init_ext[port][V4] == -1) ||
-			    (a->c->ifi6 && tcp_sock_init_ext[port][V6] == -1))
-				tcp_sock_init(a->c, AF_UNSPEC, NULL, NULL,
-					      port);
-		}
+		tcp_port_do_rebind(a->c, false);
 	}
 
 	return 0;
-- 
2.41.0


  reply	other threads:[~2023-11-15  5:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-15  5:25 [PATCH 0/3] pasta: Implement runtime auto-forwarding for UDP David Gibson
2023-11-15  5:25 ` David Gibson [this message]
2023-11-15  5:25 ` [PATCH 2/3] tcp: Simplify away tcp_port_rebind() David Gibson
2023-11-15  5:25 ` [PATCH 3/3] udp,pasta: Periodically scan for ports to automatically forward David Gibson
2023-11-19 20:10 ` [PATCH 0/3] pasta: Implement runtime auto-forwarding for UDP Stefano Brivio

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=20231115052534.1826261-2-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

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 IMAP folder(s).