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=202512 header.b=gHTR7t6U; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 763A25A004E for ; Fri, 06 Feb 2026 01:43:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1770338621; bh=9nGvdPtH5ObB5lAs8TarPej6CvXXM5kCehNOLGa+mrs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=gHTR7t6Uejtz7B5WZIh4F68DpJoTwAvn94Sdnn+NPVz2TnqFYsUXeZsg0HgTEtOCb UXV8BFQfGsfE6JY6M8SKrHDwff7EO+GFwxzZRiLoeWo0/WZpvWyKH0i7hkx0MdfmSc RnnIFMfJhFDFzTr+IeanymXnBcpcKCx2Bhv64aFl4kxKgJ6pU5wUgyfpJvA1aN8SQA 7ZKAG5cgIT+X8rIuvt7TnhySNReGTg9uyRuu69zRsFx1+m7LZer9wDAizt7/j7pzLG A4Y4XS1Dkv1pyO5E5x+ySKS+56alXX5xDpoX2X3EO2/EqlL8bpSZrTwhqn8cdJhXM+ 7psyrZnlZwMGw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4f6b1S757rz4wpx; Fri, 06 Feb 2026 11:43:40 +1100 (AEDT) Date: Fri, 6 Feb 2026 10:43:34 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 2/4] tcp: Re-introduce inactivity timeouts based on a clock algorithm Message-ID: References: <20260204114137.2784090-1-david@gibson.dropbear.id.au> <20260204114137.2784090-3-david@gibson.dropbear.id.au> <20260205011720.337a75db@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="jsC9Uii7ZD9h3QnS" Content-Disposition: inline In-Reply-To: <20260205011720.337a75db@elisabeth> Message-ID-Hash: YIO2HGC2ESGZNPJVTOINGWIQRNMXD77C X-Message-ID-Hash: YIO2HGC2ESGZNPJVTOINGWIQRNMXD77C 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: 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: --jsC9Uii7ZD9h3QnS Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Feb 05, 2026 at 01:17:28AM +0100, Stefano Brivio wrote: > Nits only: >=20 > On Wed, 4 Feb 2026 21:41:35 +1000 > 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_INTERVAL > > (2 hours), a global timer marks every TCP connection as tentatively > > inactive. That flag is cleared if we get any events, either tap side or > > 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 | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- > > tcp.h | 4 +++- > > tcp_conn.h | 3 +++ > > 3 files changed, 54 insertions(+), 5 deletions(-) > >=20 > > diff --git a/tcp.c b/tcp.c > > index f8663369..acdac7df 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 s and >=20 > Probably easier to read: ... seconds >=20 > > + * 2*INACTIVITY_INTERVAL s after the last activity. >=20 > same here. Good idea, done. >=20 > > * > > * Summary of data flows (with ESTABLISHED event) > > * ---------------------------------------------- > > @@ -333,7 +340,8 @@ enum { > > =20 > > #define RTO_INIT 1 /* s, RFC 6298 */ > > #define RTO_INIT_AFTER_SYN_RETRIES 3 /* s, RFC 6298 */ > > -#define ACT_TIMEOUT 7200 > > + > > +#define INACTIVITY_INTERVAL 7200 /* s */ > > =20 > > #define LOW_RTT_TABLE_SIZE 8 > > #define LOW_RTT_THRESHOLD 10 /* us */ > > @@ -2254,6 +2262,8 @@ int tcp_tap_handler(const struct ctx *c, uint8_t = pif, sa_family_t af, > > return 1; > > } > > =20 > > + conn->inactive =3D false; > > + > > if (th->ack && !(conn->events & ESTABLISHED)) > > tcp_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, > > return; > > } > > =20 > > + conn->inactive =3D false; > > + > > if ((conn->events & TAP_FIN_ACKED) && (events & EPOLLHUP)) { > > conn_event(c, conn, CLOSED); > > return; > > @@ -2872,18 +2884,50 @@ int tcp_init(struct ctx *c) > > return 0; > > } > > =20 > > +/** > > + * tcp_inactivity() - Scan for and close long-inactive connections > > + * @: Execution context >=20 > * @c: Execution context > * @now: Current timestamp Oops, fixed. > > + */ > > +static void tcp_inactivity(struct ctx *c, const struct timespec *now) > > +{ > > + union flow *flow; > > + > > + if (now->tv_sec - c->tcp.inactivity_run < INACTIVITY_INTERVAL) > > + return; > > + > > + debug("TCP inactivity scan"); > > + c->tcp.inactivity_run =3D now->tv_sec; > > + > > + flow_foreach(flow) { > > + struct tcp_tap_conn *conn =3D &flow->tcp; > > + > > + if (flow->f.type !=3D FLOW_TCP) > > + continue; > > + > > + if (conn->inactive) { > > + /* No activity in this interval, reset */ > > + flow_dbg(conn, "Inactive for at least %us, resetting", > > + INACTIVITY_INTERVAL); > > + tcp_rst(c, conn); > > + } > > + > > + /* Ready to check fot next interval */ >=20 > for Wow, I really wasn't typing good that day. Fixed. >=20 > > + conn->inactive =3D true; > > + } > > +} > > + > > /** > > * tcp_timer() - Periodic tasks: port detection, closed connections, p= ool refill > > * @c: Execution context > > * @now: Current timestamp > > */ > > -void tcp_timer(const struct ctx *c, const struct timespec *now) > > +void tcp_timer(struct ctx *c, const struct timespec *now) > > { > > - (void)now; > > - > > tcp_sock_refill_init(c); > > if (c->mode =3D=3D MODE_PASTA) > > tcp_splice_refill(c); > > + > > + tcp_inactivity(c, now); > > } > > =20 > > /** > > diff --git a/tcp.h b/tcp.h > > index 24b90870..e104d453 100644 > > --- a/tcp.h > > +++ b/tcp.h > > @@ -21,7 +21,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif,= sa_family_t af, > > int tcp_listen(const struct ctx *c, uint8_t pif, unsigned rule, > > const union inany_addr *addr, const char *ifname, in_port_t po= rt); > > int tcp_init(struct ctx *c); > > -void tcp_timer(const struct ctx *c, const struct timespec *now); > > +void tcp_timer(struct ctx *c, const struct timespec *now); > > void tcp_defer_handler(struct ctx *c); > > =20 > > void tcp_update_l2_buf(const unsigned char *eth_d); > > @@ -38,6 +38,7 @@ extern bool peek_offset_cap; > > * @rto_max: Maximum retry timeout (in s) > > * @syn_retries: SYN retries using exponential backoff timeout > > * @syn_linear_timeouts: SYN retries before using exponential backoff = timeout > > + * @inactivity_run: Time we last scanned for inactive connections > > */ > > struct tcp_ctx { > > struct fwd_ports fwd_in; > > @@ -47,6 +48,7 @@ struct tcp_ctx { > > int rto_max; > > uint8_t syn_retries; > > uint8_t syn_linear_timeouts; > > + time_t inactivity_run; > > }; > > =20 > > #endif /* TCP_H */ > > diff --git a/tcp_conn.h b/tcp_conn.h > > index 21cea109..7197ff63 100644 > > --- a/tcp_conn.h > > +++ b/tcp_conn.h > > @@ -16,6 +16,7 @@ > > * @ws_from_tap: Window scaling factor advertised from tap/guest > > * @ws_to_tap: Window scaling factor advertised to tap/guest > > * @tap_mss: MSS advertised by tap/guest, rounded to 2 ^ TCP_MSS_BITS > > + * @inactive: No activity within the current INACTIVITY_INTERVAL > > * @sock: Socket descriptor number > > * @events: Connection events, implying connection states > > * @timer: timerfd descriptor for timeout events > > @@ -57,6 +58,8 @@ struct tcp_tap_conn { > > (conn->rtt_exp =3D MIN(RTT_EXP_MAX, ilog2(MAX(1, rtt / RTT_STORE_MIN)= ))) > > #define RTT_GET(conn) (RTT_STORE_MIN << conn->rtt_exp) > > =20 > > + bool inactive :1; > > + > > int sock :FD_REF_BITS; > > =20 > > uint8_t events; >=20 > --=20 > Stefano >=20 --=20 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 --jsC9Uii7ZD9h3QnS Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmmFORYACgkQzQJF27ox 2GcPUxAAopmP3YCQkOuGVwHeAWloxue4QlYqF7lz4mntVHkv9HXEJh/GYd4y3lWm osrrt8HcM/Loz+OYxlZpJf3L/I+l1Ucqm3Rid8NQq+rURg6omN84irN5X7phK6o2 oigeUQkaUNN8C5Qqt12EU8bfixkJZLf1sdlvcT3FaEu8bJANWYPv5/4aGdPphVTX Xow7P2D8lCClstWqyn/5UtVMNLIjSRwBpsRdeF4xcN1ShL7APMnYdgoDJfvVg8g3 n1PDJM+O4eiVlOyf3b/2AzwolerNpoaA0clHYdS8OTG2dBhySijDZIEk4cx0Tmmg jh9WOqZTx9ZK11UnpbSn5gPwO6giz+3Z9VGpI3nRDeNlBfUcNXrLLyyBpnGbi2wj TiDH7czJJsIcfqt9S2hUsX22zAd8mo9njYpdk6VQYEUqVACu4Ojggj/ocys6LhtL 8laqYXM7HrC3GoxLxqutXGr6EEE4cSMeuA7N2YhCjKyGqcMgwONPNt3bvkhbplgJ 7bGmVxodnvihogioCUFt96lwr5p91gxKR7sG52xRq2naIzof7ZUn0RW2J0LBI806 KojifZmUrYPCGeZzzCWWbOCrsZ0lbvO/afNNhVJrG4BqFH6nCtUX9ioqlFDVAXQW GXsTmy4aymwYxBX47iZ/aq2tRa1Q/ANXq330wZFb+4mu2Wqxixg= =lpwL -----END PGP SIGNATURE----- --jsC9Uii7ZD9h3QnS--