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=202510 header.b=aOiN7cxN; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 2C3B75A0BC5 for ; Tue, 02 Dec 2025 05:02:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202510; t=1764648138; bh=siUSb+L9YzXwu/YPfSsQtoMlmF5/MSnOKJlGuzRwBrg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aOiN7cxNx4UVTLWe87f/XmvfSQLg29iQzgo0xMYayWdUHPghNI1mD/JF1v8yAgcfW h0E+pfHMLdL4oPB1Z/oXN0Ma8+jI+IG8bzCLGWo9nGv/UayIiyMzXYj37f4GI6O0Mg VJ4EiZr5f6Ctkt+0IdYvSom9DneEoCFdWRtM5B2Vz/FmYgzCaC3FLeE/t7w2jo6/3x uk3F5RrXqu+Uk6VdfEjj+CBnhWLGQh+gqu8SaIkcM4J4p6HIzHucKwgetP6+gFoLFZ 651TkP0yiiVpTKXUFECy7mR+Ze0THIsiQQn0dSt+KiaQrPDmToxdeXyDYCIm8trZNg 7kaxrSP79QiXg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dL6Y60tZNz4wQJ; Tue, 02 Dec 2025 15:02:18 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v5 06/15] util, flow, pif: Simplify sock_l4_sa() interface Date: Tue, 2 Dec 2025 15:02:06 +1100 Message-ID: <20251202040215.2351792-7-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251202040215.2351792-1-david@gibson.dropbear.id.au> References: <20251202040215.2351792-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: ZI63M5DGQVTNFQHG7D5NJFW2IMRNDONR X-Message-ID-Hash: ZI63M5DGQVTNFQHG7D5NJFW2IMRNDONR 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: 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 --- 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