Skip to contentSkip to conversation

selftests/bpf: allocate a larger timeout for connection

PATCH Applied Alexis Lothoré (eBPF Foundation) 3 participants 3 messages Aug 18 – Aug 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v7 1/1 selftests/bpf: allocate a larger timeout for connection

Some tests, like tc_tunnel or tc_edt, sporadically fail in CI with the following logs:

(network_helpers.c:309: errno: Operation now in progress) \
Failed to connect to server
send_and_test_data:FAIL:connect to server unexpected error: -115

This is due to SO_RCVTIMEO and SO_SNDTIMEO being set on the client socket (see settimeo() in client_socket()), allowing connect() to return an error and to set errno to EINPROGRESS instead of ETIMEDOUT. Increasing the timeout value for those tests is likely not a good solution (and it has already been done by commit 2790db208b44 ("selftests/bpf: Improve tc_tunnel test reliability")): some tests expect some data transfer to fail, and so the timeout value would increase overall test execution duration again (not only the connection, but any socket operation).

Another solution is to allocate a timeout budget specific to the
connection: we can apply a larger timeout only for connections, and once the connection is established, set back the timeout configured through opts->timeout_ms; this would allow connection to succeed under heavy CI load, while keeping timeout reasonable for the rest of the test traffic.

Create a dedicated connect helper that sets a large timeout for the connect step, tries to connect, and if successful, finally sets the timeout value to the one configured by the test.

Fixes 99126abec5e5 ("bpf: selftests: A few improvements to network_helpers.c")

---
Hello,
this is the v7 of the series aiming to reduce the flakyness of tc_tunnel/tc_edt tests in CI. This revision refactor a bit the new
connect timeout logic, and makes sure to apply the new logic to the
other connect() call in connect_fd_to_fd, as suggested by Daniel.
---
Changes in v7:
- Unwrap line in connect_fd_to_fd
- Link to v6: https://patch.msgid.link/[email protected]

Changes in v6:
- Factor settimeo/connect/settimeo in a dedicated helper
- Apply the same strategy to connect_fd_to_fd
- drop comment-style updated patch, applied separately by Daniel
- Link to v5: https://patch.msgid.link/[email protected]

Changes in v5:
- reformat multi-line comments
- collect Ihor's Acked-by
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- dropped the polling loop in favor of a larger, connect-specific timeout
- drop timeout configuration from tc_edt test
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- set errno before logging errors
- respect time budget set by
- respect opts->timeout_ms when polling: only poll for the remaining
time not already consume by connect()
- keep polling if poll returns with EINTR
- reorder early returns and add intermediate variables to clarify code
flow
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- drop unneeded initialization
- add back error message for immediate connection failure, and slightly
reword the async connection failure error message
- Link to v1: https://patch.msgid.link/[email protected]

To: Alexei Starovoitov <[email protected]>
To: Daniel Borkmann <[email protected]>
To: Andrii Nakryiko <[email protected]>
To: Eduard Zingerman <[email protected]>
To: Kumar Kartikeya Dwivedi <[email protected]>
To: Martin KaFai Lau <[email protected]>
To: Song Liu <[email protected]>
To: Yonghong Song <[email protected]>
To: Jiri Olsa <[email protected]>
To: Emil Tsalapatis <[email protected]>
To: Ihor Solodrai <[email protected]>
To: Shuah Khan <[email protected]>
Cc: [email protected]
Cc: Bastien Curutchet <[email protected]>
Cc: Thomas Petazzoni <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 tools/testing/selftests/bpf/network_helpers.c | 46 ++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +35 -11
index cdf2d7d3ab32..d27dc79475de 100644
@@ -49,6 +49,8 @@
 			errno = __save;					\
 })
 
+#define CONNECT_MIN_TIMEOUT_MS	5000
+
 struct ipv4_packet pkt_v4 = {
 	.eth.h_proto = __bpf_constant_htons(ETH_P_IP),
 	.iph.ihl = 5,
@@ -291,6 +293,37 @@ int client_socket(int family, int type,
 	return -1;
 }
 
+static int connect_with_timeout(int fd, const struct sockaddr_storage *addr,
+				socklen_t addrlen, int timeout_ms)
+{
+	int connect_timeout_ms = MAX(timeout_ms, CONNECT_MIN_TIMEOUT_MS);
+
+	/*
+	 * Override timeout configuration with a larger value for the
+	 * connection
+	 */
+	if (settimeo(fd, connect_timeout_ms)) {
+		log_err("Failed to set connect timeout");
+		return -1;
+	}
+
+	if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
+		log_err("Failed to connect");
+		return -1;
+	}
+
+	/*
+	 * If the timeout configured by the test is different from the
+	 * connect timeout, restore it
+	 */
+	if (timeout_ms != connect_timeout_ms && settimeo(fd, timeout_ms)) {
+		log_err("Failed to set timeout for connected socket");
+		return -1;
+	}
+
+	return 0;
+}
+
 int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
 		    const struct network_helper_opts *opts)
 {
@@ -305,8 +338,7 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
 		return -1;
 	}
 
