Skip to contentSkip to conversation

selftest/bpf: make test_tc_tunnel and test_tc_edt more robust to CI load

PATCH Superseded Alexis Lothoré (eBPF Foundation) 3 participants 7 messages Aug 14 – Aug 17

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover selftest/bpf: make test_tc_tunnel and test_tc_edt more robust to CI load

Hello,
this is the v5 of the series aiming to reduce the flakyness of tc_tunnel/tc_edt tests in CI. This revision just fixes multi-line comments style, not only for the updated helper but for the whole network_helpers.c file

---
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]

---
Alexis Lothoré (eBPF Foundation) (2):
bpf/selftests: fix comment style in network_helpers.c selftests/bpf: allocate a larger timeout for connection

 tools/testing/selftests/bpf/network_helpers.c | 60 ++++++++++++++++++++++-----
 1 file changed, 49 insertions(+), 11 deletions(-)

---
base-commit: 114cb6aa1571c30770715a1756553b812865b1ba
change-id: 20260710-tc_tunnel_flaky-27e9a191bd03

Best regards,

Signature · 1 line
Alexis Lothoré (eBPF Foundation) <[email protected]>
#1 lore raw

PATCH v5 1/2 bpf/selftests: fix comment style in network_helpers.c

BPF subsystem requires multi-line comments to have the opening /* start on its own line.

Update multi-line comments in network_helpers.c to follow this requirement.

---
 tools/testing/selftests/bpf/network_helpers.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +16 -8
index db935a9d9fc1..cdf2d7d3ab32 100644
@@ -424,7 +424,8 @@ int make_sockaddr(int family, const char *addr_str, __u16 port,
 			*len = sizeof(*sin6);
 		return 0;
 	} else if (family == AF_UNIX) {
-		/* Note that we always use abstract unix sockets to avoid having
+		/*
+		 * Note that we always use abstract unix sockets to avoid having
 		 * to clean up leftover files.
 		 */
 		struct sockaddr_un *sun = (void *)addr;
@@ -865,7 +866,8 @@ static bool is_ethernet(const u_char *packet)
 	memcpy(&arphdr_type, packet + 8, 2);
 	arphdr_type = ntohs(arphdr_type);
 
-	/* Except the following cases, the protocol type contains the
+	/*
+	 * Except the following cases, the protocol type contains the
 	 * Ethernet protocol type for the packet.
 	 *
 	 * https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html
@@ -1033,19 +1035,22 @@ static void *traffic_monitor_thread(void *arg)
 		if (!packet)
 			continue;
 
-		/* According to the man page of pcap_dump(), first argument
+		/*
+		 * According to the man page of pcap_dump(), first argument
 		 * is the pcap_dumper_t pointer even it's argument type is
 		 * u_char *.
 		 */
 		pcap_dump((u_char *)dumper, &header, packet);
 
-		/* Not sure what other types of packets look like. Here, we
+		/*
+		 * Not sure what other types of packets look like. Here, we
 		 * parse only Ethernet and compatible packets.
 		 */
 		if (!is_ethernet(packet))
 			continue;
 
-		/* Skip SLL2 header
+		/*
+		 * Skip SLL2 header
 		 * https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html
 		 *
 		 * Although the document doesn't mention that, the payload
@@ -1079,7 +1084,8 @@ static void *traffic_monitor_thread(void *arg)
 	return NULL;
 }
 
-/* Prepare the pcap handle to capture packets.
+/*
+ * Prepare the pcap handle to capture packets.
  *
  * This pcap is non-blocking and immediate mode is enabled to receive
  * captured packets as soon as possible.  The snaplen is set to 1024 bytes
@@ -1150,7 +1156,8 @@ static void encode_test_name(char *buf, size_t len, const char *test_name, const
 
 #define PCAP_DIR "/tmp/tmon_pcap"
 
-/* Start to monitor the network traffic in the given network namespace.
+/*
+ * Start to monitor the network traffic in the given network namespace.
  *
  * netns: the name of the network namespace to monitor. If NULL, the
  *        current network namespace is monitored.
@@ -1255,7 +1262,8 @@ static void traffic_monitor_release(struct tmonitor_ctx *ctx)
 	free(ctx);
 }
 
-/* Stop the network traffic monitor.
+/*
+ * Stop the network traffic monitor.
  *
  * ctx: the context returned by traffic_monitor_start()
  */
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH bpf v5 1/2] bpf/selftests: fix comment style in network_helpers.c

