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=202512 header.b=a/SetjgZ; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 17B195A065C for ; Thu, 18 Dec 2025 07:24:08 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1766039045; bh=8ktfWu+wPV9QX6+agP4oDneStmZ9QHWkrb/ovBUYabg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a/SetjgZ2EVY9F1xATWeloAEQD/pDQ0RfxWc6m06Uk6yvIaKUUcp/pSH47YlBLTMX hdkN+vRkw/3mEkl8GvN7sQtoFuFCCF1Njs15uS4d1zeSy5Cp9iUG/CumggEw0X5n7m fnh81JM1Xk/nP9P1cynhZNGdIcaSDWcnKOpBOD9GVDUmTQls4ym3DiSrDsNIIf4LUB L3fGQ1dzWK932x8lDo3VNg7lWX9uwKP8dvd4kv4TlczKDkHSvwh03VQ/nDrChrDKIO 3IbzhFem30kFeI4EtqhiCvJwnf2k9a3a6Elz7+Ha7iqVnxO+qOoHuOPExWkzlQ3OaE hfo+N9YsdzREw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dX0xK2jPNz4wD7; Thu, 18 Dec 2025 17:24:05 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 1/2] tcp: Combine tcp_sock_init_one() and tcp_sock_init() into tcp_listen() Date: Thu, 18 Dec 2025 17:22:41 +1100 Message-ID: <20251218062242.728289-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251218062242.728289-1-david@gibson.dropbear.id.au> References: <20251218062242.728289-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 66Q5RIU7TVKG5KFDFSYHLNKUHUAHOXRW X-Message-ID-Hash: 66Q5RIU7TVKG5KFDFSYHLNKUHUAHOXRW 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: Despite the name, these two functions are specifically for creating listening sockets, not any others. Recent changes mean that there's always exactly one call of tcp_sock_init_one() call per call to tcp_sock_init(). So combine them into tcp_listen(). While we're there remove a redundant check for (s > FD_REF_MAX). pif_sock_l4() already checks for this (and must, in order to properly populate the epoll reference). Signed-off-by: David Gibson --- conf.c | 2 +- tcp.c | 94 +++++++++++++++++++++------------------------------------- tcp.h | 6 ++-- 3 files changed, 38 insertions(+), 64 deletions(-) diff --git a/conf.c b/conf.c index 2942c8c2..dada3b1f 100644 --- a/conf.c +++ b/conf.c @@ -175,7 +175,7 @@ static void conf_ports_range_except(const struct ctx *c, char optname, fwd->delta[i] = to - first; if (optname == 't') - ret = tcp_sock_init(c, PIF_HOST, addr, ifname, i); + ret = tcp_listen(c, PIF_HOST, addr, ifname, i); else if (optname == 'u') ret = udp_sock_init(c, PIF_HOST, addr, ifname, i); else diff --git a/tcp.c b/tcp.c index b179e399..71c6d632 100644 --- a/tcp.c +++ b/tcp.c @@ -2673,66 +2673,24 @@ void tcp_sock_handler(const struct ctx *c, union epoll_ref ref, } /** - * tcp_sock_init_one() - Initialise listening socket for address and port + * tcp_listen() - Create listening socket * @c: Execution context * @pif: Interface to open the socket for (PIF_HOST or PIF_SPLICE) - * @addr: Pointer to address for binding, NULL for dual stack any - * @ifname: Name of interface to bind to, NULL if not configured + * @addr: Pointer to address for binding, NULL for any + * @ifname: Name of interface to bind to, NULL for any * @port: Port, host order * - * Return: fd for the new listening socket, negative error code on failure - * - * If pif == PIF_SPLICE, the caller must have already entered the guest ns. + * Return: 0 on success, negative error code on failure */ -static int tcp_sock_init_one(const struct ctx *c, uint8_t pif, - const union inany_addr *addr, const char *ifname, - in_port_t port) +int tcp_listen(const struct ctx *c, uint8_t pif, + const union inany_addr *addr, const char *ifname, in_port_t port) { union tcp_listen_epoll_ref tref = { .port = port, .pif = pif, }; const struct fwd_ports *fwd; - int s; - - if (pif == PIF_HOST) - fwd = &c->tcp.fwd_in; - else - fwd = &c->tcp.fwd_out; - - s = pif_sock_l4(c, EPOLL_TYPE_TCP_LISTEN, pif, addr, ifname, - port, tref.u32); - - if (fwd->mode == FWD_AUTO) { - int (*socks)[IP_VERSIONS] = pif == PIF_SPLICE ? - tcp_sock_ns : tcp_sock_init_ext; - - if (!addr || inany_v4(addr)) - socks[port][V4] = s < 0 ? -1 : s; - if (!addr || !inany_v4(addr)) - socks[port][V6] = s < 0 ? -1 : s; - } - - if (s < 0) - return s; - - return s; -} - -/** - * tcp_sock_init() - Create listening socket for a given host ("inbound") port - * @c: Execution context - * @pif: Interface to open the socket for (PIF_HOST or PIF_SPLICE) - * @addr: Pointer to address for binding, NULL if not configured - * @ifname: Name of interface to bind to, NULL if not configured - * @port: Port, host order - * - * Return: 0 on success, negative error code on failure - */ -int tcp_sock_init(const struct ctx *c, uint8_t pif, - const union inany_addr *addr, const char *ifname, - in_port_t port) -{ + int (*socks)[IP_VERSIONS]; int s; ASSERT(!c->no_tcp); @@ -2754,33 +2712,49 @@ int tcp_sock_init(const struct ctx *c, uint8_t pif, return 0; } - s = tcp_sock_init_one(c, pif, addr, ifname, port); + if (pif == PIF_HOST) { + fwd = &c->tcp.fwd_in; + socks = tcp_sock_init_ext; + } else { + ASSERT(pif == PIF_SPLICE); + fwd = &c->tcp.fwd_out; + socks = tcp_sock_ns; + } + + s = pif_sock_l4(c, EPOLL_TYPE_TCP_LISTEN, pif, addr, ifname, + port, tref.u32); + + if (fwd->mode == FWD_AUTO) { + if (!addr || inany_v4(addr)) + socks[port][V4] = s < 0 ? -1 : s; + if (!addr || !inany_v4(addr)) + socks[port][V6] = s < 0 ? -1 : s; + } + if (s < 0) return s; - if (s > FD_REF_MAX) - return -EIO; return 0; } /** - * tcp_ns_sock_init() - Init socket to listen for spliced outbound connections + * tcp_ns_listen() - Init socket to listen for spliced outbound connections * @c: Execution context * @port: Port, host order */ -static void tcp_ns_sock_init(const struct ctx *c, in_port_t port) +static void tcp_ns_listen(const struct ctx *c, in_port_t port) { ASSERT(!c->no_tcp); if (!c->no_bindtodevice) { - tcp_sock_init(c, PIF_SPLICE, NULL, "lo", port); + tcp_listen(c, PIF_SPLICE, NULL, "lo", port); return; } if (c->ifi4) - tcp_sock_init_one(c, PIF_SPLICE, &inany_loopback4, NULL, port); + tcp_listen(c, PIF_SPLICE, &inany_loopback4, NULL, port); if (c->ifi6) - tcp_sock_init_one(c, PIF_SPLICE, &inany_loopback6, NULL, port); + tcp_listen(c, PIF_SPLICE, &inany_loopback6, NULL, port); } /** @@ -2801,7 +2775,7 @@ static int tcp_ns_socks_init(void *arg) if (!bitmap_isset(c->tcp.fwd_out.map, port)) continue; - tcp_ns_sock_init(c, port); + tcp_ns_listen(c, port); } return 0; @@ -3003,9 +2977,9 @@ static void tcp_port_rebind(struct ctx *c, bool outbound) if ((c->ifi4 && socks[port][V4] == -1) || (c->ifi6 && socks[port][V6] == -1)) { if (outbound) - tcp_ns_sock_init(c, port); + tcp_ns_listen(c, port); else - tcp_sock_init(c, PIF_HOST, NULL, NULL, port); + tcp_listen(c, PIF_HOST, NULL, NULL, port); } } } diff --git a/tcp.h b/tcp.h index 3f21e755..9dd88762 100644 --- a/tcp.h +++ b/tcp.h @@ -18,9 +18,9 @@ void tcp_sock_handler(const struct ctx *c, union epoll_ref ref, int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af, const void *saddr, const void *daddr, uint32_t flow_lbl, const struct pool *p, int idx, const struct timespec *now); -int tcp_sock_init(const struct ctx *c, uint8_t pif, - const union inany_addr *addr, const char *ifname, - in_port_t port); +int tcp_listen(const struct ctx *c, uint8_t pif, + const union inany_addr *addr, const char *ifname, + in_port_t port); int tcp_init(struct ctx *c); void tcp_port_rebind_all(struct ctx *c); void tcp_timer(const struct ctx *c, const struct timespec *now); -- 2.52.0