Skip to content

Commit c8054d0

Browse files
TropicaoKernel Patches Daemon
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]>
1 parent c95b89c commit c8054d0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

tools/testing/selftests/bpf/network_helpers.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ 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+
101+
int on = 1, fd;
101102

102103
if (!opts)
103104
opts = &default_opts;
@@ -111,6 +112,12 @@ int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t a
111112
if (settimeo(fd, opts->timeout_ms))
112113
goto error_close;
113114

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

0 commit comments

Comments
 (0)