This isn't a bug, but would 'selftests/bpf:' be the expected subject prefix here? The rest of the directory uses that convention (351 occurrences of 'selftests/bpf:' versus 0 of 'bpf/selftests:' in…
Quoted 9 lines

bpf/selftests: fix comment style in network_helpers.c

BPF subsystem requires multi-line comments to have the opening /* start on its own line.

Update multi-line comments in network_helpers.c to follow this requirement.

This isn't a bug, but would 'selftests/bpf:' be the expected subject prefix here? The rest of the directory uses that convention (351 occurrences of 'selftests/bpf:' versus 0 of 'bpf/selftests:' in the recent history), and the following patch in this series uses 'selftests/bpf:'.

---
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/31779510003

#3 lore raw

PATCH v5 2/2 selftests/bpf: allocate a larger timeout for connection

In reply to Alexis Lothoré (eBPF Foundation)

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.

Set a larger SO_SNDTIMEO/SO_RCVTIMEO for the connection step, and reset it back to the timeout configured by the test once the connection has succeeded.

Fixes 99126abec5e5 ("bpf: selftests: A few improvements to network_helpers.c")
---
 tools/testing/selftests/bpf/network_helpers.c | 36 ++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +33 -3
index cdf2d7d3ab32..8c9236c0a5a5 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,12 @@ int client_socket(int family, int type,
 	return -1;
 }
 
+static int connect_timeout_ms(const struct network_helper_opts *opts)
+{
+	/* Enforce a minimum connect timeout value */
+	return MAX(opts->timeout_ms, CONNECT_MIN_TIMEOUT_MS);
+}
+
 int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
 		    const struct network_helper_opts *opts)
 {
@@ -305,13 +313,35 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
 		return -1;
 	}
 
+	/*
+	 * Override timeout configuration with a larger value for the
+	 * connection
+	 */
+	if (settimeo(fd, connect_timeout_ms(opts))) {
+		log_err("Failed to set connect timeout");
+		goto close;
+	}
+
 	if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
-		log_err("Failed to connect to server");
-		save_errno_close(fd);
-		return -1;
+		log_err("Failed to connect");
+		goto close;
+	}
+
+	/*
+	 * If the timeout configured by the test is different from the
+	 * connect timeout, restore it
+	 */
+	if (opts->timeout_ms != CONNECT_MIN_TIMEOUT_MS &&
+	    settimeo(fd, opts->timeout_ms)) {
+		log_err("Failed to set timeout for connected socket");
+		goto close;
 	}
 
 	return fd;
+
+close:
+	save_errno_close(fd);
+	return -1;
 }
 
 int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH bpf v5 2/2] selftests/bpf: allocate a larger timeout for connection

Does this condition match what the comment describes? The value applied to the socket before connect() is connect_timeout_ms(opts), which returns MAX(opts->timeout_ms, CONNECT_MIN_TIMEOUT_MS). The…
Quoted 4 lines
tools/testing/selftests/bpf/network_helpers.c +0 -0
index d311db786880..ed4aeee1555d 100644

[ ... ]

Quoted 32 lines
tools/testing/selftests/bpf/network_helpers.c +17 -3
@@ -291,6 +293,12 @@ int client_socket(int family, int type,
 	return -1;
 }
 
