From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 774FF5A005E for ; Mon, 9 Jan 2023 01:50:32 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NqwN50NcFz4xyB; Mon, 9 Jan 2023 11:50:29 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1673225429; bh=FfGsHyqyevjhAKCWoNtbvp+CYXLlQ77Bdnzmt6jTMZ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eUL7KXcr2jmTvipSWwg0R8PT5Qj/YgjVCPEFOOZ41Km+k9lsUwW2AiKBzm9LmUIw9 1OaVdg2FJyd1toQzLfo7NxPRtAlL4mU7cWg/OLBz9Cooj3X81C1IOJ7BpLhxgADKDB mtPi7lWlssQJ2SiGJns34K9zZaRVfVsQ7UrVd6ig= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 1/5] tcp: Make a helper to refill each socket pool Date: Mon, 9 Jan 2023 11:50:22 +1100 Message-Id: <20230109005026.24512-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230109005026.24512-1-david@gibson.dropbear.id.au> References: <20230109005026.24512-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: IJIX64MGRACTGUJUCLFLS4Q4M5S6FC22 X-Message-ID-Hash: IJIX64MGRACTGUJUCLFLS4Q4M5S6FC22 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: tcp_sock_refill() contains two near-identical loops to refill the IPv4 and IPv6 socket pools. In addition, if we get an error on the IPv4 pool we exit early and won't attempt to refill the IPv6 pool. That doesn't seem sensible, since these are essentially completely independent, and there may be value to filling up either one without the other. Address both of these with a helper function 'tcp_sock_refill_pool()' to refill a single given pool. Signed-off-by: David Gibson --- tcp.c | 63 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/tcp.c b/tcp.c index 26037b3..becad80 100644 --- a/tcp.c +++ b/tcp.c @@ -2982,6 +2982,34 @@ static int tcp_ns_socks_init(void *arg) return 0; } +/** + * tcp_sock_refill_pool() - Refill one pool of pre-opened sockets + * @c: Execution context + * @pool: Pool of sockets to refill + * @af: Address family to use + */ +static void tcp_sock_refill_pool(const struct ctx *c, int pool[], int af) +{ + int i; + + for (i = 0; i < TCP_SOCK_POOL_SIZE; i++) { + int *s = &pool[i]; + + if (*s >= 0) + break; + + *s = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP); + if (*s > SOCKET_MAX) { + close(*s); + *s = -1; + return; + } + + if (*s >= 0) + tcp_sock_set_bufsize(c, *s); + } +} + /** * struct tcp_sock_refill_arg - Arguments for tcp_sock_refill() * @c: Execution context @@ -3001,7 +3029,7 @@ struct tcp_sock_refill_arg { static int tcp_sock_refill(void *arg) { struct tcp_sock_refill_arg *a = (struct tcp_sock_refill_arg *)arg; - int i, *p4, *p6; + int *p4, *p6; if (a->ns) { ns_enter(a->c); @@ -3012,36 +3040,11 @@ static int tcp_sock_refill(void *arg) p6 = init_sock_pool6; } - for (i = 0; a->c->ifi4 && i < TCP_SOCK_POOL_SIZE; i++, p4++) { - if (*p4 >= 0) - break; - - *p4 = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP); - if (*p4 > SOCKET_MAX) { - close(*p4); - *p4 = -1; - return -EIO; - } - - if (*p4 >= 0) - tcp_sock_set_bufsize(a->c, *p4); - } - - for (i = 0; a->c->ifi6 && i < TCP_SOCK_POOL_SIZE; i++, p6++) { - if (*p6 >= 0) - break; - - *p6 = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, - IPPROTO_TCP); - if (*p6 > SOCKET_MAX) { - close(*p6); - *p6 = -1; - return -EIO; - } + if (a->c->ifi4) + tcp_sock_refill_pool(a->c, p4, AF_INET); - if (*p6 >= 0) - tcp_sock_set_bufsize(a->c, *p6); - } + if (a->c->ifi6) + tcp_sock_refill_pool(a->c, p6, AF_INET6); return 0; } -- 2.39.0