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 3E4555A0273 for ; Wed, 16 Nov 2022 05:42:26 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NBr4R4TWhz4xyT; Wed, 16 Nov 2022 15:42:15 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1668573735; bh=xItTmlPLBEe8tsoZ0AI94uWG3+JFC5uPMMhAJJP66GE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GB3cmJ5ALQv3l92D/2aD7PQDURkT9YCznsF+9Fmb0wLy2oc1/ej8YJeiOVIGeJzYW lkA4/A+10Z/cdZ4vOLuyUVdA7Ueoq4Ws2V5YwfU7vhxDV0ZyRmS/WRIb+UJ6goKYho kW1rS+gk/8AjiqCyQzJ6e2MEZyHbzUwroQXrnjGQ= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 32/32] tcp: Use dual stack sockets for port forwarding when possible Date: Wed, 16 Nov 2022 15:42:12 +1100 Message-Id: <20221116044212.3876516-33-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221116044212.3876516-1-david@gibson.dropbear.id.au> References: <20221116044212.3876516-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: TVKT7UBDSLO3CEFD3OYFRZQP5SUD3QNR X-Message-ID-Hash: TVKT7UBDSLO3CEFD3OYFRZQP5SUD3QNR 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 616b9d0..5860c9f 100644 --- a/tcp.c +++ b/tcp.c @@ -2991,8 +2991,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; @@ -3012,6 +3016,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