Skip to content

Commit 38e3651

Browse files
TropicaoMartin KaFai Lau
authored andcommitted
selftests/bpf: Systematically add SO_REUSEADDR in start_server_addr
Some tests have to stop/start a server multiple time with the same listening address. Doing so without SO_REUSADDR leads to failures due to the socket still being in TIME_WAIT right after the first instance stop/before the second instance start. Instead of letting each test manually set SO_REUSEADDR on their servers, it can be done automatically by start_server_addr for all tests (and without any major downside). Enforce SO_REUSEADDR in start_server_addr for all tests. Signed-off-by: Alexis Lothoré (eBPF Foundation) <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent b54a8e1 commit 38e3651

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

tools/testing/selftests/bpf/network_helpers.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int settimeo(int fd, int timeout_ms)
9797
int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
9898
const struct network_helper_opts *opts)
9999
{
100-
int fd;
100+
int on = 1, fd;
101101

102102
if (!opts)
103103
opts = &default_opts;
@@ -111,6 +111,12 @@ int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t a
111111
if (settimeo(fd, opts->timeout_ms))
112112
goto error_close;
113113

114+
if (type == SOCK_STREAM &&
115+
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
116+
log_err("Failed to enable SO_REUSEADDR");
117+
goto error_close;
118+
}
119+
114120
if (opts->post_socket_cb &&
115121
opts->post_socket_cb(fd, opts->cb_opts)) {
116122
log_err("Failed to call post_socket_cb");

0 commit comments

Comments
 (0)