From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 6DA645A0287 for ; Wed, 16 Nov 2022 05:42:25 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NBr4R46txz4xyL; Wed, 16 Nov 2022 15:42:15 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1668573735; bh=eHccomJdvo4sOsDpsA7wt725WgLdg999Fa69j436KBE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WfP+fTCHeahqK93JQvjmfZWvpucEsSFwwYby2XJbHNRbt2l8RqQvpy44ey97duLzO D0SsTL74LKTbCYodPXp23Xdb/+htwQnq0tKC/rJFjJ1aPZm3lMmRzTNKU2VDmNptJG g0dHr36RQ7XQZluinexhFoKTxvGMFd5YEyoUelc4= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 29/32] tcp: Consolidate tcp_sock_init[46] Date: Wed, 16 Nov 2022 15:42:09 +1100 Message-Id: <20221116044212.3876516-30-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221116044212.3876516-1-david@gibson.dropbear.id.au> References: <20221116044212.3876516-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: FWTV4DHDCVL7HAXUEGFKKIAJ3P33NATF X-Message-ID-Hash: FWTV4DHDCVL7HAXUEGFKKIAJ3P33NATF 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.3 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: Previous cleanups mean that tcp_sock_init4() and tcp_sock_init6() are almost identical, and the remaining differences can be easily parameterized. Combine both into a single tcp_sock_init_af() function. Signed-off-by: David Gibson --- tcp.c | 50 +++++++++++++++----------------------------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/tcp.c b/tcp.c index fca5df4..616b9d0 100644 --- a/tcp.c +++ b/tcp.c @@ -2973,52 +2973,32 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, } /** - * tcp_sock_init4() - Initialise listening sockets for a given IPv4 port + * tcp_sock_init_af() - Initialise listening socket for a given af and port * @c: Execution context + * @af: Address family to listen on + * @port: Port, host order * @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: fd for the new listening socket, or -1 on failure */ -static void tcp_sock_init4(const struct ctx *c, const struct in_addr *addr, - const char *ifname, in_port_t port) +static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port, + const struct in_addr *addr, const char *ifname) { in_port_t idx = port + c->tcp.fwd_in.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx }; int s; - s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; + s = sock_l4(c, af, IPPROTO_TCP, addr, ifname, port, tref.u32); if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][V4] = s; -} + tcp_sock_init_ext[port][(af == AF_INET) ? V4 : V6] = s; -/** - * tcp_sock_init6() - Initialise listening sockets for a given IPv6 port - * @c: Execution context - * @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 - */ -static void tcp_sock_init6(const struct ctx *c, - const struct in6_addr *addr, const char *ifname, - in_port_t port) -{ - in_port_t idx = port + c->tcp.fwd_in.delta[port]; - union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx }; - int s; - - s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; + if (s < 0) + return -1; - if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][V6] = s; + tcp_sock_set_bufsize(c, s); + return s; } /** @@ -3033,9 +3013,9 @@ void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, const char *ifname, in_port_t port) { if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) - tcp_sock_init4(c, addr, ifname, port); + tcp_sock_init_af(c, AF_INET, port, addr, ifname); if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) - tcp_sock_init6(c, addr, ifname, port); + tcp_sock_init_af(c, AF_INET6, port, addr, ifname); } /** -- 2.38.1