public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 03/11] udp: Pass full epoll reference through more of sock handler path
Date: Fri,  5 Jul 2024 20:44:01 +1000	[thread overview]
Message-ID: <20240705104409.3847002-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240705104409.3847002-1-david@gibson.dropbear.id.au>

udp_buf_sock_handler() takes the epoll reference from the receiving socket,
and passes the UDP relevant part on to several other functions.  Future
changes are going to need several different epoll types for UDP, and to
pass that information through to some of those functions.  To avoid extra
noise in the patches making the real changes, change those functions now
to take the full epoll reference, rather than just the UDP part.

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

diff --git a/udp.c b/udp.c
index eadf4872..d38e0b7d 100644
--- a/udp.c
+++ b/udp.c
@@ -477,25 +477,26 @@ static int udp_splice_new_ns(void *arg)
 
 /**
  * udp_mmh_splice_port() - Is source address of message suitable for splicing?
- * @uref:	UDP epoll reference for incoming message's origin socket
+ * @ref:	epoll reference for incoming message's origin socket
  * @mmh:	mmsghdr of incoming message
  *
  * Return: if source address of message in @mmh refers to localhost (127.0.0.1
  *         or ::1) its source port (host order), otherwise -1.
  */
-static int udp_mmh_splice_port(union udp_epoll_ref uref,
-			       const struct mmsghdr *mmh)
+static int udp_mmh_splice_port(union epoll_ref ref, const struct mmsghdr *mmh)
 {
 	const struct sockaddr_in6 *sa6 = mmh->msg_hdr.msg_name;
 	const struct sockaddr_in *sa4 = mmh->msg_hdr.msg_name;
 
-	if (!uref.splice)
+	ASSERT(ref.type == EPOLL_TYPE_UDP);
+
+	if (!ref.udp.splice)
 		return -1;
 
-	if (uref.v6 && IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
+	if (ref.udp.v6 && IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
 		return ntohs(sa6->sin6_port);
 
-	if (!uref.v6 && IN4_IS_ADDR_LOOPBACK(&sa4->sin_addr))
+	if (!ref.udp.v6 && IN4_IS_ADDR_LOOPBACK(&sa4->sin_addr))
 		return ntohs(sa4->sin_port);
 
 	return -1;
@@ -507,7 +508,7 @@ static int udp_mmh_splice_port(union udp_epoll_ref uref,
  * @start:	Index of first datagram in udp[46]_l2_buf
  * @n:		Total number of datagrams in udp[46]_l2_buf pool
  * @dst:	Datagrams will be sent to this port (on destination side)
- * @uref:	UDP epoll reference for origin socket
+ * @ref:	epoll reference for origin socket
  * @now:	Timestamp
  *
  * This consumes as many datagrams as are sendable via a single socket.  It
@@ -518,7 +519,7 @@ static int udp_mmh_splice_port(union udp_epoll_ref uref,
  * Return: Number of datagrams forwarded
  */
 static unsigned udp_splice_send(const struct ctx *c, size_t start, size_t n,
-				in_port_t dst, union udp_epoll_ref uref,
+				in_port_t dst, union epoll_ref ref,
 				const struct timespec *now)
 {
 	in_port_t src = udp_meta[start].splicesrc;
@@ -527,8 +528,9 @@ static unsigned udp_splice_send(const struct ctx *c, size_t start, size_t n,
 	int s;
 
 	ASSERT(udp_meta[start].splicesrc >= 0);
+	ASSERT(ref.type == EPOLL_TYPE_UDP);
 
-	if (uref.v6) {
+	if (ref.udp.v6) {
 		mmh_recv = udp6_l2_mh_sock;
 		mmh_send = udp6_mh_splice;
 		udp6_localname.sin6_port = htons(dst);
@@ -544,27 +546,27 @@ static unsigned udp_splice_send(const struct ctx *c, size_t start, size_t n,
 		if (++i >= n)
 			break;
 
-		udp_meta[i].splicesrc = udp_mmh_splice_port(uref, &mmh_recv[i]);
+		udp_meta[i].splicesrc = udp_mmh_splice_port(ref, &mmh_recv[i]);
 	} while (udp_meta[i].splicesrc == src);
 
-	if (uref.pif == PIF_SPLICE) {
+	if (ref.udp.pif == PIF_SPLICE) {
 		src += c->udp.fwd_in.rdelta[src];
-		s = udp_splice_init[uref.v6][src].sock;
-		if (s < 0 && uref.orig)
-			s = udp_splice_new(c, uref.v6, src, false);
+		s = udp_splice_init[ref.udp.v6][src].sock;
+		if (s < 0 && ref.udp.orig)
+			s = udp_splice_new(c, ref.udp.v6, src, false);
 
 		if (s < 0)
 			goto out;
 
-		udp_splice_ns[uref.v6][dst].ts = now->tv_sec;
-		udp_splice_init[uref.v6][src].ts = now->tv_sec;
+		udp_splice_ns[ref.udp.v6][dst].ts = now->tv_sec;
+		udp_splice_init[ref.udp.v6][src].ts = now->tv_sec;
 	} else {
-		ASSERT(uref.pif == PIF_HOST);
+		ASSERT(ref.udp.pif == PIF_HOST);
 		src += c->udp.fwd_out.rdelta[src];
-		s = udp_splice_ns[uref.v6][src].sock;
-		if (s < 0 && uref.orig) {
+		s = udp_splice_ns[ref.udp.v6][src].sock;
+		if (s < 0 && ref.udp.orig) {
 			struct udp_splice_new_ns_arg arg = {
-				c, uref.v6, src, -1,
+				c, ref.udp.v6, src, -1,
 			};
 
 			NS_CALL(udp_splice_new_ns, &arg);
@@ -573,8 +575,8 @@ static unsigned udp_splice_send(const struct ctx *c, size_t start, size_t n,
 		if (s < 0)
 			goto out;
 
-		udp_splice_init[uref.v6][dst].ts = now->tv_sec;
-		udp_splice_ns[uref.v6][src].ts = now->tv_sec;
+		udp_splice_init[ref.udp.v6][dst].ts = now->tv_sec;
+		udp_splice_ns[ref.udp.v6][src].ts = now->tv_sec;
 	}
 
 	sendmmsg(s, mmh_send + start, i - start, MSG_NOSIGNAL);
@@ -716,7 +718,7 @@ static size_t udp_update_hdr6(const struct ctx *c,
  * @start:	Index of first datagram in udp[46]_l2_buf pool
  * @n:		Total number of datagrams in udp[46]_l2_buf pool
  * @dstport:	Destination port number on destination side
- * @uref:	UDP epoll reference for origin socket
+ * @ref:	epoll reference for origin socket
  * @now:	Current timestamp
  *
  * This consumes as many frames as are sendable via tap.  It requires that
@@ -726,7 +728,7 @@ static size_t udp_update_hdr6(const struct ctx *c,
  * Return: Number of frames sent via tap
  */
 static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n,
-			     in_port_t dstport, union udp_epoll_ref uref,
+			     in_port_t dstport, union epoll_ref ref,
 			     const struct timespec *now)
 {
 	struct iovec (*tap_iov)[UDP_NUM_IOVS];
@@ -734,8 +736,9 @@ static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n,
 	size_t i = start;
 
 	ASSERT(udp_meta[start].splicesrc == -1);
+	ASSERT(ref.type == EPOLL_TYPE_UDP);
 
-	if (uref.v6) {
+	if (ref.udp.v6) {
 		tap_iov = udp6_l2_iov_tap;
 		mmh_recv = udp6_l2_mh_sock;
 	} else {
@@ -748,7 +751,7 @@ static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n,
 		struct udp_meta_t *bm = &udp_meta[i];
 		size_t l4len;
 
-		if (uref.v6) {
+		if (ref.udp.v6) {
 			l4len = udp_update_hdr6(c, &bm->ip6h,
 						&bm->s_in.sa6, bp, dstport,
 						udp6_l2_mh_sock[i].msg_len, now);
@@ -766,7 +769,7 @@ static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n,
 		if (++i >= n)
 			break;
 
-		udp_meta[i].splicesrc = udp_mmh_splice_port(uref, &mmh_recv[i]);
+		udp_meta[i].splicesrc = udp_mmh_splice_port(ref, &mmh_recv[i]);
 	} while (udp_meta[i].splicesrc == -1);
 
 	tap_send_frames(c, &tap_iov[start][0], UDP_NUM_IOVS, i - start);
@@ -823,12 +826,12 @@ void udp_buf_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t eve
 	 * present).  So we fill in entry 0 before the loop, then udp_*_send()
 	 * populate one entry past where they consume.
 	 */
-	udp_meta[0].splicesrc = udp_mmh_splice_port(ref.udp, mmh_recv);
+	udp_meta[0].splicesrc = udp_mmh_splice_port(ref, mmh_recv);
 	for (i = 0; i < n; i += m) {
 		if (udp_meta[i].splicesrc >= 0)
-			m = udp_splice_send(c, i, n, dstport, ref.udp, now);
+			m = udp_splice_send(c, i, n, dstport, ref, now);
 		else
-			m = udp_tap_send(c, i, n, dstport, ref.udp, now);
+			m = udp_tap_send(c, i, n, dstport, ref, now);
 	}
 }
 
-- 
@@ -477,25 +477,26 @@ static int udp_splice_new_ns(void *arg)
 
 /**
  * udp_mmh_splice_port() - Is source address of message suitable for splicing?
- * @uref:	UDP epoll reference for incoming message's origin socket
+ * @ref:	epoll reference for incoming message's origin socket
  * @mmh:	mmsghdr of incoming message
  *
  * Return: if source address of message in @mmh refers to localhost (127.0.0.1
  *         or ::1) its source port (host order), otherwise -1.
  */
-static int udp_mmh_splice_port(union udp_epoll_ref uref,
-			       const struct mmsghdr *mmh)
+static int udp_mmh_splice_port(union epoll_ref ref, const struct mmsghdr *mmh)
 {
 	const struct sockaddr_in6 *sa6 = mmh->msg_hdr.msg_name;
 	const struct sockaddr_in *sa4 = mmh->msg_hdr.msg_name;
 
-	if (!uref.splice)
+	ASSERT(ref.type == EPOLL_TYPE_UDP);
+
+	if (!ref.udp.splice)
 		return -1;
 
-	if (uref.v6 && IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
+	if (ref.udp.v6 && IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
 		return ntohs(sa6->sin6_port);
 
-	if (!uref.v6 && IN4_IS_ADDR_LOOPBACK(&sa4->sin_addr))
+	if (!ref.udp.v6 && IN4_IS_ADDR_LOOPBACK(&sa4->sin_addr))
 		return ntohs(sa4->sin_port);
 
 	return -1;
@@ -507,7 +508,7 @@ static int udp_mmh_splice_port(union udp_epoll_ref uref,
  * @start:	Index of first datagram in udp[46]_l2_buf
  * @n:		Total number of datagrams in udp[46]_l2_buf pool
  * @dst:	Datagrams will be sent to this port (on destination side)
- * @uref:	UDP epoll reference for origin socket
+ * @ref:	epoll reference for origin socket
  * @now:	Timestamp
  *
  * This consumes as many datagrams as are sendable via a single socket.  It
@@ -518,7 +519,7 @@ static int udp_mmh_splice_port(union udp_epoll_ref uref,
  * Return: Number of datagrams forwarded
  */
 static unsigned udp_splice_send(const struct ctx *c, size_t start, size_t n,
-				in_port_t dst, union udp_epoll_ref uref,
+				in_port_t dst, union epoll_ref ref,
 				const struct timespec *now)
 {
 	in_port_t src = udp_meta[start].splicesrc;
@@ -527,8 +528,9 @@ static unsigned udp_splice_send(const struct ctx *c, size_t start, size_t n,
 	int s;
 
 	ASSERT(udp_meta[start].splicesrc >= 0);
+	ASSERT(ref.type == EPOLL_TYPE_UDP);
 
-	if (uref.v6) {
+	if (ref.udp.v6) {
 		mmh_recv = udp6_l2_mh_sock;
 		mmh_send = udp6_mh_splice;
 		udp6_localname.sin6_port = htons(dst);
@@ -544,27 +546,27 @@ static unsigned udp_splice_send(const struct ctx *c, size_t start, size_t n,
 		if (++i >= n)
 			break;
 
-		udp_meta[i].splicesrc = udp_mmh_splice_port(uref, &mmh_recv[i]);
+		udp_meta[i].splicesrc = udp_mmh_splice_port(ref, &mmh_recv[i]);
 	} while (udp_meta[i].splicesrc == src);
 
-	if (uref.pif == PIF_SPLICE) {
+	if (ref.udp.pif == PIF_SPLICE) {
 		src += c->udp.fwd_in.rdelta[src];
-		s = udp_splice_init[uref.v6][src].sock;
-		if (s < 0 && uref.orig)
-			s = udp_splice_new(c, uref.v6, src, false);
+		s = udp_splice_init[ref.udp.v6][src].sock;
+		if (s < 0 && ref.udp.orig)
+			s = udp_splice_new(c, ref.udp.v6, src, false);
 
 		if (s < 0)
 			goto out;
 
-		udp_splice_ns[uref.v6][dst].ts = now->tv_sec;
-		udp_splice_init[uref.v6][src].ts = now->tv_sec;
+		udp_splice_ns[ref.udp.v6][dst].ts = now->tv_sec;
+		udp_splice_init[ref.udp.v6][src].ts = now->tv_sec;
 	} else {
-		ASSERT(uref.pif == PIF_HOST);
+		ASSERT(ref.udp.pif == PIF_HOST);
 		src += c->udp.fwd_out.rdelta[src];
-		s = udp_splice_ns[uref.v6][src].sock;
-		if (s < 0 && uref.orig) {
+		s = udp_splice_ns[ref.udp.v6][src].sock;
+		if (s < 0 && ref.udp.orig) {
 			struct udp_splice_new_ns_arg arg = {
-				c, uref.v6, src, -1,
+				c, ref.udp.v6, src, -1,
 			};
 
 			NS_CALL(udp_splice_new_ns, &arg);
@@ -573,8 +575,8 @@ static unsigned udp_splice_send(const struct ctx *c, size_t start, size_t n,
 		if (s < 0)
 			goto out;
 
-		udp_splice_init[uref.v6][dst].ts = now->tv_sec;
-		udp_splice_ns[uref.v6][src].ts = now->tv_sec;
+		udp_splice_init[ref.udp.v6][dst].ts = now->tv_sec;
+		udp_splice_ns[ref.udp.v6][src].ts = now->tv_sec;
 	}
 
 	sendmmsg(s, mmh_send + start, i - start, MSG_NOSIGNAL);
@@ -716,7 +718,7 @@ static size_t udp_update_hdr6(const struct ctx *c,
  * @start:	Index of first datagram in udp[46]_l2_buf pool
  * @n:		Total number of datagrams in udp[46]_l2_buf pool
  * @dstport:	Destination port number on destination side
- * @uref:	UDP epoll reference for origin socket
+ * @ref:	epoll reference for origin socket
  * @now:	Current timestamp
  *
  * This consumes as many frames as are sendable via tap.  It requires that
@@ -726,7 +728,7 @@ static size_t udp_update_hdr6(const struct ctx *c,
  * Return: Number of frames sent via tap
  */
 static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n,
-			     in_port_t dstport, union udp_epoll_ref uref,
+			     in_port_t dstport, union epoll_ref ref,
 			     const struct timespec *now)
 {
 	struct iovec (*tap_iov)[UDP_NUM_IOVS];
@@ -734,8 +736,9 @@ static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n,
 	size_t i = start;
 
 	ASSERT(udp_meta[start].splicesrc == -1);
+	ASSERT(ref.type == EPOLL_TYPE_UDP);
 
-	if (uref.v6) {
+	if (ref.udp.v6) {
 		tap_iov = udp6_l2_iov_tap;
 		mmh_recv = udp6_l2_mh_sock;
 	} else {
@@ -748,7 +751,7 @@ static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n,
 		struct udp_meta_t *bm = &udp_meta[i];
 		size_t l4len;
 
-		if (uref.v6) {
+		if (ref.udp.v6) {
 			l4len = udp_update_hdr6(c, &bm->ip6h,
 						&bm->s_in.sa6, bp, dstport,
 						udp6_l2_mh_sock[i].msg_len, now);
@@ -766,7 +769,7 @@ static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n,
 		if (++i >= n)
 			break;
 
-		udp_meta[i].splicesrc = udp_mmh_splice_port(uref, &mmh_recv[i]);
+		udp_meta[i].splicesrc = udp_mmh_splice_port(ref, &mmh_recv[i]);
 	} while (udp_meta[i].splicesrc == -1);
 
 	tap_send_frames(c, &tap_iov[start][0], UDP_NUM_IOVS, i - start);
@@ -823,12 +826,12 @@ void udp_buf_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t eve
 	 * present).  So we fill in entry 0 before the loop, then udp_*_send()
 	 * populate one entry past where they consume.
 	 */
-	udp_meta[0].splicesrc = udp_mmh_splice_port(ref.udp, mmh_recv);
+	udp_meta[0].splicesrc = udp_mmh_splice_port(ref, mmh_recv);
 	for (i = 0; i < n; i += m) {
 		if (udp_meta[i].splicesrc >= 0)
-			m = udp_splice_send(c, i, n, dstport, ref.udp, now);
+			m = udp_splice_send(c, i, n, dstport, ref, now);
 		else
-			m = udp_tap_send(c, i, n, dstport, ref.udp, now);
+			m = udp_tap_send(c, i, n, dstport, ref, now);
 	}
 }
 
-- 
2.45.2


  parent reply	other threads:[~2024-07-05 10:44 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-05 10:43 [PATCH v2 00/11] Preliminaries for UDP flow support David Gibson
2024-07-05 10:43 ` [PATCH v2 01/11] util: sock_l4() determine protocol from epoll type rather than the reverse David Gibson
2024-07-05 10:44 ` [PATCH v2 02/11] flow: Add flow_sidx_valid() helper David Gibson
2024-07-05 10:44 ` David Gibson [this message]
2024-07-05 10:44 ` [PATCH v2 04/11] udp: Rename IOV and mmsghdr arrays David Gibson
2024-07-05 10:44 ` [PATCH v2 05/11] udp: Unify udp[46]_mh_splice David Gibson
2024-07-05 10:44 ` [PATCH v2 06/11] udp: Unify udp[46]_l2_iov David Gibson
2024-07-05 10:44 ` [PATCH v2 07/11] udp: Don't repeatedly initialise udp[46]_eth_hdr David Gibson
2024-07-05 10:44 ` [PATCH v2 08/11] udp: Move some more of sock_handler tasks into sub-functions David Gibson
2024-07-05 10:44 ` [PATCH v2 09/11] udp: Consolidate datagram batching David Gibson
2024-07-05 10:44 ` [PATCH v2 10/11] doc: Add program to document and test assumptions about SO_REUSEADDR David Gibson
2024-07-12 11:42   ` David Taylor
2024-07-15  0:43     ` David Gibson
2024-07-05 10:44 ` [PATCH v2 11/11] doc: Test behaviour of zero length datagram recv()s David Gibson
2024-07-05 16:38 ` [PATCH v2 00/11] Preliminaries for UDP flow support 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=20240705104409.3847002-4-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).