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 2/4] udp: Better factor IPv4 and IPv6 paths in udp_sock_handler()
Date: Thu, 24 Nov 2022 19:54:19 +1100	[thread overview]
Message-ID: <20221124085421.3027886-3-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20221124085421.3027886-1-david@gibson.dropbear.id.au>

Apart from which mh array they're operating on the recvmmsg() calls in
udp_sock_handler() are identical between the IPv4 and IPv6 paths, as are
some of the control structure updates.

By using some local variables to refer to the IP version specific control
arrays, make some more logic common between the IPv4 and IPv6 paths.  As
well as slightly reducing the code size, this makes it less likely that
we'll accidentally use the IPv4 arrays in the IPv6 path or vice versa as we
did in a recently fixed bug.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 udp.c | 40 ++++++++++++++++++----------------------
 1 file changed, 18 insertions(+), 22 deletions(-)

diff --git a/udp.c b/udp.c
index 99f9374..6a34e85 100644
--- a/udp.c
+++ b/udp.c
@@ -833,9 +833,10 @@ void udp_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t events,
 		      const struct timespec *now)
 {
 	ssize_t n, msg_len = 0, missing = 0;
+	struct mmsghdr *tap_mmh, *sock_mmh;
 	int msg_bufs = 0, msg_i = 0, ret;
-	struct mmsghdr *tap_mmh;
 	struct msghdr *last_mh;
+	struct iovec *tap_iov;
 	unsigned int i;
 
 	if (events == EPOLLERR)
@@ -847,34 +848,29 @@ void udp_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t events,
 	}
 
 	if (ref.r.p.udp.udp.v6) {
-		n = recvmmsg(ref.r.s, udp6_l2_mh_sock, UDP_TAP_FRAMES, 0, NULL);
-		if (n <= 0)
-			return;
-
-		udp6_l2_mh_tap[0].msg_hdr.msg_iov = &udp6_l2_iov_tap[0];
-
-		for (i = 0; i < (unsigned)n; i++) {
-			udp_sock_fill_data_v6(c, i, ref,
-					      &msg_i, &msg_bufs, &msg_len, now);
-		}
-
-		udp6_l2_mh_tap[msg_i].msg_hdr.msg_iovlen = msg_bufs;
 		tap_mmh = udp6_l2_mh_tap;
+		sock_mmh = udp6_l2_mh_sock;
+		tap_iov = udp6_l2_iov_tap;
 	} else {
-		n = recvmmsg(ref.r.s, udp4_l2_mh_sock, UDP_TAP_FRAMES, 0, NULL);
-		if (n <= 0)
-			return;
+		tap_mmh = udp4_l2_mh_tap;
+		sock_mmh = udp4_l2_mh_sock;
+		tap_iov = udp4_l2_iov_tap;
+	}
 
-		udp4_l2_mh_tap[0].msg_hdr.msg_iov = &udp4_l2_iov_tap[0];
+	n = recvmmsg(ref.r.s, sock_mmh, UDP_TAP_FRAMES, 0, NULL);
+	if (n <= 0)
+		return;
 
-		for (i = 0; i < (unsigned)n; i++) {
+	tap_mmh[0].msg_hdr.msg_iov = &tap_iov[0];
+	for (i = 0; i < (unsigned)n; i++) {
+		if (ref.r.p.udp.udp.v6)
+			udp_sock_fill_data_v6(c, i, ref,
+					      &msg_i, &msg_bufs, &msg_len, now);
+		else
 			udp_sock_fill_data_v4(c, i, ref,
 					      &msg_i, &msg_bufs, &msg_len, now);
-		}
-
-		udp4_l2_mh_tap[msg_i].msg_hdr.msg_iovlen = msg_bufs;
-		tap_mmh = udp4_l2_mh_tap;
 	}
+	tap_mmh[msg_i].msg_hdr.msg_iovlen = msg_bufs;
 
 	if (c->mode == MODE_PASTA)
 		return;