-	if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
-		log_err("Failed to connect to server");
+	if (connect_with_timeout(fd, addr, addrlen, opts->timeout_ms)) {
 		save_errno_close(fd);
 		return -1;
 	}
@@ -376,20 +408,12 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms)
 	struct sockaddr_storage addr;
 	socklen_t len = sizeof(addr);
 
-	if (settimeo(client_fd, timeout_ms))
-		return -1;
-
 	if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) {
 		log_err("Failed to get server addr");
 		return -1;
 	}
 
-	if (connect(client_fd, (const struct sockaddr *)&addr, len)) {
-		log_err("Failed to connect to server");
-		return -1;
-	}
-
-	return 0;
+	return connect_with_timeout(client_fd, &addr, len, timeout_ms);
 }
 
 int make_sockaddr(int family, const char *addr_str, __u16 port,

---
base-commit: 530113d7299dab089435a6cd772c87fa4e0e40eb
change-id: 20260710-tc_tunnel_flaky-27e9a191bd03

Best regards,

Signature · 1 line
Alexis Lothoré (eBPF Foundation) <[email protected]>
#1 lore raw
Does the 5-second floor apply to callers that deliberately set a small timeout because they expect the connect to fail?
Quoted 9 lines
tools/testing/selftests/bpf/network_helpers.c +1 -0
index b82f572641b7..380b776f182d 100644
@@ -49,6 +49,8 @@
 			errno = __save;					\
 })
 
+#define CONNECT_MIN_TIMEOUT_MS	5000

[ ... ]

Quoted 12 lines
tools/testing/selftests/bpf/network_helpers.c +8 -0
@@ -291,6 +293,37 @@ int client_socket(int family, int type,
 	return -1;
 }
 
+static int connect_with_timeout(int fd, const struct sockaddr_storage *addr,
+				socklen_t addrlen, int timeout_ms)
+{
+	int connect_timeout_ms = MAX(timeout_ms, CONNECT_MIN_TIMEOUT_MS);
+
+	/*
+	 * Override timeout configuration with a larger value for the
+	 * connection

tools/testing/selftests/bpf/network_helpers.c int client_socket(int family, int type, · line 313

Quoted 10 lines
tools/testing/selftests/bpf/network_helpers.c +10 -0
+	 */
+	if (settimeo(fd, connect_timeout_ms)) {
+		log_err("Failed to set connect timeout");
+		return -1;
+	}
+
+	if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
+		log_err("Failed to connect");
+		return -1;
+	}

Does the 5-second floor apply to callers that deliberately set a small timeout because they expect the connect to fail?

Looking at cgroup_skb_sk_lookup.c, the test expects a connection to time out after 1 second:

  /* Client outside of test cgroup should fail to connect by timeout. */
  err = connect_fd_to_fd(out_sk, serv_sk, 1000);
  if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd", ...))

The call chain is:

test_cgroup_skb_sk_lookup()

    -> run_cgroup_bpf_test()
      -> run_lookup_test()
        -> connect_fd_to_fd(out_sk, serv_sk, 1000)
          -> connect_with_timeout(..., 1000)

With MAX(1000, 5000) returning 5000, settimeo(fd, 5000) is installed, and the deliberately-dropped connect() now blocks for 5 seconds instead of 1 second.

The test still passes because the cgroup_skb program drops the SYNs permanently, so at 5 seconds the socket is still in SYN_SENT and connect() returns EINPROGRESS. But the test now waits 5 times longer than intended.

The commit message mentions that some tests expect data transfer to fail and that increasing timeout would increase execution duration, which is what happens here for connects expected to time out.

Would it make sense to honour the caller's timeout_ms when explicitly set, and only apply the 5-second floor when timeout_ms is 0 or use a default?

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/32172718494

#2 lore raw
In reply to Alexis Lothoré (eBPF Foundation)
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Tue, 18 Aug 2026 20:43:00 +0200 you wrote:

Some tests, like tc_tunnel or tc_edt, sporadically fail in CI with the following logs:

(network_helpers.c:309: errno: Operation now in progress) \
Failed to connect to server
send_and_test_data:FAIL:connect to server unexpected error: -115

[...]

Here is the summary with links:
- [bpf,v7] selftests/bpf: allocate a larger timeout for connection

    https://git.kernel.org/bpf/bpf-next/c/669e4fa76600

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html