Skip to content

Commit 1f4f80f

Browse files
fomichevAlexei Starovoitov
authored andcommitted
selftests/bpf: test_progs: convert test_tcp_rtt
Move the files, adjust includes, remove entry from Makefile & .gitignore Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent e3e02e1 commit 1f4f80f

File tree

3 files changed

+28
-59
lines changed

3 files changed

+28
-59
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,3 @@ libbpf.so.*
3939
test_hashmap
4040
test_btf_dump
4141
xdping
42-
test_tcp_rtt

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
2828
test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
2929
test_cgroup_storage test_select_reuseport test_section_names \
3030
test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
31-
test_btf_dump test_cgroup_attach xdping test_tcp_rtt
31+
test_btf_dump test_cgroup_attach xdping
3232

3333
BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c)))
3434
TEST_GEN_FILES = $(BPF_OBJ_FILES)
@@ -112,7 +112,6 @@ $(OUTPUT)/test_netcnt: cgroup_helpers.c
112112
$(OUTPUT)/test_sock_fields: cgroup_helpers.c
113113
$(OUTPUT)/test_sysctl: cgroup_helpers.c
114114
$(OUTPUT)/test_cgroup_attach: cgroup_helpers.c
115-
$(OUTPUT)/test_tcp_rtt: cgroup_helpers.c
116115

117116
.PHONY: force
118117

tools/testing/selftests/bpf/test_tcp_rtt.c renamed to tools/testing/selftests/bpf/prog_tests/tcp_rtt.c

Lines changed: 27 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
2-
#include <error.h>
3-
#include <errno.h>
4-
#include <stdio.h>
5-
#include <unistd.h>
6-
#include <sys/types.h>
7-
#include <sys/socket.h>
8-
#include <netinet/in.h>
9-
#include <netinet/tcp.h>
10-
#include <pthread.h>
11-
12-
#include <linux/filter.h>
13-
#include <bpf/bpf.h>
14-
#include <bpf/libbpf.h>
15-
16-
#include "bpf_rlimit.h"
17-
#include "bpf_util.h"
2+
#include <test_progs.h>
183
#include "cgroup_helpers.h"
194

20-
#define CG_PATH "/tcp_rtt"
21-
225
struct tcp_rtt_storage {
236
__u32 invoked;
247
__u32 dsack_dups;
@@ -31,8 +14,8 @@ static void send_byte(int fd)
3114
{
3215
char b = 0x55;
3316

34-
if (write(fd, &b, sizeof(b)) != 1)
35-
error(1, errno, "Failed to send single byte");
17+
if (CHECK_FAIL(write(fd, &b, sizeof(b)) != 1))
18+
perror("Failed to send single byte");
3619
}
3720

3821
static int wait_for_ack(int fd, int retries)
@@ -66,8 +49,10 @@ static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 invoked,
6649
int err = 0;
6750
struct tcp_rtt_storage val;
6851

69-
if (bpf_map_lookup_elem(map_fd, &client_fd, &val) < 0)
70-
error(1, errno, "Failed to read socket storage");
52+
if (CHECK_FAIL(bpf_map_lookup_elem(map_fd, &client_fd, &val) < 0)) {
53+
perror("Failed to read socket storage");
54+
return -1;
55+
}
7156

7257
if (val.invoked != invoked) {
7358
log_err("%s: unexpected bpf_tcp_sock.invoked %d != %d",
@@ -225,61 +210,47 @@ static void *server_thread(void *arg)
225210
int fd = *(int *)arg;
226211
int client_fd;
227212

228-
if (listen(fd, 1) < 0)
229-
error(1, errno, "Failed to listed on socket");
213+
if (CHECK_FAIL(listen(fd, 1)) < 0) {
214+
perror("Failed to listed on socket");
215+
return NULL;
216+
}
230217

231218
client_fd = accept(fd, (struct sockaddr *)&addr, &len);
232-
if (client_fd < 0)
233-
error(1, errno, "Failed to accept client");
219+
if (CHECK_FAIL(client_fd < 0)) {
220+
perror("Failed to accept client");
221+
return NULL;
222+
}
234223

235224
/* Wait for the next connection (that never arrives)
236225
* to keep this thread alive to prevent calling
237226
* close() on client_fd.
238227
*/
239-
if (accept(fd, (struct sockaddr *)&addr, &len) >= 0)
240-
error(1, errno, "Unexpected success in second accept");
228+
if (CHECK_FAIL(accept(fd, (struct sockaddr *)&addr, &len) >= 0)) {
229+
perror("Unexpected success in second accept");
230+
return NULL;
231+
}
241232

242233
close(client_fd);
243234

244235
return NULL;
245236
}
246237

247-
int main(int args, char **argv)
238+
void test_tcp_rtt(void)
248239
{
249240
int server_fd, cgroup_fd;
250-
int err = EXIT_SUCCESS;
251241
pthread_t tid;
252242

253-
if (setup_cgroup_environment())
254-
goto cleanup_obj;
255-
256-
cgroup_fd = create_and_get_cgroup(CG_PATH);
257-
if (cgroup_fd < 0)
258-
goto cleanup_cgroup_env;
259-
260-
if (join_cgroup(CG_PATH))
261-
goto cleanup_cgroup;
243+
cgroup_fd = test__join_cgroup("/tcp_rtt");
244+
if (CHECK_FAIL(cgroup_fd < 0))
245+
return;
262246

263247
server_fd = start_server();
264-
if (server_fd < 0) {
265-
err = EXIT_FAILURE;
266-
goto cleanup_cgroup;
267-
}
248+
if (CHECK_FAIL(server_fd < 0))
249+
goto close_cgroup_fd;
268250

269251
pthread_create(&tid, NULL, server_thread, (void *)&server_fd);
270-
271-
if (run_test(cgroup_fd, server_fd))
272-
err = EXIT_FAILURE;
273-
252+
CHECK_FAIL(run_test(cgroup_fd, server_fd));
274253
close(server_fd);
275-
276-
printf("test_sockopt_sk: %s\n",
277-
err == EXIT_SUCCESS ? "PASSED" : "FAILED");
278-
279-
cleanup_cgroup:
254+
close_cgroup_fd:
280255
close(cgroup_fd);
281-
cleanup_cgroup_env:
282-
cleanup_cgroup_environment();
283-
cleanup_obj:
284-
return err;
285256
}

0 commit comments

Comments
 (0)