From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by passt.top (Postfix, from userid 1000) id 7EEC65A0275; Thu, 03 Dec 2026 08:18:37 +0100 (CET) From: Stefano Brivio To: passt-dev@passt.top Subject: [PATCH] udp: Flush socket errors even after an intervening recvmmsg() clears the code Date: Thu, 3 Dec 2026 08:18:37 +0100 Message-ID: <20261203071837.3124886-1-sbrivio@redhat.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: Q6F535GNEQB7V6VBAHFQSHSVYZJ2QL62 X-Message-ID-Hash: Q6F535GNEQB7V6VBAHFQSHSVYZJ2QL62 X-MailFrom: sbrivio@passt.top 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: Gonzalo Rivas Valenzuela , 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: 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 Fixes: 04984578b00f ("udp: Deal with errors as we go in udp_sock_fwd()" Signed-off-by: Stefano Brivio --- 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