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=202408 header.b=gjKDFNeR; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 78D405A004C for ; Mon, 26 Aug 2024 11:37:35 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202408; t=1724665039; bh=ECECIOQWaVN3QJ3Iz65sWD4P9lExZ0JSpl9cs0yIc6s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gjKDFNeRJtJ4TTRwH8RXb2SARFxWzuoB6mWmf9jDkPAh9B5xXK0XVAEvmiBkbaTsv 1zsMs+djlGtnTRhavaF/QwN3NQv1zn3YCjwddHJJ9zK9DdQ9Az7aR7a6yqpygCB5xJ b4MpvmqemyCdMH+ZA17e5PoZvJbejB7Ss4EXuBMFqy1awU4u0ryVEprZaKMVGfBb9m H4U//cSfEHNRV+4kgLv5PaTFcLL2CmLkaXu4l4jTAL2vt92C1ZdTK4MhvnkF5iot7N HkdxktIFmxli2ga9tk7k8B2dHC1gqzgVJ/pzO0MLJBGUEh5cXKutCs9AD8sqPPypSP Xkl9XmugUlwVQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WslvM3SwKz4x82; Mon, 26 Aug 2024 19:37:19 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 3/3] udp: Use dual stack sockets for port forwarding when possible Date: Mon, 26 Aug 2024 19:37:16 +1000 Message-ID: <20240826093716.1925064-4-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240826093716.1925064-1-david@gibson.dropbear.id.au> References: <20240826093716.1925064-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: HBHMNCGEBSPET6KNKNJ7T2BCRIXUVP66 X-Message-ID-Hash: HBHMNCGEBSPET6KNKNJ7T2BCRIXUVP66 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: 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 --- 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, -- 2.46.0