66#include "test_skmsg_load_helpers.skel.h"
77#include "test_sockmap_update.skel.h"
88#include "test_sockmap_invalid_update.skel.h"
9+ #include "bpf_iter_sockmap.skel.h"
10+
11+ #include "progs/bpf_iter_sockmap.h"
912
1013#define TCP_REPAIR 19 /* TCP sock is under repair right now */
1114
@@ -171,6 +174,88 @@ static void test_sockmap_invalid_update(void)
171174 test_sockmap_invalid_update__destroy (skel );
172175}
173176
177+ static void test_sockmap_iter (enum bpf_map_type map_type )
178+ {
179+ DECLARE_LIBBPF_OPTS (bpf_iter_attach_opts , opts );
180+ int err , len , src_fd , iter_fd , duration = 0 ;
181+ union bpf_iter_link_info linfo = {0 };
182+ __s64 sock_fd [SOCKMAP_MAX_ENTRIES ];
183+ __u32 i , num_sockets , max_elems ;
184+ struct bpf_iter_sockmap * skel ;
185+ struct bpf_link * link ;
186+ struct bpf_map * src ;
187+ char buf [64 ];
188+
189+ skel = bpf_iter_sockmap__open_and_load ();
190+ if (CHECK (!skel , "bpf_iter_sockmap__open_and_load" , "skeleton open_and_load failed\n" ))
191+ return ;
192+
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+
201+ if (map_type == BPF_MAP_TYPE_SOCKMAP ) {
202+ src = skel -> maps .sockmap ;
203+ max_elems = bpf_map__max_entries (src );
204+ } else {
205+ src = skel -> maps .sockhash ;
206+ max_elems = num_sockets ;
207+ }
208+
209+ src_fd = bpf_map__fd (src );
210+
211+ for (i = 0 ; i < num_sockets ; i ++ ) {
212+ sock_fd [i ] = connected_socket_v4 ();
213+ if (CHECK (sock_fd [i ] == -1 , "connected_socket_v4" , "cannot connect\n" ))
214+ goto out ;
215+
216+ err = bpf_map_update_elem (src_fd , & i , & sock_fd [i ], BPF_NOEXIST );
217+ if (CHECK (err , "map_update" , "failed: %s\n" , strerror (errno )))
218+ goto out ;
219+ }
220+
221+ linfo .map .map_fd = src_fd ;
222+ opts .link_info = & linfo ;
223+ opts .link_info_len = sizeof (linfo );
224+ link = bpf_program__attach_iter (skel -> progs .count_elems , & opts );
225+ if (CHECK (IS_ERR (link ), "attach_iter" , "attach_iter failed\n" ))
226+ goto out ;
227+
228+ iter_fd = bpf_iter_create (bpf_link__fd (link ));
229+ if (CHECK (iter_fd < 0 , "create_iter" , "create_iter failed\n" ))
230+ goto free_link ;
231+
232+ /* do some tests */
233+ while ((len = read (iter_fd , buf , sizeof (buf ))) > 0 )
234+ ;
235+ if (CHECK (len < 0 , "read" , "failed: %s\n" , strerror (errno )))
236+ goto close_iter ;
237+
238+ /* test results */
239+ if (CHECK (skel -> bss -> elems != max_elems , "elems" , "got %u expected %u\n" ,
240+ skel -> bss -> elems , max_elems ))
241+ goto close_iter ;
242+
243+ if (CHECK (skel -> bss -> socks != num_sockets , "socks" , "got %u expected %u\n" ,
244+ skel -> bss -> socks , num_sockets ))
245+ goto close_iter ;
246+
247+ close_iter :
248+ close (iter_fd );
249+ free_link :
250+ bpf_link__destroy (link );
251+ out :
252+ for (i = 0 ; i < num_sockets ; i ++ ) {
253+ if (sock_fd [i ] >= 0 )
254+ close (sock_fd [i ]);
255+ }
256+ bpf_iter_sockmap__destroy (skel );
257+ }
258+
174259void test_sockmap_basic (void )
175260{
176261 if (test__start_subtest ("sockmap create_update_free" ))
@@ -187,4 +272,8 @@ void test_sockmap_basic(void)
187272 test_sockmap_update (BPF_MAP_TYPE_SOCKHASH );
188273 if (test__start_subtest ("sockmap update in unsafe context" ))
189274 test_sockmap_invalid_update ();
275+ if (test__start_subtest ("sockmap iter" ))
276+ test_sockmap_iter (BPF_MAP_TYPE_SOCKMAP );
277+ if (test__start_subtest ("sockhash iter" ))
278+ test_sockmap_iter (BPF_MAP_TYPE_SOCKHASH );
190279}
0 commit comments