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 1/4] udp: Don't attempt to get dual-stack sockets in nonsensical cases
Date: Fri, 20 Sep 2024 14:12:41 +1000	[thread overview]
Message-ID: <20240920041244.593192-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240920041244.593192-1-david@gibson.dropbear.id.au>

To save some kernel memory we try to use "dual stack" sockets (that listen
to both IPv4 and IPv6 traffic) when possible.   However udp_sock_init()
attempts to do this in some cases that can't work.  Specifically we can
only do this when listening on any address.  That's never true for the
ns (splicing) case, because we always listen on loopback.  For the !ns
case and AF_UNSPEC case, addr should always be NULL, but add an assert to
verify.

This is harmless: if addr is non-NULL, sock_l4() will just fail and we'll
fall back to the other path.  But, it's messy and makes some upcoming
changes harder, so avoid attempting this in cases we know can't work.

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

diff --git a/udp.c b/udp.c
index 7b283138..8cea80cd 100644
--- a/udp.c
+++ b/udp.c
@@ -803,21 +803,16 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
 
 	ASSERT(!c->no_udp);
 
-	if (af == AF_UNSPEC && c->ifi4 && c->ifi6) {
+	if (af == AF_UNSPEC && c->ifi4 && c->ifi6 && !ns) {
 		int s;
 
+		ASSERT(!addr);
+
 		/* 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;
-		}
+		s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN,
+			    NULL, ifname, port, uref.u32);
+		udp_splice_init[V4][port] = s < 0 ? -1 : s;
+		udp_splice_init[V6][port] = s < 0 ? -1 : s;
 		if (IN_INTERVAL(0, FD_REF_MAX, s))
 			return 0;
 	}
-- 
@@ -803,21 +803,16 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
 
 	ASSERT(!c->no_udp);
 
-	if (af == AF_UNSPEC && c->ifi4 && c->ifi6) {
+	if (af == AF_UNSPEC && c->ifi4 && c->ifi6 && !ns) {
 		int s;
 
+		ASSERT(!addr);
+
 		/* 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;
-		}
+		s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN,
+			    NULL, ifname, port, uref.u32);
+		udp_splice_init[V4][port] = s < 0 ? -1 : s;
+		udp_splice_init[V6][port] = s < 0 ? -1 : s;
 		if (IN_INTERVAL(0, FD_REF_MAX, s))
 			return 0;
 	}
-- 
2.46.1


  reply	other threads:[~2024-09-20  4:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-20  4:12 [PATCH 0/4] Use inany addresses through port forwarding configuration David Gibson
2024-09-20  4:12 ` David Gibson [this message]
2024-09-20  4:12 ` [PATCH 2/4] util, pif: Replace sock_l4() with pif_sock_l4() David Gibson
2024-09-20  4:12 ` [PATCH 3/4] tcp, udp: Make {tcp,udp}_sock_init() take an inany address David Gibson
2024-09-20  4:12 ` [PATCH 4/4] inany: Add inany_pton() helper David Gibson
2024-09-25 17:39 ` [PATCH 0/4] Use inany addresses through port forwarding configuration 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=20240920041244.593192-2-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).