public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 3/8] fwd: Consolidate scans (not rebinds) in fwd.c
Date: Sat, 11 Oct 2025 15:48:22 +1100	[thread overview]
Message-ID: <20251011044827.862757-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20251011044827.862757-1-david@gibson.dropbear.id.au>

fwd_scan_ports_timer(), via the things it calls, goes through all the auto
forwarding cases (tcp, udp, inbound, outbound) and for each one first scans
for listening ports, then rebinds - that is, closes or opens our own
listening ports to match.

Rearrange to do all the scans first, then all the rebinds after.  This lets
us consolidate all the scans into fwd.c, and will enable further cleanups.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 fwd.c | 27 +++++++++++++++++++++------
 fwd.h |  4 ----
 tcp.c | 12 ++++--------
 tcp.h |  2 +-
 udp.c | 14 ++++----------
 udp.h |  2 +-
 6 files changed, 31 insertions(+), 30 deletions(-)

diff --git a/fwd.c b/fwd.c
index bd89a94a..19309f14 100644
--- a/fwd.c
+++ b/fwd.c
@@ -153,7 +153,8 @@ static void procfs_scan_listen(int fd, unsigned int lstate,
  * @fwd:	Forwarding information to update
  * @rev:	Forwarding information for the reverse direction
  */
-void fwd_scan_ports_tcp(struct fwd_ports *fwd, const struct fwd_ports *rev)
+static void fwd_scan_ports_tcp(struct fwd_ports *fwd,
+			       const struct fwd_ports *rev)
 {
 	memset(fwd->map, 0, PORT_BITMAP_SIZE);
 	procfs_scan_listen(fwd->scan4, TCP_LISTEN, fwd->map, rev->map);
@@ -167,9 +168,10 @@ void fwd_scan_ports_tcp(struct fwd_ports *fwd, const struct fwd_ports *rev)
  * @tcp_fwd:	Corresponding TCP forwarding information
  * @tcp_rev:	TCP forwarding information for the reverse direction
  */
-void fwd_scan_ports_udp(struct fwd_ports *fwd, const struct fwd_ports *rev,
-			const struct fwd_ports *tcp_fwd,
-			const struct fwd_ports *tcp_rev)
+static void fwd_scan_ports_udp(struct fwd_ports *fwd,
+			       const struct fwd_ports *rev,
+			       const struct fwd_ports *tcp_fwd,
+			       const struct fwd_ports *tcp_rev)
 {
 	uint8_t exclude[PORT_BITMAP_SIZE];
 
@@ -248,10 +250,23 @@ void fwd_scan_ports_timer(struct ctx *c, const struct timespec *now)
 
 	scan_ports_run = *now;
 
+	if (c->tcp.fwd_out.mode == FWD_AUTO)
+		fwd_scan_ports_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in);
+	if (c->tcp.fwd_in.mode == FWD_AUTO)
+		fwd_scan_ports_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out);
+	if (c->udp.fwd_out.mode == FWD_AUTO) {
+		fwd_scan_ports_udp(&c->udp.fwd_out, &c->udp.fwd_in,
+				   &c->tcp.fwd_out, &c->tcp.fwd_in);
+	}
+	if (c->udp.fwd_in.mode == FWD_AUTO) {
+		fwd_scan_ports_udp(&c->udp.fwd_in, &c->udp.fwd_out,
+				   &c->tcp.fwd_in, &c->tcp.fwd_out);
+	}
+
 	if (!c->no_tcp)
-		tcp_scan_ports(c);
+		tcp_port_rebind_all(c);
 	if (!c->no_udp)
-		udp_scan_ports(c);
+		udp_port_rebind_all(c);
 }
 
 /**
diff --git a/fwd.h b/fwd.h
index 385e5bd8..8fe4119a 100644
--- a/fwd.h
+++ b/fwd.h
@@ -44,10 +44,6 @@ struct fwd_ports {
 
 #define FWD_PORT_SCAN_INTERVAL		1000	/* ms */
 
-void fwd_scan_ports_tcp(struct fwd_ports *fwd, const struct fwd_ports *rev);
-void fwd_scan_ports_udp(struct fwd_ports *fwd, const struct fwd_ports *rev,
-			const struct fwd_ports *tcp_fwd,
-			const struct fwd_ports *tcp_rev);
 void fwd_scan_ports_init(struct ctx *c);
 void fwd_scan_ports_timer(struct ctx *c, const struct timespec *now);
 
diff --git a/tcp.c b/tcp.c
index dbbb00c4..a330538d 100644
--- a/tcp.c
+++ b/tcp.c
@@ -2869,22 +2869,18 @@ static int tcp_port_rebind_outbound(void *arg)
 }
 
 /**
- * tcp_scan_ports() - Update forwarding maps based on scan of listening ports
+ * tcp_port_rebind_all() - Rebind ports to match forward maps (in host & ns)
  * @c:		Execution context
  */
