xref: /linux/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c (revision a1c3be890440a1769ed6f822376a3e3ab0d42994)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
5 #include "test_tcpbpf.h"
6 #include "test_tcpbpf_kern.skel.h"
7 
8 #define LO_ADDR6 "::1"
9 #define CG_NAME "/tcpbpf-user-test"
10 
11 static __u32 duration;
12 
13 static void verify_result(struct tcpbpf_globals *result)
14 {
15 	__u32 expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) |
16 				 (1 << BPF_SOCK_OPS_RWND_INIT) |
17 				 (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) |
18 				 (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) |
19 				 (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) |
20 				 (1 << BPF_SOCK_OPS_NEEDS_ECN) |
21 				 (1 << BPF_SOCK_OPS_STATE_CB) |
22 				 (1 << BPF_SOCK_OPS_TCP_LISTEN_CB));
23 
24 	/* check global map */
25 	CHECK(expected_events != result->event_map, "event_map",
26 	      "unexpected event_map: actual 0x%08x != expected 0x%08x\n",
27 	      result->event_map, expected_events);
28 
29 	ASSERT_EQ(result->bytes_received, 501, "bytes_received");
30 	ASSERT_EQ(result->bytes_acked, 1002, "bytes_acked");
31 	ASSERT_EQ(result->data_segs_in, 1, "data_segs_in");
32 	ASSERT_EQ(result->data_segs_out, 1, "data_segs_out");
33 	ASSERT_EQ(result->bad_cb_test_rv, 0x80, "bad_cb_test_rv");
34 	ASSERT_EQ(result->good_cb_test_rv, 0, "good_cb_test_rv");
35 	ASSERT_EQ(result->num_listen, 1, "num_listen");
36 
37 	/* 3 comes from one listening socket + both ends of the connection */
38 	ASSERT_EQ(result->num_close_events, 3, "num_close_events");
39 
40 	/* check setsockopt for SAVE_SYN */
41 	ASSERT_EQ(result->tcp_save_syn, 0, "tcp_save_syn");
42 
43 	/* check getsockopt for SAVED_SYN */
44 	ASSERT_EQ(result->tcp_saved_syn, 1, "tcp_saved_syn");
45 
46 	/* check getsockopt for window_clamp */
47 	ASSERT_EQ(result->window_clamp_client, 9216, "window_clamp_client");
48 	ASSERT_EQ(result->window_clamp_server, 9216, "window_clamp_server");
49 }
50 
51 static void run_test(struct tcpbpf_globals *result)
52 {
53 	int listen_fd = -1, cli_fd = -1, accept_fd = -1;
54 	char buf[1000];
55 	int err = -1;
56 	int i, rv;
57 
58 	listen_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
59 	if (CHECK(listen_fd == -1, "start_server", "listen_fd:%d errno:%d\n",
60 		  listen_fd, errno))
61 		goto done;
62 
63 	cli_fd = connect_to_fd(listen_fd, 0);
64 	if (CHECK(cli_fd == -1, "connect_to_fd(listen_fd)",
65 		  "cli_fd:%d errno:%d\n", cli_fd, errno))
66 		goto done;
67 
68 	accept_fd = accept(listen_fd, NULL, NULL);
69 	if (CHECK(accept_fd == -1, "accept(listen_fd)",
70 		  "accept_fd:%d errno:%d\n", accept_fd, errno))
71 		goto done;
72 
73 	/* Send 1000B of '+'s from cli_fd -> accept_fd */
74 	for (i = 0; i < 1000; i++)
75 		buf[i] = '+';
76 
77 	rv = send(cli_fd, buf, 1000, 0);
78 	if (CHECK(rv != 1000, "send(cli_fd)", "rv:%d errno:%d\n", rv, errno))
79 		goto done;
80 
81 	rv = recv(accept_fd, buf, 1000, 0);
82 	if (CHECK(rv != 1000, "recv(accept_fd)", "rv:%d errno:%d\n", rv, errno))
83 		goto done;
84 
85 	/* Send 500B of '.'s from accept_fd ->cli_fd */
86 	for (i = 0; i < 500; i++)
87 		buf[i] = '.';
88 
89 	rv = send(accept_fd, buf, 500, 0);
90 	if (CHECK(rv != 500, "send(accept_fd)", "rv:%d errno:%d\n", rv, errno))
91 		goto done;
92 
93 	rv = recv(cli_fd, buf, 500, 0);
94 	if (CHECK(rv != 500, "recv(cli_fd)", "rv:%d errno:%d\n", rv, errno))
95 		goto done;
96 
97 	/*
98 	 * shutdown accept first to guarantee correct ordering for
99 	 * bytes_received and bytes_acked when we go to verify the results.
100 	 */
101 	shutdown(accept_fd, SHUT_WR);
102 	err = recv(cli_fd, buf, 1, 0);
103 	if (CHECK(err, "recv(cli_fd) for fin", "err:%d errno:%d\n", err, errno))
104 		goto done;
105 
106 	shutdown(cli_fd, SHUT_WR);
107 	err = recv(accept_fd, buf, 1, 0);
108 	CHECK(err, "recv(accept_fd) for fin", "err:%d errno:%d\n", err, errno);
109 done:
110 	if (accept_fd != -1)
111 		close(accept_fd);
112 	if (cli_fd != -1)
113 		close(cli_fd);
114 	if (listen_fd != -1)
115 		close(listen_fd);
116 
117 	if (!err)
118 		verify_result(result);
119 }
120 
121 void test_tcpbpf_user(void)
122 {
123 	struct test_tcpbpf_kern *skel;
124 	int cg_fd = -1;
125 
126 	skel = test_tcpbpf_kern__open_and_load();
127 	if (CHECK(!skel, "open and load skel", "failed"))
128 		return;
129 
130 	cg_fd = test__join_cgroup(CG_NAME);
131 	if (CHECK(cg_fd < 0, "test__join_cgroup(" CG_NAME ")",
132 		  "cg_fd:%d errno:%d", cg_fd, errno))
133 		goto err;
134 
135 	skel->links.bpf_testcb = bpf_program__attach_cgroup(skel->progs.bpf_testcb, cg_fd);
136 	if (!ASSERT_OK_PTR(skel->links.bpf_testcb, "attach_cgroup(bpf_testcb)"))
137 		goto err;
138 
139 	run_test(&skel->bss->global);
140 
141 err:
142 	if (cg_fd != -1)
143 		close(cg_fd);
144 	test_tcpbpf_kern__destroy(skel);
145 }
146