11// SPDX-License-Identifier: GPL-2.0
2- #include <error.h>
3- #include <errno.h>
4- #include <stdio.h>
5- #include <unistd.h>
6- #include <sys/types.h>
7- #include <sys/socket.h>
8- #include <netinet/in.h>
9- #include <netinet/tcp.h>
10- #include <pthread.h>
11-
12- #include <linux/filter.h>
13- #include <bpf/bpf.h>
14- #include <bpf/libbpf.h>
15-
16- #include "bpf_rlimit.h"
17- #include "bpf_util.h"
2+ #include <test_progs.h>
183#include "cgroup_helpers.h"
194
20- #define CG_PATH "/tcp_rtt"
21-
225struct tcp_rtt_storage {
236 __u32 invoked ;
247 __u32 dsack_dups ;
@@ -31,8 +14,8 @@ static void send_byte(int fd)
3114{
3215 char b = 0x55 ;
3316
34- if (write (fd , & b , sizeof (b )) != 1 )
35- error ( 1 , errno , "Failed to send single byte" );
17+ if (CHECK_FAIL ( write (fd , & b , sizeof (b )) != 1 ) )
18+ perror ( "Failed to send single byte" );
3619}
3720
3821static int wait_for_ack (int fd , int retries )
@@ -66,8 +49,10 @@ static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 invoked,
6649 int err = 0 ;
6750 struct tcp_rtt_storage val ;
6851
69- if (bpf_map_lookup_elem (map_fd , & client_fd , & val ) < 0 )
70- error (1 , errno , "Failed to read socket storage" );
52+ if (CHECK_FAIL (bpf_map_lookup_elem (map_fd , & client_fd , & val ) < 0 )) {
53+ perror ("Failed to read socket storage" );
54+ return -1 ;
55+ }
7156
7257 if (val .invoked != invoked ) {
7358 log_err ("%s: unexpected bpf_tcp_sock.invoked %d != %d" ,
@@ -225,61 +210,47 @@ static void *server_thread(void *arg)
225210 int fd = * (int * )arg ;
226211 int client_fd ;
227212
228- if (listen (fd , 1 ) < 0 )
229- error (1 , errno , "Failed to listed on socket" );
213+ if (CHECK_FAIL (listen (fd , 1 )) < 0 ) {
214+ perror ("Failed to listed on socket" );
215+ return NULL ;
216+ }
230217
231218 client_fd = accept (fd , (struct sockaddr * )& addr , & len );
232- if (client_fd < 0 )
233- error (1 , errno , "Failed to accept client" );
219+ if (CHECK_FAIL (client_fd < 0 )) {
220+ perror ("Failed to accept client" );
221+ return NULL ;
222+ }
234223
235224 /* Wait for the next connection (that never arrives)
236225 * to keep this thread alive to prevent calling
237226 * close() on client_fd.
238227 */
239- if (accept (fd , (struct sockaddr * )& addr , & len ) >= 0 )
240- error (1 , errno , "Unexpected success in second accept" );
228+ if (CHECK_FAIL (accept (fd , (struct sockaddr * )& addr , & len ) >= 0 )) {
229+ perror ("Unexpected success in second accept" );
230+ return NULL ;
231+ }
241232
242233 close (client_fd );
243234
244235 return NULL ;
245236}
246237
247- int main ( int args , char * * argv )
238+ void test_tcp_rtt ( void )
248239{
249240 int server_fd , cgroup_fd ;
250- int err = EXIT_SUCCESS ;
251241 pthread_t tid ;
252242
253- if (setup_cgroup_environment ())
254- goto cleanup_obj ;
255-
256- cgroup_fd = create_and_get_cgroup (CG_PATH );
257- if (cgroup_fd < 0 )
258- goto cleanup_cgroup_env ;
259-
260- if (join_cgroup (CG_PATH ))
261- goto cleanup_cgroup ;
243+ cgroup_fd = test__join_cgroup ("/tcp_rtt" );
244+ if (CHECK_FAIL (cgroup_fd < 0 ))
245+ return ;
262246
263247 server_fd = start_server ();
264- if (server_fd < 0 ) {
265- err = EXIT_FAILURE ;
266- goto cleanup_cgroup ;
267- }
248+ if (CHECK_FAIL (server_fd < 0 ))
249+ goto close_cgroup_fd ;
268250
269251 pthread_create (& tid , NULL , server_thread , (void * )& server_fd );
270-
271- if (run_test (cgroup_fd , server_fd ))
272- err = EXIT_FAILURE ;
273-
252+ CHECK_FAIL (run_test (cgroup_fd , server_fd ));
274253 close (server_fd );
275-
276- printf ("test_sockopt_sk: %s\n" ,
277- err == EXIT_SUCCESS ? "PASSED" : "FAILED" );
278-
279- cleanup_cgroup :
254+ close_cgroup_fd :
280255 close (cgroup_fd );
281- cleanup_cgroup_env :
282- cleanup_cgroup_environment ();
283- cleanup_obj :
284- return err ;
285256}
0 commit comments