-- 
@@ -833,9 +833,10 @@ void udp_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t events,
 		      const struct timespec *now)
 {
 	ssize_t n, msg_len = 0, missing = 0;
+	struct mmsghdr *tap_mmh, *sock_mmh;
 	int msg_bufs = 0, msg_i = 0, ret;
-	struct mmsghdr *tap_mmh;
 	struct msghdr *last_mh;
+	struct iovec *tap_iov;
 	unsigned int i;
 
 	if (events == EPOLLERR)
@@ -847,34 +848,29 @@ void udp_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t events,
 	}
 
 	if (ref.r.p.udp.udp.v6) {
-		n = recvmmsg(ref.r.s, udp6_l2_mh_sock, UDP_TAP_FRAMES, 0, NULL);
-		if (n <= 0)
-			return;
-
-		udp6_l2_mh_tap[0].msg_hdr.msg_iov = &udp6_l2_iov_tap[0];
-
-		for (i = 0; i < (unsigned)n; i++) {
-			udp_sock_fill_data_v6(c, i, ref,
-					      &msg_i, &msg_bufs, &msg_len, now);
-		}
-
-		udp6_l2_mh_tap[msg_i].msg_hdr.msg_iovlen = msg_bufs;
 		tap_mmh = udp6_l2_mh_tap;
+		sock_mmh = udp6_l2_mh_sock;
+		tap_iov = udp6_l2_iov_tap;
 	} else {
-		n = recvmmsg(ref.r.s, udp4_l2_mh_sock, UDP_TAP_FRAMES, 0, NULL);
-		if (n <= 0)
-			return;
+		tap_mmh = udp4_l2_mh_tap;
+		sock_mmh = udp4_l2_mh_sock;
+		tap_iov = udp4_l2_iov_tap;
+	}
 
-		udp4_l2_mh_tap[0].msg_hdr.msg_iov = &udp4_l2_iov_tap[0];
+	n = recvmmsg(ref.r.s, sock_mmh, UDP_TAP_FRAMES, 0, NULL);
+	if (n <= 0)
+		return;
 
-		for (i = 0; i < (unsigned)n; i++) {
+	tap_mmh[0].msg_hdr.msg_iov = &tap_iov[0];
+	for (i = 0; i < (unsigned)n; i++) {
+		if (ref.r.p.udp.udp.v6)
+			udp_sock_fill_data_v6(c, i, ref,
+					      &msg_i, &msg_bufs, &msg_len, now);
+		else
 			udp_sock_fill_data_v4(c, i, ref,
 					      &msg_i, &msg_bufs, &msg_len, now);
-		}
-
-		udp4_l2_mh_tap[msg_i].msg_hdr.msg_iovlen = msg_bufs;
-		tap_mmh = udp4_l2_mh_tap;
 	}
+	tap_mmh[msg_i].msg_hdr.msg_iovlen = msg_bufs;
 
 	if (c->mode == MODE_PASTA)
 		return;
-- 
2.38.1


  parent reply	other threads:[~2022-11-24  8:54 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-24  8:54 [PATCH 0/4] udp: Fix some confusion of IPv4 and IPv6 control structures David Gibson
2022-11-24  8:54 ` [PATCH 1/4] udp: Fix inorrect use of IPv6 mh buffers in IPv4 path David Gibson
2022-11-24  8:54 ` David Gibson [this message]
2022-11-24  8:54 ` [PATCH 3/4] udp: Preadjust udp[46]_l2_iov_tap[].iov_base for pasta mode David Gibson
2022-11-24  8:54 ` [PATCH 4/4] udp: Factor out control structure management from udp_sock_fill_data_v[46] David Gibson
2022-11-25  2:01 ` [PATCH 0/4] udp: Fix some confusion of IPv4 and IPv6 control structures Stefano Brivio
2022-11-25  7:11   ` David Gibson
2022-12-06  6:47 ` 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=20221124085421.3027886-3-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).