From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id DD0E05A0271 for ; Wed, 15 Nov 2023 06:25:45 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1700025940; bh=E35PViEuT76mSalpcdfeV2qs7GxOMPFvQ2QqmdPAsO8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XoHDp1/6n6+NFSxHr4FB1zYSvfybpcagzh1WYXCr4GSrogxKVUUUPVC1nOpuGJpId jv8+SkGfS7DvdmCSenAumyQBO2HMjeZbTpGP5ZxOFp5H7+/xw0WUWXFl97aD9WiS5f 3UXFuH902YaEqwCUWg7Hmo5wzzd+tzZV0o8eP2Lk= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4SVWpX2FXlz4xDB; Wed, 15 Nov 2023 16:25:40 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 1/3] tcp: Use common helper for rebinding inbound and outbound ports Date: Wed, 15 Nov 2023 16:25:32 +1100 Message-ID: <20231115052534.1826261-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231115052534.1826261-1-david@gibson.dropbear.id.au> References: <20231115052534.1826261-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: BVRHF5I6CEUYLFQQ6U3NIKKHQITZLAHY X-Message-ID-Hash: BVRHF5I6CEUYLFQQ6U3NIKKHQITZLAHY 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: 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 --- 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; -- 2.41.0