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
@@ -179,9 +177,9 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
179177 DECLARE_LIBBPF_OPTS (bpf_iter_attach_opts , opts );
180178 int err , len , src_fd , iter_fd , duration = 0 ;
181179 union bpf_iter_link_info linfo = {0 };
182- __s64 sock_fd [SOCKMAP_MAX_ENTRIES ];
183- __u32 i , num_sockets , max_elems ;
180+ __u32 i , num_sockets , num_elems ;
184181 struct bpf_iter_sockmap * skel ;
182+ __s64 * sock_fd = NULL ;
185183 struct bpf_link * link ;
186184 struct bpf_map * src ;
187185 char buf [64 ];
@@ -190,22 +188,26 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
190188 if (CHECK (!skel , "bpf_iter_sockmap__open_and_load" , "skeleton open_and_load failed\n" ))
191189 return ;
192190
193- for (i = 0 ; i < ARRAY_SIZE (sock_fd ); i ++ )
194- sock_fd [i ] = -1 ;
195-
196- /* Make sure we have at least one "empty" entry to test iteration of
197- * an empty slot.
198- */
199- num_sockets = ARRAY_SIZE (sock_fd ) - 1 ;
200-
201191 if (map_type == BPF_MAP_TYPE_SOCKMAP ) {
202192 src = skel -> maps .sockmap ;
203- max_elems = bpf_map__max_entries (src );
193+ num_elems = bpf_map__max_entries (src );
204194 } else {
205195 src = skel -> maps .sockhash ;
206- max_elems = num_sockets ;
196+ num_elems = bpf_map__max_entries ( src ) - 1 ;
207197 }
208198
199+ /* Make sure we have at least one "empty" entry to test iteration of
200+ * an empty slot.
201+ */
202+ num_sockets = bpf_map__max_entries (src ) - 1 ;
203+
204+ sock_fd = calloc (num_sockets , sizeof (* sock_fd ));
205+ if (CHECK (!sock_fd , "calloc(sock_fd)" , "failed to allocate\n" ))
206+ goto out ;
207+
208+ for (i = 0 ; i < num_sockets ; i ++ )
209+ sock_fd [i ] = -1 ;
210+
209211 src_fd = bpf_map__fd (src );
210212
211213 for (i = 0 ; i < num_sockets ; i ++ ) {
@@ -236,8 +238,8 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
236238 goto close_iter ;
237239
238240 /* test results */
239- if (CHECK (skel -> bss -> elems != max_elems , "elems" , "got %u expected %u\n" ,
240- skel -> bss -> elems , max_elems ))
241+ if (CHECK (skel -> bss -> elems != num_elems , "elems" , "got %u expected %u\n" ,
242+ skel -> bss -> elems , num_elems ))
241243 goto close_iter ;
242244
243245 if (CHECK (skel -> bss -> socks != num_sockets , "socks" , "got %u expected %u\n" ,
@@ -249,10 +251,11 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
249251free_link :
250252 bpf_link__destroy (link );
251253out :
252- for (i = 0 ; i < num_sockets ; i ++ ) {
254+ for (i = 0 ; sock_fd && i < num_sockets ; i ++ )
253255 if (sock_fd [i ] >= 0 )
254256 close (sock_fd [i ]);
255- }
257+ if (sock_fd )
258+ free (sock_fd );
256259 bpf_iter_sockmap__destroy (skel );
257260}
258261
0 commit comments