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 7C82C5A0279 for ; Thu, 29 Feb 2024 09:42:57 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709196173; bh=64b8mGNIg3ZZmeiGk8IOdgwP8PGIaHPflpsI1+D2XSQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aG451TuXSCyO8OihwcxbpyPk+yNO+9ouqLDcEMAXHQARjQZ05Kwjcb40O4qMIe5Ec AqMwYVyiYXR2meb+lWUulbPPfaNNd4QAuZVjSHvpcvxM0UEDB4BGRw7xm2vqcO7Woq fWXkQzEIZdyHB3GkJA4y3NbKqdySDUaCOWkm2PhPoDe62PgYuwNs3WYeUashXnVzc2 cGeSsQ8puBo6WPyIQW6Z2zfecYFplKjoPqchOuWR3QzEnI2eaiELgb5FYETs5aBDi7 dJnCDr5Kb4alaKfIoxnxg+jd945n3WFrw95obg5JL9+5RjUevGHXk1FbFnnlsCxAUk 0AADxj3f0pWOA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Tll991nrVz4wyj; Thu, 29 Feb 2024 19:42:53 +1100 (AEDT) From: David Gibson To: Stefano Brivio , Laurent Vivier , passt-dev@passt.top Subject: [PATCH 1/6] udp: Refactor udp_sock[46]_iov_init() Date: Thu, 29 Feb 2024 19:42:45 +1100 Message-ID: <20240229084250.3202450-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240229084250.3202450-1-david@gibson.dropbear.id.au> References: <20240229084250.3202450-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: XQ7OQREHKLKDD564DFN6MRN7526C3BX5 X-Message-ID-Hash: XQ7OQREHKLKDD564DFN6MRN7526C3BX5 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: Each of these functions have 3 essentially identical loops in a row. Merge the loops into a single common udp_sock_iov_init() function, calling udp_sock[46]_iov_init_one() helpers to initialize each "slot" in the various parallel arrays. This is slightly neater now, and more naturally allows changes we want to make where more initialization will become common between IPv4 and IPv6. Signed-off-by: David Gibson --- udp.c | 102 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/udp.c b/udp.c index 5b7778eb..092d343e 100644 --- a/udp.c +++ b/udp.c @@ -307,72 +307,74 @@ void udp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s) } /** - * udp_sock4_iov_init() - Initialise scatter-gather L2 buffers for IPv4 sockets + * udp_sock4_iov_init_one() - Initialise a scatter-gather L2 buffer for IPv4 * @c: Execution context + * @i: Index of buffer to initialize */ -static void udp_sock4_iov_init(const struct ctx *c) +static void udp_sock4_iov_init_one(const struct ctx *c, size_t i) { - struct mmsghdr *h; - int i; - - for (i = 0; i < ARRAY_SIZE(udp4_l2_buf); i++) { - udp4_l2_buf[i] = (struct udp4_l2_buf_t) { - .taph = TAP_HDR_INIT(ETH_P_IP), - .iph = L2_BUF_IP4_INIT(IPPROTO_UDP) - }; - } - - for (i = 0, h = udp4_l2_mh_sock; i < UDP_MAX_FRAMES; i++, h++) { - struct msghdr *mh = &h->msg_hdr; + struct msghdr *mh = &udp4_l2_mh_sock[i].msg_hdr; + struct udp4_l2_buf_t *buf = &udp4_l2_buf[i]; + struct iovec *siov = &udp4_l2_iov_sock[i]; + struct iovec *tiov = &udp4_l2_iov_tap[i]; - mh->msg_name = &udp4_l2_buf[i].s_in; - mh->msg_namelen = sizeof(udp4_l2_buf[i].s_in); + *buf = (struct udp4_l2_buf_t) { + .taph = TAP_HDR_INIT(ETH_P_IP), + .iph = L2_BUF_IP4_INIT(IPPROTO_UDP) + }; - udp4_l2_iov_sock[i].iov_base = udp4_l2_buf[i].data; - udp4_l2_iov_sock[i].iov_len = sizeof(udp4_l2_buf[i].data); - mh->msg_iov = &udp4_l2_iov_sock[i]; - mh->msg_iovlen = 1; - } + siov->iov_base = buf->data; + siov->iov_len = sizeof(buf->data); - for (i = 0; i < UDP_MAX_FRAMES; i++) { - struct iovec *iov = &udp4_l2_iov_tap[i]; + mh->msg_name = &buf->s_in; + mh->msg_namelen = sizeof(buf->s_in); + mh->msg_iov = siov; + mh->msg_iovlen = 1; - iov->iov_base = tap_iov_base(c, &udp4_l2_buf[i].taph); - } + tiov->iov_base = tap_iov_base(c, &buf->taph); } /** - * udp_sock6_iov_init() - Initialise scatter-gather L2 buffers for IPv6 sockets + * udp_sock6_iov_init_one() - Initialise a scatter-gather L2 buffer for IPv6 * @c: Execution context + * @i: Index of buffer to initialize */ -static void udp_sock6_iov_init(const struct ctx *c) +static void udp_sock6_iov_init_one(const struct ctx *c, size_t i) { - struct mmsghdr *h; - int i; + struct msghdr *mh = &udp6_l2_mh_sock[i].msg_hdr; + struct udp6_l2_buf_t *buf = &udp6_l2_buf[i]; + struct iovec *siov = &udp6_l2_iov_sock[i]; + struct iovec *tiov = &udp6_l2_iov_tap[i]; - for (i = 0; i < ARRAY_SIZE(udp6_l2_buf); i++) { - udp6_l2_buf[i] = (struct udp6_l2_buf_t) { - .taph = TAP_HDR_INIT(ETH_P_IPV6), - .ip6h = L2_BUF_IP6_INIT(IPPROTO_UDP) - }; - } + *buf = (struct udp6_l2_buf_t) { + .taph = TAP_HDR_INIT(ETH_P_IPV6), + .ip6h = L2_BUF_IP6_INIT(IPPROTO_UDP) + }; - for (i = 0, h = udp6_l2_mh_sock; i < UDP_MAX_FRAMES; i++, h++) { - struct msghdr *mh = &h->msg_hdr; + siov->iov_base = buf->data; + siov->iov_len = sizeof(buf->data); - mh->msg_name = &udp6_l2_buf[i].s_in6; - mh->msg_namelen = sizeof(struct sockaddr_in6); + mh->msg_name = &buf->s_in6; + mh->msg_namelen = sizeof(buf->s_in6); + mh->msg_iov = siov; + mh->msg_iovlen = 1; - udp6_l2_iov_sock[i].iov_base = udp6_l2_buf[i].data; - udp6_l2_iov_sock[i].iov_len = sizeof(udp6_l2_buf[i].data); - mh->msg_iov = &udp6_l2_iov_sock[i]; - mh->msg_iovlen = 1; - } + tiov->iov_base = tap_iov_base(c, &buf->taph); +} - for (i = 0; i < UDP_MAX_FRAMES; i++) { - struct iovec *iov = &udp6_l2_iov_tap[i]; +/** + * udp_sock_iov_init() - Initialise scatter-gather L2 buffers + * @c: Execution context + */ +static void udp_sock_iov_init(const struct ctx *c) +{ + size_t i; - iov->iov_base = tap_iov_base(c, &udp6_l2_buf[i].taph); + for (i = 0; i < UDP_MAX_FRAMES; i++) { + if (c->ifi4) + udp_sock4_iov_init_one(c, i); + if (c->ifi6) + udp_sock6_iov_init_one(c, i); } } @@ -1240,11 +1242,7 @@ v6: */ int udp_init(struct ctx *c) { - if (c->ifi4) - udp_sock4_iov_init(c); - - if (c->ifi6) - udp_sock6_iov_init(c); + udp_sock_iov_init(c); udp_invert_portmap(&c->udp.fwd_in); udp_invert_portmap(&c->udp.fwd_out); -- 2.44.0