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 348F05A0271 for ; Thu, 17 Nov 2022 06:59:23 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NCTkl5Zk2z4xyd; Thu, 17 Nov 2022 16:59:11 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1668664751; bh=7ypTLQFLAAr2VVq1gOQ1w5sQZtCdoiT5s8UD+wC064M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aKTtVegQa+VsiFSBBKbV/ZxwCFc1YTea+tyw+x9G9GOPleD0UQaaq7SGHkswt/Qjw PvJ5vfsCfuYHBX2kixkrdPkNvDAkZ7lqE0cDEHqNn162bHqo46pM92iX6JV158uHXi p5FG4fW8eCGFPQOODnXBvP+J0diW185q0EIbXHyo= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 32/32] tcp: Use dual stack sockets for port forwarding when possible Date: Thu, 17 Nov 2022 16:59:08 +1100 Message-Id: <20221117055908.2782981-33-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221117055908.2782981-1-david@gibson.dropbear.id.au> References: <20221117055908.2782981-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: VFWUZVQ3LHHYQODGQKMNBSHJWJL3UALN X-Message-ID-Hash: VFWUZVQ3LHHYQODGQKMNBSHJWJL3UALN 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.3 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: Platforms like Linux allow IPv6 sockets to listen for IPv4 connections as well as native IPv6 connections. By doing this we halve the number of listening sockets we need for TCP (assuming passt/pasta is listening on the same ports for IPv4 and IPv6). When forwarding many ports (e.g. -t all) this can significantly reduce the amount of kernel memory that passt consumes. When forwarding all TCP and UDP ports for both IPv4 and IPv6 (-t all -u all), this reduces kernel memory usage from ~677MiB to ~487MiB (kernel version 6.0.8 on Fedora 37, x86_64). Signed-off-by: David Gibson --- tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tcp.c b/tcp.c index c199863..8044617 100644 --- a/tcp.c +++ b/tcp.c @@ -2996,8 +2996,12 @@ static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port, s = sock_l4(c, af, IPPROTO_TCP, addr, ifname, port, tref.u32); - if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][(af == AF_INET) ? V4 : V6] = s; + if (c->tcp.fwd_in.mode == FWD_AUTO) { + if (af == AF_INET || af == AF_UNSPEC) + tcp_sock_init_ext[port][V4] = s; + if (af == AF_INET6 || af == AF_UNSPEC) + tcp_sock_init_ext[port][V6] = s; + } if (s < 0) return -1; @@ -3017,6 +3021,12 @@ static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port, void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, const char *ifname, in_port_t port) { + if (af == AF_UNSPEC && c->ifi4 && c->ifi6) + /* Attempt to get a dual stack socket */ + if (tcp_sock_init_af(c, AF_UNSPEC, port, addr, ifname) >= 0) + return; + + /* Otherwise create a socket per IP version */ if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) tcp_sock_init_af(c, AF_INET, port, addr, ifname); if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) -- 2.38.1