+static int connect_timeout_ms(const struct network_helper_opts *opts)
+{
+	/* Enforce a minimum connect timeout value */
+	return MAX(opts->timeout_ms, CONNECT_MIN_TIMEOUT_MS);
+}
+
 int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
 		    const struct network_helper_opts *opts)
 {
@@ -305,13 +313,35 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
 		return -1;
 	}
 
+	/*
+	 * Override timeout configuration with a larger value for the
+	 * connection
+	 */
+	if (settimeo(fd, connect_timeout_ms(opts))) {
+		log_err("Failed to set connect timeout");
+		goto close;
+	}
+
 	if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
-		log_err("Failed to connect to server");
-		save_errno_close(fd);
-		return -1;
+		log_err("Failed to connect");
+		goto close;

tools/testing/selftests/bpf/network_helpers.c int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add · line 337

Quoted 10 lines
tools/testing/selftests/bpf/network_helpers.c +10 -0
+	}
+
+	/*
+	 * If the timeout configured by the test is different from the
+	 * connect timeout, restore it
+	 */
+	if (opts->timeout_ms != CONNECT_MIN_TIMEOUT_MS &&
+	    settimeo(fd, opts->timeout_ms)) {
+		log_err("Failed to set timeout for connected socket");
+		goto close;
                     ^^^^

Does this condition match what the comment describes? The value applied to the socket before connect() is connect_timeout_ms(opts), which returns MAX(opts->timeout_ms, CONNECT_MIN_TIMEOUT_MS). The guard tests opts->timeout_ms != CONNECT_MIN_TIMEOUT_MS, so the two expressions only match when opts->timeout_ms <= CONNECT_MIN_TIMEOUT_MS.

When a test passes a timeout larger than the minimum, the guard fires even though the socket already has the right value. For example, prog_tests/xdp_synproxy.c calls connect_to_fd(server_fd, 10000). connect_timeout_ms() returns 10000, settimeo() applies 10000, then the guard (10000 != 5000) is true and settimeo() re-applies the same 10000 value the socket already has.

Would opts->timeout_ms != connect_timeout_ms(opts) express the stated intent? That guard would be false when opts->timeout_ms == 10000 (nothing to restore), and true when opts->timeout_ms < CONNECT_MIN_TIMEOUT_MS (restore the smaller value the test asked for).

tools/testing/selftests/bpf/network_helpers.c int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add · line 345

Quoted 8 lines
tools/testing/selftests/bpf/network_helpers.c +6 -0
+	}
+
 	return fd;
+
+close:
+	save_errno_close(fd);
+	return -1;
 }

---
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/31779510003

maintainer
#5 lore raw

Re: [PATCH bpf v5 2/2] selftests/bpf: allocate a larger timeout for connection

In reply to Alexis Lothoré (eBPF Foundation)

Hi Alexis,

Quoted 80 lines · Alexis Lothoré (eBPF Foundation)

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.

Set a larger SO_SNDTIMEO/SO_RCVTIMEO for the connection step, and reset it back to the timeout configured by the test once the connection has succeeded.

Fixes 99126abec5e5 ("bpf: selftests: A few improvements to network_helpers.c")
---
  tools/testing/selftests/bpf/network_helpers.c | 36 ++++++++++++++++++++++++---
  1 file changed, 33 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +20 -3
index cdf2d7d3ab32..8c9236c0a5a5 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,12 @@ int client_socket(int family, int type,
  	return -1;
  }
 
+static int connect_timeout_ms(const struct network_helper_opts *opts)
+{
+	/* Enforce a minimum connect timeout value */
+	return MAX(opts->timeout_ms, CONNECT_MIN_TIMEOUT_MS);
+}
+
  int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
  		    const struct network_helper_opts *opts)
  {
@@ -305,13 +313,35 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
  		return -1;
  	}
 
+	/*
+	 * Override timeout configuration with a larger value for the
+	 * connection
+	 */
+	if (settimeo(fd, connect_timeout_ms(opts))) {
+		log_err("Failed to set connect timeout");
+		goto close;
+	}
+
  	if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
