public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 1/6] udp: Refactor udp_sock[46]_iov_init()
Date: Wed,  6 Mar 2024 16:34:23 +1100	[thread overview]
Message-ID: <20240306053428.1176129-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240306053428.1176129-1-david@gibson.dropbear.id.au>

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 <david@gibson.dropbear.id.au>
---
 udp.c | 102 ++++++++++++++++++++++++++++------------------------------
 1 file changed, 50 insertions(+), 52 deletions(-)

diff --git a/udp.c b/udp.c
index 1f46afb2..df5f3c48 100644
--- a/udp.c
+++ b/udp.c
@@ -294,72 +294,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);
 	}
 }
 
@@ -1230,11 +1232,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);
-- 
@@ -294,72 +294,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);
 	}
 }
 
@@ -1230,11 +1232,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


  reply	other threads:[~2024-03-06  5:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-06  5:34 [PATCH v2 0/6] udp: Small cleanups David Gibson
2024-03-06  5:34 ` David Gibson [this message]
2024-03-06  5:34 ` [PATCH v2 2/6] udp: Consistent port variable names in udp_update_hdr[46] David Gibson
2024-03-06  5:34 ` [PATCH v2 3/6] udp: Pass data length explicitly to to udp_update_hdr[46] David Gibson
2024-03-06  5:34 ` [PATCH v2 4/6] udp: Re-order udp_update_hdr[46] for clarity and brevity David Gibson
2024-03-06  5:34 ` [PATCH v2 5/6] udp: Avoid unnecessary pointer in udp_update_hdr4() David Gibson
2024-03-06  5:34 ` [PATCH v2 6/6] udp: Use existing helper for UDP checksum on inbound IPv6 packets David Gibson
2024-03-13 14:21 ` [PATCH v2 0/6] udp: Small cleanups Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240306053428.1176129-2-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).