From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202510 header.b=nlqIE+Il; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 25A9D5A061A for ; Fri, 31 Oct 2025 05:19:35 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202510; t=1761884372; bh=iPg5Uwk2cjfufazTm3j5y2QR7M3rf7PTiHcuI8wMDxo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nlqIE+Ilm8J6axCI0hZiUWmvPVhX7CbMYpu0pL+dV3sS/hDWuvtU+Pl8O9VZuetGk CKEVhqMVwsLczyHk6f35IlScQrnlw2AseP78NMDpAomZuCildN4E41uzxpiUC+n6j6 uyrtvtmAMJB9F1UD8bMSd2U1qsVIfO8eQz7W7LlwBs5pB3PlfxzCx1MLPWlEKC9MVv iLaaYLlPGR/4fI+nzVjq/6Fc3MRV7/FDFFqtnCrj0rIIMVkUqDb/pI911TzMnG055L N0ihneB4EEdn7gLZLV6dX0hG/rV3FyJddrOcD3x3PwRV215Ue7N5H4L5D348LS0FVx MJ2USoMF6PUYQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4cySRm1z4xz4xH1; Fri, 31 Oct 2025 15:19:32 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 3/8] fwd: Consolidate scans (not rebinds) in fwd.c Date: Fri, 31 Oct 2025 15:19:25 +1100 Message-ID: <20251031041930.1272259-4-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251031041930.1272259-1-david@gibson.dropbear.id.au> References: <20251031041930.1272259-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: WLYZ5ZLSXL75UYNJKEAOWRVAAMHRBRIR X-Message-ID-Hash: WLYZ5ZLSXL75UYNJKEAOWRVAAMHRBRIR X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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 6e248fe8..523097b8 100644 --- a/fwd.c +++ b/fwd.c @@ -365,7 +365,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); @@ -379,9 +380,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]; @@ -460,10 +462,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 ec7bb3bb..77925822 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 fc1a2610..c35c1c3f 100644 --- a/tcp.c +++ b/tcp.c @@ -2884,22 +2884,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 9cd736d7..00823867 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 d95ef2f6..8cff8809 100644 --- a/udp.c +++ b/udp.c @@ -1252,24 +1252,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 e3fb78ba..f1d83f38 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); /** -- 2.51.0