From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH 1/4] tcp: Fix ACK sequence on FIN to tap
Date: Tue, 7 Oct 2025 10:31:11 +1100 [thread overview]
Message-ID: <aORRP0hvz5MmzzcF@zatzit> (raw)
In-Reply-To: <20251007003219.3f286b1d@elisabeth>
[-- Attachment #1: Type: text/plain, Size: 7017 bytes --]
On Tue, Oct 07, 2025 at 12:32:19AM +0200, Stefano Brivio wrote:
> On Fri, 3 Oct 2025 13:19:17 +1000
> David Gibson <david@gibson.dropbear.id.au> wrote:
>
> > On Thu, Oct 02, 2025 at 01:58:41PM +0200, Stefano Brivio wrote:
> > > On Thu, 2 Oct 2025 12:41:08 +1000
> > > David Gibson <david@gibson.dropbear.id.au> wrote:
> > >
> > > > On Thu, Oct 02, 2025 at 02:06:43AM +0200, Stefano Brivio wrote:
> > > > > If we reach end-of-file on a socket (or get EPOLLRDHUP / EPOLLHUP) and
> > > > > send a FIN segment to the guest / container acknowledging a sequence
> > > > > number that's behind what we received so far, we won't have any
> > > > > further trigger to send an updated ACK segment, as we are now
> > > > > switching the epoll socket monitoring to edge-triggered mode.
> > > > >
> > > > > To avoid this situation, in tcp_update_seqack_wnd(), we set the next
> > > > > acknowledgement sequence to the current observed sequence, regardless
> > > > > of what was acknowledged socket-side.
> > > >
> > > > To double check my understanding: things should work if we always
> > > > acknowledged everything we've received. Acknowledging only what the
> > > > peer has acked is a refinement to give the guest a view that's closer
> > > > to what it would be end-to-end with the peer (which might improve the
> > > > operation of flow control).
> > >
> > > Right.
> > >
> > > > We can't use that refined mechanism when the socket is closing
> > > > (amongst other cases), because while we can get the peer acked bytes
> > > > from TCP_INFO, we can't get events when that changes, so we have no
> > > > mechanism to provide updates to the guest at the right time. So we
> > > > fall back to the simpler method.
> > > >
> > > > Is that correct?
> > >
> > > Also correct, yes. If you have a better idea to summarise this in the
> > > comment in tcp_buf_data_from_sock() let me know.
> >
> > Hm, I might. Or actually a way to reorganise the code that I think
> > will be a bit clearer and probably allow a clearer comment too.
>
> I would keep reworks for a later moment. Right now, it's already taking
> me long enough to find a moment to investigate these issues, write these
> fixes, and test them.
I mean... the change I'm proposing reduces lines of code (excepting
the big new comment), makes it easier to reason about and is localised
to the immediately surrounding code. But whatever, I don't
particularly care about the order we do things.
> > > Maybe I could mention
> > > EPOLLET explicitly there?
> >
> > I don't think EPOLLET is actually relevant. Even if we had level
> > triggered events, a change in bytes_acked doesn't count as an event
> > (AFAIK).
>
> It doesn't count, but with level-triggered events, we would be busy
> polling bytes_acked, as you noted. I was mentioning EPOLLET because it
> could be taken, intuitively, as a "stop listening for events" (almost)
> step. I'll leave that out then.
I mean, if busy polling were acceptable we could accomplish that
easily enough by doing it in tcp_defer_handler() regardless of
EPOLLET.
> > So either some other event is on, in which case we'd
> > effectively be busy polling bytes_acked, or it's not in which case we
> > don't get updates, just like now.
> >
> > I principle we could implement some sort of timer based polling, but
> > that sounds like way more trouble than it's worth.
>
> We already have something similar, based on ACK_INTERVAL and
> ACK_TO_TAP_DUE, and it shouldn't be overly complicated to extend that
> to a new FIN_TO_TAP_DUE flag. But indeed beyond the scope of this
> series.
Certainly.
> > > > > However, we don't necessarily call tcp_update_seqack_wnd() before
> > > > > sending the FIN segment, which might potentially lead to a situation,
> > > > > not observed in practice, where we unnecessarily cause a
> > > > > retransmission at some point after our FIN segment.
> > > > >
> > > > > Avoid that by setting the ACK sequence to whatever we received from
> > > > > the container / guest, before sending a FIN segment and switching to
> > > > > EPOLLET.
> > > > >
> > > > > Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
> > > >
> > > > Based on my understanding above, this looks correct to me, so,
> > > >
> > > > Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> > > >
> > > > My only concern is whether we could instead insert an extra call to
> > > > tcp_update_seqack_wnd() to reduce duplicated logic.
> > >
> > > Hmm, maybe, but on the other hand we're closing the connection. Should
> > > we really spend time querying TCP_INFO to recalculate the window at
> > > this point? I wouldn't.
> >
> > Good point. I mean tcp_update_seqack_wnd() could skip the TCP_INFO in
> > that case, but that does look a bit fiddly.
> >
> > On the other hand, in favour of not duplicating logic...
> >
> > [snip]
> > > > > @@ -368,7 +368,19 @@ int tcp_buf_data_from_sock(const struct ctx *c, struct tcp_tap_conn *conn)
> > > > > conn_flag(c, conn, STALLED);
> > > > > } else if ((conn->events & (SOCK_FIN_RCVD | TAP_FIN_SENT)) ==
> > > > > SOCK_FIN_RCVD) {
> > > > > - int ret = tcp_buf_send_flag(c, conn, FIN | ACK);
> > > > > + int ret;
> > > > > +
> > > > > + /* On TAP_FIN_SENT, we won't get further data events
> > > > > + * from the socket, and this might be the last ACK
> > > > > + * segment we send to the tap, so update its sequence to
> > > > > + * include everything we received until now.
> > > > > + *
> > > > > + * See also the special handling on CONN_IS_CLOSING() in
> > > > > + * tcp_update_seqack_wnd().
> > > > > + */
> > > > > + conn->seq_ack_to_tap = conn->seq_from_tap;
> >
> > ... the equivalent bits in tcp_update_seqack_wnd() have after them:
> > if (SEQ_LT(conn->seq_ack_to_tap, prev_ack_to_tap))
> > conn->seq_ack_to_tap = prev_ack_to_tap;
> >
> > Don't we need that here as well, in case the guest is retransmitting
> > when we get the sock side FIN?
>
> Not really, because we don't rewind conn->seq_from_tap, so we don't
> risk jumping back here.
Ah, because this one's on the sock->tap data path, whereas the other
calls are on the tap->sock data path. Good point.
> In tcp_update_seqack_wnd(), we might jump back (that should be double
> checked eventually, I'm not sure it's still the case) if we happened to
> acknowledge more than acknowledged socket-side while handling some
> particular condition, and then we switch back to acknowledging only
> bytes_acked.
>
> It should happen if the destination is/was a low RTT one, but we run
> out of slots in low_rtt_dst in favour of other entries. I don't
> remember any other case.
--
David Gibson (he or they) | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you, not the other way
| around.
http://www.ozlabs.org/~dgibson
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2025-10-06 23:34 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-02 0:06 [PATCH 0/4] tcp: Fix bad switch to CLOSE-WAIT state and surrounding issues Stefano Brivio
2025-10-02 0:06 ` [PATCH 1/4] tcp: Fix ACK sequence on FIN to tap Stefano Brivio
2025-10-02 2:41 ` David Gibson
2025-10-02 11:58 ` Stefano Brivio
2025-10-03 3:19 ` David Gibson
2025-10-06 22:32 ` Stefano Brivio
2025-10-06 23:31 ` David Gibson [this message]
2025-10-02 0:06 ` [PATCH 2/4] tcp: Completely ignore data segment in CLOSE-WAIT state, log a message Stefano Brivio
2025-10-02 2:44 ` David Gibson
2025-10-02 0:06 ` [PATCH 3/4] tcp: Don't consider FIN flags with mismatching sequence Stefano Brivio
2025-10-02 2:52 ` David Gibson
2025-10-02 3:02 ` David Gibson
2025-10-02 11:51 ` Stefano Brivio
2025-10-03 3:43 ` David Gibson
2025-10-06 22:32 ` Stefano Brivio
2025-10-06 23:34 ` David Gibson
2025-10-02 0:06 ` [PATCH 4/4] tcp: On partial send (incomplete sendmsg()), request a retransmission right away Stefano Brivio
2025-10-02 3:00 ` David Gibson
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=aORRP0hvz5MmzzcF@zatzit \
--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).