public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 3/3] udp: Use dual stack sockets for port forwarding when possible
Date: Mon, 26 Aug 2024 19:37:16 +1000	[thread overview]
Message-ID: <20240826093716.1925064-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240826093716.1925064-1-david@gibson.dropbear.id.au>

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 (assuming passt/pasta is listening on the same
ports for IPv4 and IPv6).  When forwarding many ports (e.g. -u all) this
can significantly reduce the amount of kernel memory that passt consumes.

We've used such dual stack sockets for TCP since 8e914238b "tcp: Use dual
stack sockets for port forwarding when possible".  Add similar support for
UDP "listening" sockets.  Since UDP sockets don't use as much kernel memory
as TCP sockets this isn't as big a saving, but it's still significant.
When forwarding all TCP and UDP ports for both IPv4 & IPv6 (-t all -u all),
this reduces kernel memory usage from ~522 MiB to ~380MiB (kernel version
6.10.6 on Fedora 40, x86_64).

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 udp.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/udp.c b/udp.c
index a2e7043a..8bfafed2 100644
--- a/udp.c
+++ b/udp.c
@@ -726,6 +726,25 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
 	else
 		uref.pif = PIF_HOST;
 
+	if (af == AF_UNSPEC && c->ifi4 && c->ifi6) {
+		int s;
+
+		/* Attempt to get a dual stack socket */
+		if (!ns) {
+			s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN,
+				    addr, ifname, port, uref.u32);
+			udp_splice_init[V4][port] = s < 0 ? -1 : s;
+			udp_splice_init[V6][port] = s < 0 ? -1 : s;
+		} else {
+			s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN,
+				    &in4addr_loopback, ifname, port, uref.u32);
+			udp_splice_ns[V4][port] = s < 0 ? -1 : s;
+			udp_splice_ns[V6][port] = s < 0 ? -1 : s;
+		}
+		if (IN_INTERVAL(0, FD_REF_MAX, s))
+			return 0;
+	}
+
 	if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) {
 		if (!ns) {
 			r4 = sock_l4(c, AF_INET, EPOLL_TYPE_UDP_LISTEN,
-- 
@@ -726,6 +726,25 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
 	else
 		uref.pif = PIF_HOST;
 
+	if (af == AF_UNSPEC && c->ifi4 && c->ifi6) {
+		int s;
+
+		/* Attempt to get a dual stack socket */
+		if (!ns) {
+			s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN,
+				    addr, ifname, port, uref.u32);
+			udp_splice_init[V4][port] = s < 0 ? -1 : s;
+			udp_splice_init[V6][port] = s < 0 ? -1 : s;
+		} else {
+			s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN,
+				    &in4addr_loopback, ifname, port, uref.u32);
+			udp_splice_ns[V4][port] = s < 0 ? -1 : s;
+			udp_splice_ns[V6][port] = s < 0 ? -1 : s;
+		}
+		if (IN_INTERVAL(0, FD_REF_MAX, s))
+			return 0;
+	}
+
 	if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) {
 		if (!ns) {
 			r4 = sock_l4(c, AF_INET, EPOLL_TYPE_UDP_LISTEN,
-- 
2.46.0


  parent reply	other threads:[~2024-08-26  9:37 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-26  9:37 [PATCH 0/3] Dual stack sockets for UDP David Gibson
2024-08-26  9:37 ` [PATCH 1/3] udp: Merge udp[46]_mh_recv arrays David Gibson
2024-08-26 19:32   ` Stefano Brivio
2024-08-27  1:12     ` David Gibson
2024-08-27  5:33       ` Stefano Brivio
2024-08-27  6:04         ` David Gibson
2024-08-26  9:37 ` [PATCH 2/3] udp: Remove unnnecessary local from udp_sock_init() David Gibson
2024-08-26  9:37 ` David Gibson [this message]
2024-08-26 19:33 ` [PATCH 0/3] Dual stack sockets for UDP Stefano Brivio

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=20240826093716.1925064-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).