* [PATCH v2] tcp: Enable SO_KEEPALIVE if we see keep-alive segments from container / guest
@ 2025-12-05 14:36 Stefano Brivio
2025-12-06 5:49 ` David Gibson
0 siblings, 1 reply; 2+ messages in thread
From: Stefano Brivio @ 2025-12-05 14:36 UTC (permalink / raw)
To: passt-dev; +Cc: David Gibson, Dominic Kohls
This is an approximation, as enabling SO_KEEPALIVE doesn't mean that
a keep-alive segment will be sent right away, rather that keep-alive
segments will start being sent if the connection is idle.
On the other hand, we don't have direct control over the host-side
TCP, so this is probably the best approximation we can get.
By default, namespaces inherit keep-alive parameters from their parent
namespace, so we can assume that, in case of a container, we'll wait
for the same interval it took for the container to start sending us
keep-alives, effectively doubling that interval.
To keep this simple, set SO_KEEPALIVE whenever we see a keep-alive
segment, instead of tracking its state. Keep-alive segments are
relatively infrequent, so we don't expect any substantial cost from
doing that.
Reported-by: Dominic Kohls <Dominic.Wenzler@rewe-group.com>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
v2: Coverity Scan just reminded me that we shouldn't ignore
setsockopt() return codes. It doesn't really matter if this fails
as we can't do much else in that case, but a trace-level message
is a good idea anyway (not that I've ever seen this failing).
tcp.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/tcp.c b/tcp.c
index 8357c0e..d6a5337 100644
--- a/tcp.c
+++ b/tcp.c
@@ -1838,6 +1838,10 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
tcp_send_flag(c, conn, ACK);
tcp_timer_ctl(c, conn);
+ if (setsockopt(conn->sock, SOL_SOCKET, SO_KEEPALIVE,
+ &((int){ 1 }), sizeof(int)))
+ flow_trace(conn, "failed to set SO_KEEPALIVE");
+
if (p->count == 1) {
tcp_tap_window_update(c, conn,
ntohs(th->window));
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] tcp: Enable SO_KEEPALIVE if we see keep-alive segments from container / guest
2025-12-05 14:36 [PATCH v2] tcp: Enable SO_KEEPALIVE if we see keep-alive segments from container / guest Stefano Brivio
@ 2025-12-06 5:49 ` David Gibson
0 siblings, 0 replies; 2+ messages in thread
From: David Gibson @ 2025-12-06 5:49 UTC (permalink / raw)
To: Stefano Brivio; +Cc: passt-dev, Dominic Kohls
[-- Attachment #1: Type: text/plain, Size: 2191 bytes --]
On Fri, Dec 05, 2025 at 03:36:23PM +0100, Stefano Brivio wrote:
> This is an approximation, as enabling SO_KEEPALIVE doesn't mean that
> a keep-alive segment will be sent right away, rather that keep-alive
> segments will start being sent if the connection is idle.
>
> On the other hand, we don't have direct control over the host-side
> TCP, so this is probably the best approximation we can get.
>
> By default, namespaces inherit keep-alive parameters from their parent
> namespace, so we can assume that, in case of a container, we'll wait
> for the same interval it took for the container to start sending us
> keep-alives, effectively doubling that interval.
>
> To keep this simple, set SO_KEEPALIVE whenever we see a keep-alive
> segment, instead of tracking its state. Keep-alive segments are
> relatively infrequent, so we don't expect any substantial cost from
> doing that.
>
> Reported-by: Dominic Kohls <Dominic.Wenzler@rewe-group.com>
> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
> v2: Coverity Scan just reminded me that we shouldn't ignore
> setsockopt() return codes. It doesn't really matter if this fails
> as we can't do much else in that case, but a trace-level message
> is a good idea anyway (not that I've ever seen this failing).
>
> tcp.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/tcp.c b/tcp.c
> index 8357c0e..d6a5337 100644
> --- a/tcp.c
> +++ b/tcp.c
> @@ -1838,6 +1838,10 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
> tcp_send_flag(c, conn, ACK);
> tcp_timer_ctl(c, conn);
>
> + if (setsockopt(conn->sock, SOL_SOCKET, SO_KEEPALIVE,
> + &((int){ 1 }), sizeof(int)))
> + flow_trace(conn, "failed to set SO_KEEPALIVE");
> +
> if (p->count == 1) {
> tcp_tap_window_update(c, conn,
> ntohs(th->window));
> --
> 2.43.0
>
--
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 --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-12-06 5:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-05 14:36 [PATCH v2] tcp: Enable SO_KEEPALIVE if we see keep-alive segments from container / guest Stefano Brivio
2025-12-06 5:49 ` David Gibson
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).