On Thu, Mar 23, 2023 at 05:08:31PM +0100, Stefano Brivio wrote: I'm pretty sure this will make things better than they were, so in that sense: Reviewed-by: David Gibson <david(a)gibson.dropbear.id.au> However, I'm not entirely convinced by some of the reasoning below.Since commit cc6d8286d104 ("tcp: Reset ACK_FROM_TAP_DUE flag only as needed, update timer"), we don't clear ACK_FROM_TAP_DUE whenever we process an ACK segment, but, more correctly, only if we're really not waiting for a further ACK segment, that is, only if the acknowledged sequence matches what we sent. In the new function implementing this, tcp_update_seqack_from_tap(), we also reset the retransmission counter and store the updated ACK sequence. Both should be done iff forward progress is acknowledged, implied by the fact that the new ACK sequence is greater than the one we previously stored. At that point, it looked natural to also include the statements that clear and set the ACK_FROM_TAP_DUE flag inside the same conditional block: if we're not making forward progress, the need for an ACK, or lack thereof, should remain unchanged. There's a case where this isn't true, though: if a client initiatesMaybe mention this is a tap-side client specifically.a connection, and the server doesn't send data, with the client also not sending any further data except for what's possibly sent along with the first ACK segment following SYN, ACK from the server, we'llThis doesn't seem right. In my case the client *is* immediately sending data. It's *not* sending any in the ACK from the handshake (is that even allowed?). AFAICT it's just the fact that the (socket side) server doesn't send data which is relevant.never, in the established state of the connection, call tcp_update_seqack_from_tap() with reported forward progress. In that case, we'll never reset the initial ACK_FROM_TAP_DUE (used to trigger handshake timeouts), interpret the trigger as a the need for a retransmission (which won't actually retransmit anything), and eventually time out a perfectly healthy connection once we reach the maximum retransmission count. This is relatively simple to reproduce if we switch back to 30s iperf3 test runs, but it depends on the timing of the iperf3 client: sometimes the first ACK from the client (part of the three-way handshake) will come with data (and we'll hit the problem), sometimes data will be sent later (and we call to tcp_update_seqack_from_tap() from tcp_data_from_tap() later, avoiding the issue).This last bit seems wrong too. What I'm seeing is that tcp_update_seqack_from_tap() *is* called later from tcp_data_from_tap(), but it doesn't avoid the issue, because the from-client ack number hasn't advanced, so it doesn't do anything.A reliable reproducer is a simpler: $ strace -e accept,shutdown socat TCP-LISTEN:1111 STDIO & [2] 2202832 $ pasta --config-net -- sh -c '(sleep 30; echo a) | socat STDIN TCP:88.198.0.161:1111'I assume 88.198.0.161 is the gateway address here?accept(5, {sa_family=AF_INET, sin_port=htons(57200), sin_addr=inet_addr("127.0.0.1")}, [16]) = 6 shutdown(6, SHUT_RDWR) = 0 --- SIGTTOU {si_signo=SIGTTOU, si_code=SI_KERNEL} --- --- stopped by SIGTTOU --- 2023/03/23 16:05:06 socat[3] E write(5, 0x5645dbca9000, 2): Connection reset by peerThat reproduces a problem, but not exactly the one I'm seeing (see notes above). Mine can be reproduced with: $ strace -e accept,shutdown socat -u TCP-LISTEN:1111 OPEN:/dev/null,wronly & $ ./pasta --config-net -- socat -u OPEN:/dev/zero,rdonly TCP:192.168.17.1:1111 then waiting 30s.where the socat client connects, and no data exchange happens for 30s in either direction. Here, I'm using the default gateway address to reach the socat server on the host. Fix this by clearing and setting the ACK_FROM_TAP_DUE flag regardless of reported forward progress. If we clear it when it's already unset, conn_flag() will do nothing with it. If it was set, it's always fine to reschedule the timer (as long as we're waiting for a further ACK), because we just received an ACK segment, no matter its sequence.Hrm... is that actually true? Consider this situation: the server (socket side) sent some data that got lost, so the client (tap side) is not ever going to ack it. However, the client is continuing to send data, so we keep getting acks from it that don't make forward progress. Won't the change as proposed mean we then keep delaying the retransmit indefinitely? I've been thinking about this a bunch, and it's doing my head in a bit. However, I think the problem is that we have ACK_FROM_TAP_DUE set in the first place, when we don't actually have any outstanding sent data to ack. I think that's happening because we're not clearing it on the very first ACK from the client - the one in the handshake. That's because of the + 1 in: tcp_seq_init(c, conn, now); conn->seq_ack_from_tap = conn->seq_to_tap + 1; I think we have a subtle conflict of two reasonable seeming invariants here (written for the client on tap side case below, but there are variants for other cases, I think). A) We expect an ack iff seq_to_tap > seq_ack_from_tap According to this invariant, we want to remove the + 1 there. We advance seq_to_tap when we send the syn-ack, and ACK_FROM_TAP_DUE is set. seq_ack_from_tap catches up when we receive the handshake ack and ACK_FROM_TAP_DUE is cleared. B) (seq_to_tap - seq_ack_from_tap) is equal to the number of bytes in the socket buffer which have been sent to the client at least once This wants the + 1, because before the server sends data there's obviously nothing in the socket buffers, including during the handshake. But... I think the only place that relies on (B) is tcp_data_from_sock(), and I don't think it ever gets called with !ESTABLISHED. So.. I think we want to stick with invariant (A) and remove the "+ 1", for both the conn_from_sock and conn_from_tap variants.Fixes: cc6d8286d104 ("tcp: Reset ACK_FROM_TAP_DUE flag only as needed, update timer") Reported-by: David Gibson <david(a)gibson.dropbear.id.au> Analysed-by: David Gibson <david(a)gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio(a)redhat.com> --- tcp.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tcp.c b/tcp.c index f156287..b225bbe 100644 --- a/tcp.c +++ b/tcp.c @@ -1610,12 +1610,12 @@ out: static void tcp_update_seqack_from_tap(const struct ctx *c, struct tcp_tap_conn *conn, uint32_t seq) { - if (SEQ_GT(seq, conn->seq_ack_from_tap)) { - if (seq == conn->seq_to_tap) - conn_flag(c, conn, ~ACK_FROM_TAP_DUE); - else - conn_flag(c, conn, ACK_FROM_TAP_DUE); + if (seq == conn->seq_to_tap) + conn_flag(c, conn, ~ACK_FROM_TAP_DUE); + else + conn_flag(c, conn, ACK_FROM_TAP_DUE); + if (SEQ_GT(seq, conn->seq_ack_from_tap)) { conn->retrans = 0; conn->seq_ack_from_tap = seq; }-- David Gibson | 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