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