xref: /openbmc/linux/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1aaf376bdSAlexander Duyck // SPDX-License-Identifier: GPL-2.0
2aaf376bdSAlexander Duyck #include <test_progs.h>
3247f0ec3SAlexander Duyck #include <network_helpers.h>
4aaf376bdSAlexander Duyck 
5aaf376bdSAlexander Duyck #include "test_tcpbpf.h"
60a099d14SAlexander Duyck #include "test_tcpbpf_kern.skel.h"
7aaf376bdSAlexander Duyck 
8247f0ec3SAlexander Duyck #define LO_ADDR6 "::1"
9aaf376bdSAlexander Duyck #define CG_NAME "/tcpbpf-user-test"
10aaf376bdSAlexander Duyck 
verify_result(struct tcpbpf_globals * result)1121b5177eSAlexander Duyck static void verify_result(struct tcpbpf_globals *result)
12aaf376bdSAlexander Duyck {
13d3813ea1SAlexander Duyck 	__u32 expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) |
14aaf376bdSAlexander Duyck 				 (1 << BPF_SOCK_OPS_RWND_INIT) |
15aaf376bdSAlexander Duyck 				 (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) |
16aaf376bdSAlexander Duyck 				 (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) |
17aaf376bdSAlexander Duyck 				 (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) |
18aaf376bdSAlexander Duyck 				 (1 << BPF_SOCK_OPS_NEEDS_ECN) |
19aaf376bdSAlexander Duyck 				 (1 << BPF_SOCK_OPS_STATE_CB) |
20aaf376bdSAlexander Duyck 				 (1 << BPF_SOCK_OPS_TCP_LISTEN_CB));
21d3813ea1SAlexander Duyck 
22d3813ea1SAlexander Duyck 	/* check global map */
23*9d0b05bdSWang Yufen 	ASSERT_EQ(expected_events, result->event_map, "event_map");
24d3813ea1SAlexander Duyck 
2521b5177eSAlexander Duyck 	ASSERT_EQ(result->bytes_received, 501, "bytes_received");
2621b5177eSAlexander Duyck 	ASSERT_EQ(result->bytes_acked, 1002, "bytes_acked");
2721b5177eSAlexander Duyck 	ASSERT_EQ(result->data_segs_in, 1, "data_segs_in");
2821b5177eSAlexander Duyck 	ASSERT_EQ(result->data_segs_out, 1, "data_segs_out");
2921b5177eSAlexander Duyck 	ASSERT_EQ(result->bad_cb_test_rv, 0x80, "bad_cb_test_rv");
3021b5177eSAlexander Duyck 	ASSERT_EQ(result->good_cb_test_rv, 0, "good_cb_test_rv");
3121b5177eSAlexander Duyck 	ASSERT_EQ(result->num_listen, 1, "num_listen");
32247f0ec3SAlexander Duyck 
33247f0ec3SAlexander Duyck 	/* 3 comes from one listening socket + both ends of the connection */
3421b5177eSAlexander Duyck 	ASSERT_EQ(result->num_close_events, 3, "num_close_events");
35aaf376bdSAlexander Duyck 
36aaf376bdSAlexander Duyck 	/* check setsockopt for SAVE_SYN */
3721b5177eSAlexander Duyck 	ASSERT_EQ(result->tcp_save_syn, 0, "tcp_save_syn");
38d3813ea1SAlexander Duyck 
39aaf376bdSAlexander Duyck 	/* check getsockopt for SAVED_SYN */
4021b5177eSAlexander Duyck 	ASSERT_EQ(result->tcp_saved_syn, 1, "tcp_saved_syn");
4155144f31SPrankur gupta 
4255144f31SPrankur gupta 	/* check getsockopt for window_clamp */
4355144f31SPrankur gupta 	ASSERT_EQ(result->window_clamp_client, 9216, "window_clamp_client");
4455144f31SPrankur gupta 	ASSERT_EQ(result->window_clamp_server, 9216, "window_clamp_server");
45aaf376bdSAlexander Duyck }
46aaf376bdSAlexander Duyck 
run_test(struct tcpbpf_globals * result)4721b5177eSAlexander Duyck static void run_test(struct tcpbpf_globals *result)
48247f0ec3SAlexander Duyck {
49247f0ec3SAlexander Duyck 	int listen_fd = -1, cli_fd = -1, accept_fd = -1;
50247f0ec3SAlexander Duyck 	char buf[1000];
51247f0ec3SAlexander Duyck 	int err = -1;
52247f0ec3SAlexander Duyck 	int i, rv;
53247f0ec3SAlexander Duyck 
54247f0ec3SAlexander Duyck 	listen_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
55*9d0b05bdSWang Yufen 	if (!ASSERT_NEQ(listen_fd, -1, "start_server"))
56247f0ec3SAlexander Duyck 		goto done;
57247f0ec3SAlexander Duyck 
58247f0ec3SAlexander Duyck 	cli_fd = connect_to_fd(listen_fd, 0);
59*9d0b05bdSWang Yufen 	if (!ASSERT_NEQ(cli_fd, -1, "connect_to_fd(listen_fd)"))
60247f0ec3SAlexander Duyck 		goto done;
61247f0ec3SAlexander Duyck 
62247f0ec3SAlexander Duyck 	accept_fd = accept(listen_fd, NULL, NULL);
63*9d0b05bdSWang Yufen 	if (!ASSERT_NEQ(accept_fd, -1, "accept(listen_fd)"))
64247f0ec3SAlexander Duyck 		goto done;
65247f0ec3SAlexander Duyck 
66247f0ec3SAlexander Duyck 	/* Send 1000B of '+'s from cli_fd -> accept_fd */
67247f0ec3SAlexander Duyck 	for (i = 0; i < 1000; i++)
68247f0ec3SAlexander Duyck 		buf[i] = '+';
69247f0ec3SAlexander Duyck 
70247f0ec3SAlexander Duyck 	rv = send(cli_fd, buf, 1000, 0);
71*9d0b05bdSWang Yufen 	if (!ASSERT_EQ(rv, 1000, "send(cli_fd)"))
72247f0ec3SAlexander Duyck 		goto done;
73247f0ec3SAlexander Duyck 
74247f0ec3SAlexander Duyck 	rv = recv(accept_fd, buf, 1000, 0);
75*9d0b05bdSWang Yufen 	if (!ASSERT_EQ(rv, 1000, "recv(accept_fd)"))
76247f0ec3SAlexander Duyck 		goto done;
77247f0ec3SAlexander Duyck 
78247f0ec3SAlexander Duyck 	/* Send 500B of '.'s from accept_fd ->cli_fd */
79247f0ec3SAlexander Duyck 	for (i = 0; i < 500; i++)
80247f0ec3SAlexander Duyck 		buf[i] = '.';
81247f0ec3SAlexander Duyck 
82247f0ec3SAlexander Duyck 	rv = send(accept_fd, buf, 500, 0);
83*9d0b05bdSWang Yufen 	if (!ASSERT_EQ(rv, 500, "send(accept_fd)"))
84247f0ec3SAlexander Duyck 		goto done;
85247f0ec3SAlexander Duyck 
86247f0ec3SAlexander Duyck 	rv = recv(cli_fd, buf, 500, 0);
87*9d0b05bdSWang Yufen 	if (!ASSERT_EQ(rv, 500, "recv(cli_fd)"))
88247f0ec3SAlexander Duyck 		goto done;
89247f0ec3SAlexander Duyck 
90247f0ec3SAlexander Duyck 	/*
91247f0ec3SAlexander Duyck 	 * shutdown accept first to guarantee correct ordering for
92247f0ec3SAlexander Duyck 	 * bytes_received and bytes_acked when we go to verify the results.
93247f0ec3SAlexander Duyck 	 */
94247f0ec3SAlexander Duyck 	shutdown(accept_fd, SHUT_WR);
95247f0ec3SAlexander Duyck 	err = recv(cli_fd, buf, 1, 0);
96*9d0b05bdSWang Yufen 	if (!ASSERT_OK(err, "recv(cli_fd) for fin"))
97247f0ec3SAlexander Duyck 		goto done;
98247f0ec3SAlexander Duyck 
99247f0ec3SAlexander Duyck 	shutdown(cli_fd, SHUT_WR);
100247f0ec3SAlexander Duyck 	err = recv(accept_fd, buf, 1, 0);
101*9d0b05bdSWang Yufen 	ASSERT_OK(err, "recv(accept_fd) for fin");
102247f0ec3SAlexander Duyck done:
103247f0ec3SAlexander Duyck 	if (accept_fd != -1)
104247f0ec3SAlexander Duyck 		close(accept_fd);
105247f0ec3SAlexander Duyck 	if (cli_fd != -1)
106247f0ec3SAlexander Duyck 		close(cli_fd);
107247f0ec3SAlexander Duyck 	if (listen_fd != -1)
108247f0ec3SAlexander Duyck 		close(listen_fd);
109247f0ec3SAlexander Duyck 
110d3813ea1SAlexander Duyck 	if (!err)
11121b5177eSAlexander Duyck 		verify_result(result);
112247f0ec3SAlexander Duyck }
113247f0ec3SAlexander Duyck 
test_tcpbpf_user(void)114aaf376bdSAlexander Duyck void test_tcpbpf_user(void)
115aaf376bdSAlexander Duyck {
1160a099d14SAlexander Duyck 	struct test_tcpbpf_kern *skel;
117aaf376bdSAlexander Duyck 	int cg_fd = -1;
1180a099d14SAlexander Duyck 
1190a099d14SAlexander Duyck 	skel = test_tcpbpf_kern__open_and_load();
120*9d0b05bdSWang Yufen 	if (!ASSERT_OK_PTR(skel, "open and load skel"))
1210a099d14SAlexander Duyck 		return;
122aaf376bdSAlexander Duyck 
123aaf376bdSAlexander Duyck 	cg_fd = test__join_cgroup(CG_NAME);
124*9d0b05bdSWang Yufen 	if (!ASSERT_GE(cg_fd, 0, "test__join_cgroup(" CG_NAME ")"))
125aaf376bdSAlexander Duyck 		goto err;
126aaf376bdSAlexander Duyck 
1270a099d14SAlexander Duyck 	skel->links.bpf_testcb = bpf_program__attach_cgroup(skel->progs.bpf_testcb, cg_fd);
1280a099d14SAlexander Duyck 	if (!ASSERT_OK_PTR(skel->links.bpf_testcb, "attach_cgroup(bpf_testcb)"))
129aaf376bdSAlexander Duyck 		goto err;
130aaf376bdSAlexander Duyck 
13121b5177eSAlexander Duyck 	run_test(&skel->bss->global);
132aaf376bdSAlexander Duyck 
133aaf376bdSAlexander Duyck err:
134aaf376bdSAlexander Duyck 	if (cg_fd != -1)
135aaf376bdSAlexander Duyck 		close(cg_fd);
1360a099d14SAlexander Duyck 	test_tcpbpf_kern__destroy(skel);
137aaf376bdSAlexander Duyck }
138