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=202602 header.b=Q8iT9QdX; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id EDA095A0275 for ; Mon, 02 Mar 2026 05:37:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1772426234; bh=7ZZKpKwb4JMfkjamISVM+boBDGrRSG31ssCJEMjUXdE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Q8iT9QdXhoO2bbuVQ/yteHGaZ9benSR8zdpXHr8ciIRWA8cBq19lc6c8l57oVqSgx HKhdbRPeXOnES04bgBfHJclXLlQyTsY8fcK5yQIqRYGqDuFWaX0XbHBvRZu0gPKvO+ PeYjN7wCrtPPsBUJTMHiUvPWgpcrXFGjYf/oFlSyZ3LYdY+FiDOVTRwYkilP/r/SYJ tnlqlMkza8na2LCIazuKFvORruZnW0kOAxy84AfJjKYs3kM9GYXIVxy40eXeA9FKpz ybcyVTJOys32kbQHLqGLzat1elNRuIwz9MjjN6eDf0lE20WULHIe8+in9d0QppCF8v lRQxzuJubc7XA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fPR3t2X8Lz4w8x; Mon, 02 Mar 2026 15:37:14 +1100 (AEDT) Date: Mon, 2 Mar 2026 15:37:10 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v2 2/4] tcp: Re-introduce inactivity timeouts based on a clock algorithm Message-ID: References: <20260206061739.33648-1-david@gibson.dropbear.id.au> <20260206061739.33648-3-david@gibson.dropbear.id.au> <20260225071541.04f63648@elisabeth> MIME-Version: 1.0 In-Reply-To: <20260225071541.04f63648@elisabeth> 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 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="dXRRZES075qb4gw7" Content-Disposition: inline Message-ID-Hash: 7ZWM6HJ7EIJGBELHNXEOSYRSSU2ZSLE6 X-Message-ID-Hash: 7ZWM6HJ7EIJGBELHNXEOSYRSSU2ZSLE6 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: passt-dev@passt.top 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: --dXRRZES075qb4gw7 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Feb 25, 2026 at 07:15:41AM +0100, Stefano Brivio wrote: > On Fri, 6 Feb 2026 17:17:37 +1100 > David Gibson wrote: >=20 > > We previously had a mechanism to remove TCP connections which were > > inactive for 2 hours. That was broken for a long time, due to poor > > interactions with the timerfd handling, so we removed it. > >=20 > > Adding this long scale timer onto the timerfd handling, which mostly > > handles much shorter timeouts is tricky to reason about. However, for = the > > inactivity timeouts, we don't require precision. Instead, we can use > > a 1-bit page replacement / "clock" algorithm. Every INACTIVITY_INTERVA= L > > (2 hours), a global timer marks every TCP connection as tentatively > > inactive. That flag is cleared if we get any events, either tap side o= r > > socket side. > >=20 > > If the inactive flag is still set when the next INACTIVITY_INTERVAL exp= ires > > then the connection has been inactive for an extended period and we res= et > > and close it. In practice this means that connections will be removed > > after 2-4 hours of inactivity. > >=20 > > This is not a true fix for bug 179, but it does mitigate the damage, by > > limiting the time that inactive connections will remain around, > >=20 > > Link: https://bugs.passt.top/show_bug.cgi?id=3D179 > > Signed-off-by: David Gibson > > --- > > tcp.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- > > tcp.h | 4 +++- > > tcp_conn.h | 3 +++ > > 3 files changed, 55 insertions(+), 5 deletions(-) > >=20 > > diff --git a/tcp.c b/tcp.c > > index f8663369..09929ee9 100644 > > --- a/tcp.c > > +++ b/tcp.c > > @@ -198,6 +198,13 @@ > > * TCP_INFO, with a representable range from RTT_STORE_MIN (100 us) = to > > * RTT_STORE_MAX (3276.8 ms). The timeout value is clamped according= ly. > > * > > + * We also use a global interval timer for an activity timeout which d= oesn't > > + * require precision: > > + * > > + * - INACTIVITY_INTERVAL: if a connection has had no activity for an e= ntire > > + * interval, close and reset it. This means that idle connections (= without > > + * keepalives) will be removed between INACTIVITY_INTERVAL seconds a= nd > > + * 2*INACTIVITY_INTERVAL seconds after the last activity. > > * > > * Summary of data flows (with ESTABLISHED event) > > * ---------------------------------------------- > > @@ -333,7 +340,8 @@ enum { > > =20 > > #define RTO_INIT=09=09=091=09=09/* s, RFC 6298 */ > > #define RTO_INIT_AFTER_SYN_RETRIES=093=09=09/* s, RFC 6298 */ > > -#define ACT_TIMEOUT=09=09=097200 > > + > > +#define INACTIVITY_INTERVAL=09=097200=09=09/* s */ > > =20 > > #define LOW_RTT_TABLE_SIZE=09=098 > > #define LOW_RTT_THRESHOLD=09=0910 /* us */ > > @@ -2254,6 +2262,8 @@ int tcp_tap_handler(const struct ctx *c, uint8_t = pif, sa_family_t af, > > =09=09return 1; > > =09} > > =20 > > +=09conn->inactive =3D false; > > + > > =09if (th->ack && !(conn->events & ESTABLISHED)) > > =09=09tcp_update_seqack_from_tap(c, conn, ntohl(th->ack_seq)); > > =20 > > @@ -2622,6 +2632,8 @@ void tcp_sock_handler(const struct ctx *c, union = epoll_ref ref, > > =09=09return; > > =09} > > =20 > > +=09conn->inactive =3D false; > > + > > =09if ((conn->events & TAP_FIN_ACKED) && (events & EPOLLHUP)) { > > =09=09conn_event(c, conn, CLOSED); > > =09=09return; > > @@ -2872,18 +2884,51 @@ int tcp_init(struct ctx *c) > > =09return 0; > > } > > =20 > > +/** > > + * tcp_inactivity() - Scan for and close long-inactive connections > > + * @c:=09=09Execution context > > + * @now: =09Current timestamp > > + */ > > +static void tcp_inactivity(struct ctx *c, const struct timespec *now) > > +{ > > +=09union flow *flow; > > + > > +=09if (now->tv_sec - c->tcp.inactivity_run < INACTIVITY_INTERVAL) > > +=09=09return; > > + > > +=09debug("TCP inactivity scan"); > > +=09c->tcp.inactivity_run =3D now->tv_sec; > > + > > +=09flow_foreach(flow) { >=20 > Nit: this could be flow_foreach_of_type((flow), FLOW_TCP), or, given Oops, I forgot I created that. > that it's the second usage of that, we could finally introduce a > foreach_tcp_flow() macro, and rebuild foreach_established_tcp_flow() on > top of that. I looked into making a foreach_tcp_flow() macro that used a struct tcp_conn * instead of a union flow *. I think it's possible but it was pretty fiddly, so I gave up. Given that, I'm more comfortable keeping flow_foreach_of_type(). Patch using it for these cases posted. > Using foreach_established_tcp_flow() should be equivalent here by the > way, because in all non-established cases we should have shorter > timeouts, but it looks unnecessarily fragile. Agreed. > Same for tcp_keepalive() from 4/4. Done. --=20 David Gibson (he or they)=09| I'll have my music baroque, and my code david AT gibson.dropbear.id.au=09| minimalist, thank you, not the other way =09=09=09=09| around. http://www.ozlabs.org/~dgibson --dXRRZES075qb4gw7 Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmmlE/UACgkQzQJF27ox 2GfzVRAAmk8XzGxg1QrXgR1eYhV8AWq8KLUmzxMmOAIpvqv4+UiWarUA2y2OBd+H uCMVqx7SnDa0xzW6Jp4ipCH1MM4q4eB9jGifNbPupdMX0pYftLhEFUMNbPSFzYAg o087erHp9+5/gGOlw2PyR2kKII8obOdsLJOI531twaGhpj5LxgNNnk2JfbTSlNNB 9VhAUxWsy9nSFtxQ9DC2LkD8rQQ1QKzfePQAjWFeV3f4S4c9slW6gL7clvE1pwWU yEvlmXlLv+yQ1Sv9DMs+9PFzcBtH7+QOIGCs1mALyyB7EELGNlfWQLj8KhTYjNYn MZb3Ihgpf9BpTfOA+8mx1TDC2MhAc/VXJPt8rdL9fJO1I1rjotMeZu3U1bVM7j1O gzuzwnUlVy0QE5FPkNanwiUW9z/AUU/ewMG0GwKSSISMeHdrNO0r4BLeoCApeIiq L07BQ3KJcrE1JKS7gDD/5uW9qPWcc9xFhJG8Vsjuf5Yt2xjbMbkDg3/Kxmfcshwo 4QWuG5lFb8IapK22FLgnCRcRU+uY73kDcshoLPTEdUxr/DTVcVAWaZWxVPNvNVID xqOhrMUeRFyb5umY81vZz0NE0OUfQySDGUXBk06HuSfZkksbzvkheGeyULrYpYzA TcwhU/8Eo8Su7jkYKrOC5HTFSpq6dv22e3hkoeWVsTK5TCs8wAs= =iR9W -----END PGP SIGNATURE----- --dXRRZES075qb4gw7--