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