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 v5 06/15] util, flow, pif: Simplify sock_l4_sa() interface
Date: Tue,  2 Dec 2025 15:02:06 +1100	[thread overview]
Message-ID: <20251202040215.2351792-7-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20251202040215.2351792-1-david@gibson.dropbear.id.au>

sock_l4_sa() has a somewhat confusing 'v6only' option controlling whether
to set the IPV6_V6ONLY socket option.  Usually it's set when the given
address is IPv6, but not when we want to create a dual stack listening
socket.  The latter only makes sense when the address is :: however.

Clarify this by only keeping the v6only option in an internal helper
sock_l4_().  External users will call either sock_l4() which always creates
a socket bound to a specific IP version, or sock_l4_dualstack() which
creates a dual stack socket, but takes only a port not an address.

We drop the '_sa' suffix while we're at it - it exists because this used
to be an internal version with a sock_l4() wrapper.  The wrapper no longer
exists so the '_sa' is no longer useful.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 flow.c |  6 ++----
 pif.c  | 12 ++++--------
 util.c | 45 ++++++++++++++++++++++++++++++++++++++++++---
 util.h |  6 ++++--
 4 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/flow.c b/flow.c
index 11e3f283..8d72965c 100644
--- a/flow.c
+++ b/flow.c
@@ -183,8 +183,7 @@ static int flowside_sock_splice(void *arg)
 
 	ns_enter(a->c);
 
-	a->fd = sock_l4_sa(a->c, a->type, a->sa, NULL,
-			   a->sa->sa_family == AF_INET6);
+	a->fd = sock_l4(a->c, a->type, a->sa, NULL);
 	a->err = errno;
 
 	return 0;
@@ -219,8 +218,7 @@ int flowside_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif,
 		else if (sa.sa_family == AF_INET6)
 			ifname = c->ip6.ifname_out;
 
