From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202508 header.b=FsoPrIV5; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id C53DD5A026F for ; Fri, 03 Oct 2025 08:30:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202508; t=1759473055; bh=n9l1Lokcxx/uUqTCYRZtK1lwYQmoXETdhMeJLaCjDAM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FsoPrIV5os0EwjPwiRA9ToeDHo7Lr2wCP9woCHZFQcFGrwaRS17KMe9umfSXmV7JR EqUbf2pF/2ohHTWlayt4kdg9EMCiycnL+KNmCYmo/Aq7vC9Fn+JtAf11D4aVavS+3b GF5Jy4x0zLZaNralhnqypbpg6E92s2PxzZq9mgeIZ/SDEyPKpSbjSL8RcgpcwFKeon YIwGzMGrDCqFKm+zmB3yTulb4hU5M+5x/xheuN3VBHfskzxvpqtfwMYOdRQXNIB00i NtvXeIzgPdQZOug0VWVOKc3diWJF6ewzk8v4EbujMK+GvvOHBQvxK+NroSmrEZ1b2e UNTbVc+twg2pw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4cdJhH31lnz4wBj; Fri, 3 Oct 2025 16:30:55 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 1/1] tcp: Clarify logic calculating how much guest data to ack Date: Fri, 3 Oct 2025 16:30:51 +1000 Message-ID: <20251003063051.1127873-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251003063051.1127873-1-david@gibson.dropbear.id.au> References: <20251003063051.1127873-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: RUTHCLIJGA3WZEB7KPVW2XXM3WQ527PD X-Message-ID-Hash: RUTHCLIJGA3WZEB7KPVW2XXM3WQ527PD X-MailFrom: dgibson@gandalf.ozlabs.org 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: 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: This is fairly complex, because we have a method we prefer but we need to fall back to a simpler one in a bunch of cases. Slightly reorganise the code to make the flow clearer, and add a large comment giving the rationale. Signed-off-by: David Gibson --- tcp.c | 68 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/tcp.c b/tcp.c index 7da41797..85eb2c32 100644 --- a/tcp.c +++ b/tcp.c @@ -1014,35 +1014,51 @@ int tcp_update_seqack_wnd(const struct ctx *c, struct tcp_tap_conn *conn, uint32_t new_wnd_to_tap = prev_wnd_to_tap; int s = conn->sock; - if (!bytes_acked_cap) { - conn->seq_ack_to_tap = conn->seq_from_tap; - if (SEQ_LT(conn->seq_ack_to_tap, prev_ack_to_tap)) - conn->seq_ack_to_tap = prev_ack_to_tap; - } else { - if ((unsigned)SNDBUF_GET(conn) < SNDBUF_SMALL || - tcp_rtt_dst_low(conn) || CONN_IS_CLOSING(conn) || - (conn->flags & LOCAL) || force_seq) { - conn->seq_ack_to_tap = conn->seq_from_tap; - } else if (conn->seq_ack_to_tap != conn->seq_from_tap) { - if (!tinfo) { - tinfo = &tinfo_new; - if (getsockopt(s, SOL_TCP, TCP_INFO, tinfo, &sl)) - return 0; - } - - /* This trips a cppcheck bug in some versions, including - * cppcheck 2.18.3. - * https://sourceforge.net/p/cppcheck/discussion/general/thread/fecde59085/ - */ - /* cppcheck-suppress [uninitvar,unmatchedSuppression] */ - conn->seq_ack_to_tap = tinfo->tcpi_bytes_acked + - conn->seq_init_from_tap; - - if (SEQ_LT(conn->seq_ack_to_tap, prev_ack_to_tap)) - conn->seq_ack_to_tap = prev_ack_to_tap; + /* At this point we could ack all the data we've accepted for forwarding + * (seq_from_tap). When possible, however, we want to only ack what the + * peer has acked. This makes it appear to the guest more like a direct + * connection to the peer, and may improve flow control behaviour. + * + * For it to be possible and worth it we need: + * - The TCP_INFO Linux extension which gives us the peer acked bytes + * - Not to be told not to (force_seq) + * - Not half-closed in the peer->guest direction + * With no data coming from the peer, we won't get further events + * which would prompt us to recheck bytes_acked. We could poll on + * a timer, but that's more trouble than it's worth. + * - Not a host local connection + * Data goes directly from socket to socket in this case, with + * nothing meaningful "in flight". + * - Large enough send buffer + * If this is small, there's not enough in flight to bother. + */ + if (bytes_acked_cap && !force_seq && + !CONN_IS_CLOSING(conn) && + !(conn->flags & LOCAL) && !tcp_rtt_dst_low(conn) && + (unsigned)SNDBUF_GET(conn) >= SNDBUF_SMALL) { + if (!tinfo) { + tinfo = &tinfo_new; + if (getsockopt(s, SOL_TCP, TCP_INFO, tinfo, &sl)) + return 0; } + + /* This trips a cppcheck bug in some versions, including + * cppcheck 2.18.3. + * https://sourceforge.net/p/cppcheck/discussion/general/thread/fecde59085/ + */ + /* cppcheck-suppress [uninitvar,unmatchedSuppression] */ + conn->seq_ack_to_tap = tinfo->tcpi_bytes_acked + + conn->seq_init_from_tap; + } else { + /* Fall back to acking everything we have */ + conn->seq_ack_to_tap = conn->seq_from_tap; } + /* If the guest is retransmitting, don't let our ACKed sequence go + * backwards */ + if (SEQ_LT(conn->seq_ack_to_tap, prev_ack_to_tap)) + conn->seq_ack_to_tap = prev_ack_to_tap; + if (!snd_wnd_cap) { tcp_get_sndbuf(conn); new_wnd_to_tap = MIN(SNDBUF_GET(conn), MAX_WINDOW); -- 2.51.0