Skip to content

Commit 6f1f4c1

Browse files
author
Martin KaFai Lau
committed
Merge branch 'selftests-bpf-enfoce-so_reuseaddr-in-basic-test-servers'
Alexis Lothoré says: ==================== This small series is another follow-up to [1], in which I misunderstood Martin's initial feedback (see [2]). I proposed to make tc-tunnel apply SO_REUSEPORT once server is brought up. This series updates start_server_addr to really apply Martin's proposal after his clarification [3] [1] https://lore.kernel.org/bpf/20251031-tc_tunnel_improv-v1-0-0ffe44d27eda@bootlin.com/ [2] https://lore.kernel.org/bpf/efa3540a-1f52-46ca-9f49-e631a5e3e48c@linux.dev/ [3] https://lore.kernel.org/bpf/4cbabdf1-af2c-490a-a41a-b40c1539c1cb@linux.dev/ ==================== Link: https://patch.msgid.link/20251105-start-server-soreuseaddr-v1-0-1bbd9c1f8d65@bootlin.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
2 parents b54a8e1 + 5b7d6c9 commit 6f1f4c1

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
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");

tools/testing/selftests/bpf/prog_tests/test_tc_tunnel.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct subtest_cfg {
6969
int client_egress_prog_fd;
7070
int server_ingress_prog_fd;
7171
char extra_decap_mod_args[TUNNEL_ARGS_MAX_LEN];
72-
int *server_fd;
72+
int server_fd;
7373
};
7474

7575
struct connection {
@@ -135,26 +135,23 @@ static int run_server(struct subtest_cfg *cfg)
135135
{
136136
int family = cfg->ipproto == 6 ? AF_INET6 : AF_INET;
137137
struct nstoken *nstoken;
138+
struct network_helper_opts opts = {
139+
.timeout_ms = TIMEOUT_MS
140+
};
138141

139142
nstoken = open_netns(SERVER_NS);
140143
if (!ASSERT_OK_PTR(nstoken, "open server ns"))
141144
return -1;
142145

143-
cfg->server_fd = start_reuseport_server(family, SOCK_STREAM,
144-
cfg->server_addr, TEST_PORT,
145-
TIMEOUT_MS, 1);
146+
cfg->server_fd = start_server_str(family, SOCK_STREAM, cfg->server_addr,
147+
TEST_PORT, &opts);
146148
close_netns(nstoken);
147-
if (!ASSERT_OK_PTR(cfg->server_fd, "start server"))
149+
if (!ASSERT_OK_FD(cfg->server_fd, "start server"))
148150
return -1;
149151

150152
return 0;
151153
}
152154

153-
static void stop_server(struct subtest_cfg *cfg)
154-
{
155-
free_fds(cfg->server_fd, 1);
156-
}
157-
158155
static int check_server_rx_data(struct subtest_cfg *cfg,
159156
struct connection *conn, int len)
160157
{
@@ -188,7 +185,7 @@ static struct connection *connect_client_to_server(struct subtest_cfg *cfg)
188185
return NULL;
189186
}
190187

191-
server_fd = accept(*cfg->server_fd, NULL, NULL);
188+
server_fd = accept(cfg->server_fd, NULL, NULL);
192189
if (server_fd < 0) {
193190
close(client_fd);
194191
free(conn);
@@ -384,12 +381,13 @@ static int configure_ebpf_decapsulation(struct subtest_cfg *cfg)
384381

385382
static void run_test(struct subtest_cfg *cfg)
386383
{
387-
struct nstoken *nstoken = open_netns(CLIENT_NS);
384+
struct nstoken *nstoken;
388385

389-
if (!ASSERT_OK_PTR(nstoken, "open client ns"))
386+
if (!ASSERT_OK(run_server(cfg), "run server"))
390387
return;
391388

392-
if (!ASSERT_OK(run_server(cfg), "run server"))
389+
nstoken = open_netns(CLIENT_NS);
390+
if (!ASSERT_OK_PTR(nstoken, "open client ns"))
393391
goto fail;
394392

395393
/* Basic communication must work */
@@ -416,8 +414,8 @@ static void run_test(struct subtest_cfg *cfg)
416414
ASSERT_OK(send_and_test_data(cfg, true), "connect with encap and decap progs");
417415

418416
fail:
419-
stop_server(cfg);
420417
close_netns(nstoken);
418+
close(cfg->server_fd);
421419
}
422420

423421
static int setup(void)

0 commit comments

Comments
 (0)