-		log_err("Failed to connect to server");
-		save_errno_close(fd);
-		return -1;
+		log_err("Failed to connect");
+		goto close;
+	}

tools/testing/selftests/bpf/network_helpers.c int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add · line 338

Quoted 10 lines
tools/testing/selftests/bpf/network_helpers.c +9 -0
+
+	/*
+	 * If the timeout configured by the test is different from the
+	 * connect timeout, restore it
+	 */
+	if (opts->timeout_ms != CONNECT_MIN_TIMEOUT_MS &&
+	    settimeo(fd, opts->timeout_ms)) {
+		log_err("Failed to set timeout for connected socket");
+		goto close;
  	}

Could you do one last spin? I'll get the comment fix applied, but the above still has issues as pointed out by sashiko, so maybe this could be reworked as follows (untested):

tools/testing/selftests/bpf/network_helpers.c +24 -38
index 8c9236c0a5a5..ec209a744889 100644
@@ -293,10 +293,27 @@ int client_socket(int family, int type,
 	return -1;
 }
 
-static int connect_timeout_ms(const struct network_helper_opts *opts)
+static int connect_with_timeout(int fd, const struct sockaddr_storage *addr,
+				socklen_t addrlen, int timeout_ms)
 {
-	/* Enforce a minimum connect timeout value */
-	return MAX(opts->timeout_ms, CONNECT_MIN_TIMEOUT_MS);
+	int connect_timeout_ms = MAX(timeout_ms, CONNECT_MIN_TIMEOUT_MS);
+
+	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 to server");
+		return -1;
+	}
+
+	if (connect_timeout_ms != timeout_ms && settimeo(fd, timeout_ms)) {
+		log_err("Failed to restore timeout for connected socket");
+		return -1;
+	}
+
+	return 0;
 }
 
 int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
@@ -313,35 +330,12 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
 		return -1;
 	}
 
-	/*
-	 * Override timeout configuration with a larger value for the
-	 * connection
-	 */
-	if (settimeo(fd, connect_timeout_ms(opts))) {
-		log_err("Failed to set connect timeout");
-		goto close;
-	}
-
-	if (connect(fd, (const struct sockaddr *)addr, addrlen)) {
-		log_err("Failed to connect");
-		goto close;
-	}
-
-	/*
-	 * If the timeout configured by the test is different from the
-	 * connect timeout, restore it
-	 */
-	if (opts->timeout_ms != CONNECT_MIN_TIMEOUT_MS &&
-	    settimeo(fd, opts->timeout_ms)) {
-		log_err("Failed to set timeout for connected socket");
-		goto close;
+	if (connect_with_timeout(fd, addr, addrlen, opts->timeout_ms)) {
+		save_errno_close(fd);
+		return -1;
 	}
 
 	return fd;
-
-close:
-	save_errno_close(fd);
-	return -1;
 }
 
 int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
@@ -406,20 +400,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,
#6 lore raw

Re: [PATCH bpf v5 2/2] selftests/bpf: allocate a larger timeout for connection

Hi Daniel,

Quoted 3 lines · Daniel Borkmann

Hi Alexis,

On 8/14/26 9:11 AM, Alexis Lothoré (eBPF Foundation) wrote:

[...]

Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+	/*
+	 * If the timeout configured by the test is different from the
+	 * connect timeout, restore it
+	 */
+	if (opts->timeout_ms != CONNECT_MIN_TIMEOUT_MS &&
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +3 -0
+	    settimeo(fd, opts->timeout_ms)) {
+		log_err("Failed to set timeout for connected socket");
+		goto close;
  	}

Could you do one last spin? I'll get the comment fix applied, but the above still has issues as pointed out by sashiko, so maybe this could be reworked as follows (untested):

Sure, I was planning to respin today with fixes to issues raised by the CI bot (wrong patch subject and mishandling of timeouts larger than the default connect timeout). I'll send it later today.

Thanks,

Alexis

Signature · 3 lines
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com