-		return sock_l4_sa(c, type, &sa, ifname,
-				  sa.sa_family == AF_INET6);
+		return sock_l4(c, type, &sa, ifname);
 
 	case PIF_SPLICE: {
 		struct flowside_sock_args args = {
diff --git a/pif.c b/pif.c
index e6a50806..85904f35 100644
--- a/pif.c
+++ b/pif.c
@@ -76,11 +76,6 @@ int pif_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif,
 		const union inany_addr *addr, const char *ifname,
 		in_port_t port, uint32_t data)
 {
-	union sockaddr_inany sa = {
-		.sa6.sin6_family = AF_INET6,
-		.sa6.sin6_addr = in6addr_any,
-		.sa6.sin6_port = htons(port),
-	};
 	union epoll_ref ref;
 	int ret;
 
@@ -93,11 +88,12 @@ int pif_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif,
 	}
 
 	if (!addr) {
-		ref.fd = sock_l4_sa(c, type, &sa, ifname, false);
+		ref.fd = sock_l4_dualstack(c, type, port, ifname);
 	} else {
+		union sockaddr_inany sa;
+
 		pif_sockaddr(c, &sa, pif, addr, port);
-		ref.fd = sock_l4_sa(c, type, &sa, ifname,
-				    sa.sa_family == AF_INET6);
+		ref.fd = sock_l4(c, type, &sa, ifname);
 	}
 
 	if (ref.fd < 0)
diff --git a/util.c b/util.c
index 3dd1654f..ee369614 100644
--- a/util.c
+++ b/util.c
@@ -40,7 +40,7 @@
 #endif
 
 /**
- * sock_l4_sa() - Create and bind socket to socket address, add to epoll list
+ * sock_l4_() - Create and bind socket to socket address
  * @c:		Execution context
  * @type:	epoll type
  * @sa:		Socket address to bind to
@@ -49,8 +49,9 @@
  *
  * Return: newly created socket, negative error code on failure
  */
-int sock_l4_sa(const struct ctx *c, enum epoll_type type,
-	       const union sockaddr_inany *sa, const char *ifname, bool v6only)
+static int sock_l4_(const struct ctx *c, enum epoll_type type,
+		    const union sockaddr_inany *sa, const char *ifname,
+		    bool v6only)
 {
 	sa_family_t af = sa->sa_family;
 	bool freebind = false;
@@ -168,6 +169,44 @@ int sock_l4_sa(const struct ctx *c, enum epoll_type type,
 	return fd;
 }
 
+/**
+ * sock_l4() - Create and bind socket to given address
+ * @c:		Execution context
+ * @type:	epoll type
+ * @sa:		Socket address to bind to
+ * @ifname:	Interface for binding, NULL for any
+ *
+ * Return: newly created socket, negative error code on failure
+ */
+int sock_l4(const struct ctx *c, enum epoll_type type,
+	    const union sockaddr_inany *sa, const char *ifname)
+{
+	return sock_l4_(c, type, sa, ifname, sa->sa_family == AF_INET6);
+}
+
+/**
+ * sock_l4_dualstack() - Create a dual stack socket bound with wildcard address
+ * @c:		Execution context
+ * @type:	epoll type
+ * @port	Port to bind to (:: and 0.0.0.0)
+ * @ifname:	Interface for binding, NULL for any
+ *
+ * Return: newly created socket, negative error code on failure
+ *
+ * A dual stack socket is effectively bound to both :: and 0.0.0.0.
+ */
+int sock_l4_dualstack(const struct ctx *c, enum epoll_type type,
+		      in_port_t port, const char *ifname)
+{
+	union sockaddr_inany sa = {
+		.sa6.sin6_family = AF_INET6,
+		.sa6.sin6_addr = in6addr_any,
+		.sa6.sin6_port = htons(port),
+	};
+
+	return sock_l4_(c, type, &sa, ifname, 0);
+}
+
 /**
  * sock_unix() - Create and bind AF_UNIX socket
  * @sock_path:	Socket path. If empty, set on return (UNIX_SOCK_PATH as prefix)
diff --git a/util.h b/util.h
index fb82ad30..ebc14138 100644
--- a/util.h
+++ b/util.h
@@ -208,8 +208,10 @@ int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
 struct ctx;
 union sockaddr_inany;
 
-int sock_l4_sa(const struct ctx *c, enum epoll_type type,
-	       const union sockaddr_inany *sa, const char *ifname, bool v6only);
+int sock_l4(const struct ctx *c, enum epoll_type type,
+	    const union sockaddr_inany *sa, const char *ifname);
+int sock_l4_dualstack(const struct ctx *c, enum epoll_type type,
+		      in_port_t port, const char *ifname);
 int sock_unix(char *sock_path);
 void sock_probe_features(struct ctx *c);
 long timespec_diff_ms(const struct timespec *a, const struct timespec *b);
-- 
2.52.0


  parent reply	other threads:[~2025-12-02  4:02 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-02  4:02 [PATCH v5 00/15] Reduce differences between inbound and outbound socket binding David Gibson
2025-12-02  4:02 ` [PATCH v5 01/15] util: Correct error message on SO_BINDTODEVICE failure David Gibson
2025-12-02  4:02 ` [PATCH v5 02/15] util: Extend sock_probe_mem() to sock_probe_features() David Gibson
2025-12-03  6:34   ` Stefano Brivio
2025-12-02  4:02 ` [PATCH v5 03/15] conf: More useful errors for kernels without SO_BINDTODEVICE David Gibson
2025-12-02  4:02 ` [PATCH v5 04/15] flow: Remove bogus @path field from flowside_sock_args David Gibson
2025-12-02  4:02 ` [PATCH v5 05/15] inany: Let length of sockaddr_inany be implicit from the family David Gibson
2025-12-02  4:02 ` David Gibson [this message]
2025-12-02  4:02 ` [PATCH v5 07/15] tcp: Merge tcp_ns_sock_init[46]() into tcp_sock_init_one() David Gibson
2025-12-02  4:02 ` [PATCH v5 08/15] udp: Unify some more inbound/outbound parts of udp_sock_init() David Gibson
2025-12-02  4:02 ` [PATCH v5 09/15] udp: Move udp_sock_init() special case to its caller David Gibson
2025-12-02  4:02 ` [PATCH v5 10/15] util: Fix setting of IPV6_V6ONLY socket option David Gibson
2025-12-02  4:02 ` [PATCH v5 11/15] tcp, udp: Remove fallback if creating dual stack socket fails David Gibson
2025-12-02  4:02 ` [PATCH v5 12/15] tcp, udp: Bind outbound listening sockets by interface instead of address David Gibson
2025-12-03  4:41   ` David Gibson
2025-12-03  6:38     ` Stefano Brivio
2025-12-03 13:13       ` Stefano Brivio
2025-12-02  4:02 ` [PATCH v5 13/15] util: Rename sock_l4_dualstack() to sock_l4_dualstack_any() David Gibson
2025-12-02  4:02 ` [PATCH v5 14/15] tcp: Always populate oaddr field for socket initiated flows David Gibson
2025-12-02  4:02 ` [PATCH v5 15/15] fwd: Preserve non-standard loopback address when splice forwarding David Gibson
2025-12-03  6:34 ` [PATCH v5 00/15] Reduce differences between inbound and outbound socket binding 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=20251202040215.2351792-7-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).