1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "cgroup_helpers.h"
4 #include "network_helpers.h"
5 #include "tcp_rtt.skel.h"
6 
7 struct tcp_rtt_storage {
8 	__u32 invoked;
9 	__u32 dsack_dups;
10 	__u32 delivered;
11 	__u32 delivered_ce;
12 	__u32 icsk_retransmits;
13 };
14 
15 static void send_byte(int fd)
16 {
17 	char b = 0x55;
18 
19 	ASSERT_EQ(write(fd, &b, sizeof(b)), 1, "send single byte");
20 }
21 
22 static int wait_for_ack(int fd, int retries)
23 {
24 	struct tcp_info info;
25 	socklen_t optlen;
26 	int i, err;
27 
28 	for (i = 0; i < retries; i++) {
29 		optlen = sizeof(info);
30 		err = getsockopt(fd, SOL_TCP, TCP_INFO, &info, &optlen);
31 		if (err < 0) {
32 			log_err("Failed to lookup TCP stats");
33 			return err;
34 		}
35 
36 		if (info.tcpi_unacked == 0)
37 			return 0;
38 
39 		usleep(10);
40 	}
41 
42 	log_err("Did not receive ACK");
43 	return -1;
44 }
45 
46 static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 invoked,
47 		     __u32 dsack_dups, __u32 delivered, __u32 delivered_ce,
48 		     __u32 icsk_retransmits)
49 {
50 	int err = 0;
51 	struct tcp_rtt_storage val;
52 
53 	if (!ASSERT_GE(bpf_map_lookup_elem(map_fd, &client_fd, &val), 0, "read socket storage"))
54 		return -1;
55 
56 	if (val.invoked != invoked) {
57 		log_err("%s: unexpected bpf_tcp_sock.invoked %d != %d",
58 			msg, val.invoked, invoked);
59 		err++;
60 	}
61 
62 	if (val.dsack_dups != dsack_dups) {
63 		log_err("%s: unexpected bpf_tcp_sock.dsack_dups %d != %d",
64 			msg, val.dsack_dups, dsack_dups);
65 		err++;
66 	}
67 
68 	if (val.delivered != delivered) {
69 		log_err("%s: unexpected bpf_tcp_sock.delivered %d != %d",
70 			msg, val.delivered, delivered);
71 		err++;
72 	}
73 
74 	if (val.delivered_ce != delivered_ce) {
75 		log_err("%s: unexpected bpf_tcp_sock.delivered_ce %d != %d",
76 			msg, val.delivered_ce, delivered_ce);
77 		err++;
78 	}
79 
80 	if (val.icsk_retransmits != icsk_retransmits) {
81 		log_err("%s: unexpected bpf_tcp_sock.icsk_retransmits %d != %d",
82 			msg, val.icsk_retransmits, icsk_retransmits);
83 		err++;
84 	}
85 
86 	return err;
87 }
88 
89 
90 static int run_test(int cgroup_fd, int server_fd)
91 {
92 	struct tcp_rtt *skel;
93 	int client_fd;
94 	int prog_fd;
95 	int map_fd;
96 	int err;
97 
98 	skel = tcp_rtt__open_and_load();
99 	if (!ASSERT_OK_PTR(skel, "skel_open_load"))
100 		return -1;
101 
102 	map_fd = bpf_map__fd(skel->maps.socket_storage_map);
103 	prog_fd = bpf_program__fd(skel->progs._sockops);
104 
105 	err = bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS, 0);
106 	if (err) {
107 		log_err("Failed to attach BPF program");
108 		goto close_bpf_object;
109 	}
110 
111 	client_fd = connect_to_fd(server_fd, 0);
112 	if (client_fd < 0) {
113 		err = -1;
114 		goto close_bpf_object;
115 	}
116 
117 	err += verify_sk(map_fd, client_fd, "syn-ack",
118 			 /*invoked=*/1,
119 			 /*dsack_dups=*/0,
120 			 /*delivered=*/1,
121 			 /*delivered_ce=*/0,
122 			 /*icsk_retransmits=*/0);
123 
124 	send_byte(client_fd);
125 	if (wait_for_ack(client_fd, 100) < 0) {
126 		err = -1;
127 		goto close_client_fd;
128 	}
129 
130 
131 	err += verify_sk(map_fd, client_fd, "first payload byte",
132 			 /*invoked=*/2,
133 			 /*dsack_dups=*/0,
134 			 /*delivered=*/2,
135 			 /*delivered_ce=*/0,
136 			 /*icsk_retransmits=*/0);
137 
138 close_client_fd:
139 	close(client_fd);
140 
141 close_bpf_object:
142 	tcp_rtt__destroy(skel);
143 	return err;
144 }
145 
146 void test_tcp_rtt(void)
147 {
148 	int server_fd, cgroup_fd;
149 
150 	cgroup_fd = test__join_cgroup("/tcp_rtt");
151 	if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup /tcp_rtt"))
152 		return;
153 
154 	server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
155 	if (!ASSERT_GE(server_fd, 0, "start_server"))
156 		goto close_cgroup_fd;
157 
158 	ASSERT_OK(run_test(cgroup_fd, server_fd), "run_test");
159 
160 	close(server_fd);
161 
162 close_cgroup_fd:
163 	close(cgroup_fd);
164 }
165