-void tcp_scan_ports(struct ctx *c)
+void tcp_port_rebind_all(struct ctx *c)
 {
 	ASSERT(c->mode == MODE_PASTA && !c->no_tcp);
 
-	if (c->tcp.fwd_out.mode == FWD_AUTO) {
-		fwd_scan_ports_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in);
+	if (c->tcp.fwd_out.mode == FWD_AUTO)
 		NS_CALL(tcp_port_rebind_outbound, c);
-	}
 
-	if (c->tcp.fwd_in.mode == FWD_AUTO) {
-		fwd_scan_ports_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out);
+	if (c->tcp.fwd_in.mode == FWD_AUTO)
 		tcp_port_rebind(c, false);
-	}
 }
 
 /**
diff --git a/tcp.h b/tcp.h
index 43ab0655..69b6c5dc 100644
--- a/tcp.h
+++ b/tcp.h
@@ -21,7 +21,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
 int tcp_sock_init(const struct ctx *c, const union inany_addr *addr,
 		  const char *ifname, in_port_t port);
 int tcp_init(struct ctx *c);
-void tcp_scan_ports(struct ctx *c);
+void tcp_port_rebind_all(struct ctx *c);
 void tcp_timer(const struct ctx *c, const struct timespec *now);
 void tcp_defer_handler(struct ctx *c);
 
diff --git a/udp.c b/udp.c
index d15f03dc..e27b2219 100644
--- a/udp.c
+++ b/udp.c
@@ -1239,24 +1239,18 @@ static int udp_port_rebind_outbound(void *arg)
 }
 
 /**
- * udp_scan_ports() - Update forwarding maps based on scan of listening ports
+ * udp_port_rebind_all() - Rebind ports to match forward maps (in host & ns)
  * @c:		Execution context
  */
-void udp_scan_ports(struct ctx *c)
+void udp_port_rebind_all(struct ctx *c)
 {
 	ASSERT(c->mode == MODE_PASTA && !c->no_udp);
 
-	if (c->udp.fwd_out.mode == FWD_AUTO) {
-		fwd_scan_ports_udp(&c->udp.fwd_out, &c->udp.fwd_in,
-				   &c->tcp.fwd_out, &c->tcp.fwd_in);
+	if (c->udp.fwd_out.mode == FWD_AUTO)
 		NS_CALL(udp_port_rebind_outbound, c);
-	}
 
-	if (c->udp.fwd_in.mode == FWD_AUTO) {
-		fwd_scan_ports_udp(&c->udp.fwd_in, &c->udp.fwd_out,
-				   &c->tcp.fwd_in, &c->tcp.fwd_out);
+	if (c->udp.fwd_in.mode == FWD_AUTO)
 		udp_port_rebind(c, false);
-	}
 }
 
 /**
diff --git a/udp.h b/udp.h
index a6de1f1c..a2bf2720 100644
--- a/udp.h
+++ b/udp.h
@@ -18,7 +18,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 int udp_sock_init(const struct ctx *c, int ns, const union inany_addr *addr,
 		  const char *ifname, in_port_t port);
 int udp_init(struct ctx *c);
-void udp_scan_ports(struct ctx *c);
+void udp_port_rebind_all(struct ctx *c);
 void udp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s);
 
 /**
-- 
2.51.0


  parent reply	other threads:[~2025-10-11  4:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-11  4:48 [PATCH 0/8] RFC: Cleanups to auto port scanning David Gibson
2025-10-11  4:48 ` [PATCH 1/8] icmp: Remove vestiges of ICMP timer David Gibson
2025-10-11  4:48 ` [PATCH 2/8] tcp, udp, fwd: Run all port scanning from a single timer David Gibson
2025-10-11  4:48 ` David Gibson [this message]
2025-10-11  4:48 ` [PATCH 4/8] fwd: Move port exclusion handling from procfs_scan_listen() to callers David Gibson
2025-10-11  4:48 ` [PATCH 5/8] fwd: Share port scanning logic between init and timer cases David Gibson
2025-10-11  4:48 ` [PATCH 6/8] fwd: Check forwarding mode in fwd_scan_ports_*() rather than caller David Gibson
2025-10-11  4:48 ` [PATCH 7/8] fwd: Update all port maps before applying exclusions David Gibson
2025-10-11  4:48 ` [PATCH 8/8] tcp, udp: Don't exclude ports in {tcp,udp}_port_rebind() David Gibson

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=20251011044827.862757-4-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).