public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH] udp: Flush socket errors even after an intervening recvmmsg() clears the code
@ 2026-12-03  7:18 Stefano Brivio
  0 siblings, 0 replies; only message in thread
From: Stefano Brivio @ 2026-12-03  7:18 UTC (permalink / raw)
  To: passt-dev; +Cc: Gonzalo Rivas Valenzuela, David Gibson

Gonzalo reports that if a socket error occurs (for example, due to an
ICMP message) between udp_sock_errs() and a subsequent recvmmsg()
call, coming from udp_sock_recv(), the recvmmsg() call will clear the
socket error code, but not the queue.

At this point, once we're done handling the socket events, EPOLLERR is
still set, so udp_listen_sock_handler(), and, in turn, udp_sock_fwd(),
will be called again, but the recvmsg() call in udp_peek_addr() won't
report any actual error (rather EAGAIN).

As a result, we won't call udp_sock_errs() and leave the error in the
queue.  This leads to a event processing loop, until a further error
is reported.

This is a consequence of 04984578b00f ("udp: Deal with errors as we go
in udp_sock_fwd()"): earlier, udp_sock_errs() would be called on any
EPOLLERR event.  But now that we clear errors as we iterate through
udp_peek_addr() calls, we don't necessarily do that anymore.

This part is still desirable, so, instead of going back to an initial
out-of-order call to udp_sock_errs() on EPOLLERR, keep the processing
order as it is, but make sure that we'll call udp_sock_errs() at least
once per event handling cycle, whenever EPOLLERR was set on the
socket.

This way, if the error code vanished because of an intervening
recvmmsg() call, we will still be able to clear queued errors.

Link: https://bugs.passt.top/show_bug.cgi?id=228
Reported-by: Gonzalo Rivas Valenzuela <grv@jessica.cl>
Fixes: 04984578b00f ("udp: Deal with errors as we go in udp_sock_fwd()"
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
 udp.c          | 28 +++++++++++++++++++---------
 udp_flow.c     |  2 +-
 udp_internal.h |  2 +-
 3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/udp.c b/udp.c
index 7966079..2b0b41f 100644
--- a/udp.c
+++ b/udp.c
@@ -705,13 +705,13 @@ static int udp_sock_errs(const struct ctx *c, int s, flow_sidx_t sidx,
 		n_err += rc;
 
 	if (rc < 0)
-		return -1; /* error reading error, unrecoverable */
+		goto fail; /* error reading error, unrecoverable */
 
 	errlen = sizeof(err);
 	if (getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0 ||
 	    errlen != sizeof(err)) {
 		flow_perror_ratelimit(uflow, now, "Error reading SO_ERROR");
-		return -1;  /* error reading error, unrecoverable */
+		goto fail; /* error reading error, unrecoverable */
 	}
 
 	if (err) {
@@ -724,10 +724,16 @@ static int udp_sock_errs(const struct ctx *c, int s, flow_sidx_t sidx,
 		/* EPOLLERR, but no errors to clear !? */
 		flow_err_ratelimit(uflow, now,
 				   "EPOLLERR event without reported errors");
-		return -1; /* no way to clear, unrecoverable */
+		goto fail; /* no way to clear, unrecoverable */
 	}
 
 	return n_err;
+
+fail:
+	err_ratelimit(now,
+"UDP: Unrecoverable error on listening socket: (%s port %hu)",
+		      pif_name(pif), port);
+	return -1;
 }
 
 /**
@@ -856,12 +862,13 @@ static void udp_buf_sock_to_tap(const struct ctx *c, int s, int n,
  * udp_sock_fwd() - Forward datagrams from a possibly unconnected socket
  * @c:		Execution context
  * @s:		Socket to forward from
+ * @errors:	There are socket errors (EPOLLERR) to be flushed
  * @rule_hint:	Forwarding rule to use, or -1 if unknown
  * @frompif:	Interface to which @s belongs
  * @port:	Our (local) port number of @s
  * @now:	Current timestamp
  */
-void udp_sock_fwd(const struct ctx *c, int s, int rule_hint,
+void udp_sock_fwd(const struct ctx *c, int s, bool errors, int rule_hint,
 		  uint8_t frompif, in_port_t port, const struct timespec *now)
 {
 	union sockaddr_inany src;
@@ -876,14 +883,14 @@ void udp_sock_fwd(const struct ctx *c, int s, int rule_hint,
 		if (rc < 0) {
 			trace("Error peeking at socket address: %s",
 			      strerror_(-rc));
+
 			/* Clear errors & carry on */
 			if (udp_sock_errs(c, s, FLOW_SIDX_NONE,
 					  frompif, port, now) < 0) {
-				err_ratelimit(now,
-"UDP: Unrecoverable error on listening socket: (%s port %hu)",
-				    pif_name(frompif), port);
-				/* FIXME: what now?  close/re-open socket? */
+				; /* FIXME: what now?  close/re-open socket? */
 			}
+			errors = false;
+
 			continue;
 		}
 
@@ -918,6 +925,9 @@ void udp_sock_fwd(const struct ctx *c, int s, int rule_hint,
 				debug_perror("Failed to discard datagram");
 		}
 	}
+
+	if (errors)
+		udp_sock_errs(c, s, FLOW_SIDX_NONE, frompif, port, now);
 }
 
 /**
@@ -932,7 +942,7 @@ void udp_listen_sock_handler(const struct ctx *c,
 			     const struct timespec *now)
 {
 	if (events & (EPOLLERR | EPOLLIN)) {
-		udp_sock_fwd(c, ref.fd, ref.listen.rule,
+		udp_sock_fwd(c, ref.fd, events & EPOLLERR, ref.listen.rule,
 			     ref.listen.pif, ref.listen.port, now);
 	}
 }
diff --git a/udp_flow.c b/udp_flow.c
index 57e159f..0e78fa6 100644
--- a/udp_flow.c
+++ b/udp_flow.c
@@ -393,7 +393,7 @@ static void udp_flush_flow(const struct ctx *c,
 {
 	/* We don't know exactly where the datagrams will come from, but we know
 	 * they'll have an interface and oport matching this flow */
-	udp_sock_fwd(c, uflow->s[sidei], -1, uflow->f.pif[sidei],
+	udp_sock_fwd(c, uflow->s[sidei], false, -1, uflow->f.pif[sidei],
 		     uflow->f.side[sidei].oport, now);
 }
 
diff --git a/udp_internal.h b/udp_internal.h
index 361cc74..55dae38 100644
--- a/udp_internal.h
+++ b/udp_internal.h
@@ -33,7 +33,7 @@ size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udphdr *uh,
 		       struct iov_tail *payload,
 		       const struct flowside *toside, size_t dlen,
 		       bool no_udp_csum);
-void udp_sock_fwd(const struct ctx *c, int s, int rule_hint,
+void udp_sock_fwd(const struct ctx *c, int s, bool errors, int rule_hint,
 		  uint8_t frompif, in_port_t port, const struct timespec *now);
 
 #endif /* UDP_INTERNAL_H */
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-03  7:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03  7:18 [PATCH] udp: Flush socket errors even after an intervening recvmmsg() clears the code Stefano Brivio

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).