88#include  "test_sockmap_invalid_update.skel.h" 
99#include  "bpf_iter_sockmap.skel.h" 
1010
11- #include  "progs/bpf_iter_sockmap.h" 
12- 
1311#define  TCP_REPAIR 		19	/* TCP sock is under repair right now */ 
1412
1513#define  TCP_REPAIR_ON 		1
@@ -201,9 +199,9 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
201199	DECLARE_LIBBPF_OPTS (bpf_iter_attach_opts , opts );
202200	int  err , len , src_fd , iter_fd , duration  =  0 ;
203201	union  bpf_iter_link_info  linfo  =  {0 };
204- 	__s64  sock_fd [SOCKMAP_MAX_ENTRIES ];
205- 	__u32  i , num_sockets , max_elems ;
202+ 	__u32  i , num_sockets , num_elems ;
206203	struct  bpf_iter_sockmap  * skel ;
204+ 	__s64  * sock_fd  =  NULL ;
207205	struct  bpf_link  * link ;
208206	struct  bpf_map  * src ;
209207	char  buf [64 ];
@@ -212,22 +210,23 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
212210	if  (CHECK (!skel , "bpf_iter_sockmap__open_and_load" , "skeleton open_and_load failed\n" ))
213211		return ;
214212
215- 	for  (i  =  0 ; i  <  ARRAY_SIZE (sock_fd ); i ++ )
216- 		sock_fd [i ] =  -1 ;
217- 
218- 	/* Make sure we have at least one "empty" entry to test iteration of 
219- 	 * an empty slot. 
220- 	 */ 
221- 	num_sockets  =  ARRAY_SIZE (sock_fd ) -  1 ;
222- 
223213	if  (map_type  ==  BPF_MAP_TYPE_SOCKMAP ) {
224214		src  =  skel -> maps .sockmap ;
225- 		max_elems  =  bpf_map__max_entries (src );
215+ 		num_elems  =  bpf_map__max_entries (src );
216+ 		num_sockets  =  num_elems  -  1 ;
226217	} else  {
227218		src  =  skel -> maps .sockhash ;
228- 		max_elems  =  num_sockets ;
219+ 		num_elems  =  bpf_map__max_entries (src ) -  1 ;
220+ 		num_sockets  =  num_elems ;
229221	}
230222
223+ 	sock_fd  =  calloc (num_sockets , sizeof (* sock_fd ));
224+ 	if  (CHECK (!sock_fd , "calloc(sock_fd)" , "failed to allocate\n" ))
225+ 		goto out ;
226+ 
227+ 	for  (i  =  0 ; i  <  num_sockets ; i ++ )
228+ 		sock_fd [i ] =  -1 ;
229+ 
231230	src_fd  =  bpf_map__fd (src );
232231
233232	for  (i  =  0 ; i  <  num_sockets ; i ++ ) {
@@ -258,8 +257,8 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
258257		goto close_iter ;
259258
260259	/* test results */ 
261- 	if  (CHECK (skel -> bss -> elems  !=  max_elems , "elems" , "got %u expected %u\n" ,
262- 		  skel -> bss -> elems , max_elems ))
260+ 	if  (CHECK (skel -> bss -> elems  !=  num_elems , "elems" , "got %u expected %u\n" ,
261+ 		  skel -> bss -> elems , num_elems ))
263262		goto close_iter ;
264263
265264	if  (CHECK (skel -> bss -> socks  !=  num_sockets , "socks" , "got %u expected %u\n" ,
@@ -271,10 +270,11 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
271270free_link :
272271	bpf_link__destroy (link );
273272out :
274- 	for  (i  =  0 ; i  <  num_sockets ; i ++ ) { 
273+ 	for  (i  =  0 ; sock_fd   &&   i  <  num_sockets ; i ++ )
275274		if  (sock_fd [i ] >= 0 )
276275			close (sock_fd [i ]);
277- 	}
276+ 	if  (sock_fd )
277+ 		free (sock_fd );
278278	bpf_iter_sockmap__destroy (skel );
279279}
280280
0 commit comments