88#include "test_sockmap_update.skel.h"
99#include "test_sockmap_invalid_update.skel.h"
1010#include "test_sockmap_skb_verdict_attach.skel.h"
11+ #include "test_sockmap_progs_query.skel.h"
1112#include "bpf_iter_sockmap.skel.h"
1213
1314#define TCP_REPAIR 19 /* TCP sock is under repair right now */
@@ -315,6 +316,63 @@ static void test_sockmap_skb_verdict_attach(enum bpf_attach_type first,
315316 test_sockmap_skb_verdict_attach__destroy (skel );
316317}
317318
319+ static __u32 query_prog_id (int prog_fd )
320+ {
321+ struct bpf_prog_info info = {};
322+ __u32 info_len = sizeof (info );
323+ int err ;
324+
325+ err = bpf_obj_get_info_by_fd (prog_fd , & info , & info_len );
326+ if (!ASSERT_OK (err , "bpf_obj_get_info_by_fd" ) ||
327+ !ASSERT_EQ (info_len , sizeof (info ), "bpf_obj_get_info_by_fd" ))
328+ return 0 ;
329+
330+ return info .id ;
331+ }
332+
333+ static void test_sockmap_progs_query (enum bpf_attach_type attach_type )
334+ {
335+ struct test_sockmap_progs_query * skel ;
336+ int err , map_fd , verdict_fd ;
337+ __u32 attach_flags = 0 ;
338+ __u32 prog_ids [3 ] = {};
339+ __u32 prog_cnt = 3 ;
340+
341+ skel = test_sockmap_progs_query__open_and_load ();
342+ if (!ASSERT_OK_PTR (skel , "test_sockmap_progs_query__open_and_load" ))
343+ return ;
344+
345+ map_fd = bpf_map__fd (skel -> maps .sock_map );
346+
347+ if (attach_type == BPF_SK_MSG_VERDICT )
348+ verdict_fd = bpf_program__fd (skel -> progs .prog_skmsg_verdict );
349+ else
350+ verdict_fd = bpf_program__fd (skel -> progs .prog_skb_verdict );
351+
352+ err = bpf_prog_query (map_fd , attach_type , 0 /* query flags */ ,
353+ & attach_flags , prog_ids , & prog_cnt );
354+ ASSERT_OK (err , "bpf_prog_query failed" );
355+ ASSERT_EQ (attach_flags , 0 , "wrong attach_flags on query" );
356+ ASSERT_EQ (prog_cnt , 0 , "wrong program count on query" );
357+
358+ err = bpf_prog_attach (verdict_fd , map_fd , attach_type , 0 );
359+ if (!ASSERT_OK (err , "bpf_prog_attach failed" ))
360+ goto out ;
361+
362+ prog_cnt = 1 ;
363+ err = bpf_prog_query (map_fd , attach_type , 0 /* query flags */ ,
364+ & attach_flags , prog_ids , & prog_cnt );
365+ ASSERT_OK (err , "bpf_prog_query failed" );
366+ ASSERT_EQ (attach_flags , 0 , "wrong attach_flags on query" );
367+ ASSERT_EQ (prog_cnt , 1 , "wrong program count on query" );
368+ ASSERT_EQ (prog_ids [0 ], query_prog_id (verdict_fd ),
369+ "wrong prog_ids on query" );
370+
371+ bpf_prog_detach2 (verdict_fd , map_fd , attach_type );
372+ out :
373+ test_sockmap_progs_query__destroy (skel );
374+ }
375+
318376void test_sockmap_basic (void )
319377{
320378 if (test__start_subtest ("sockmap create_update_free" ))
@@ -341,4 +399,12 @@ void test_sockmap_basic(void)
341399 test_sockmap_skb_verdict_attach (BPF_SK_SKB_STREAM_VERDICT ,
342400 BPF_SK_SKB_VERDICT );
343401 }
402+ if (test__start_subtest ("sockmap msg_verdict progs query" ))
403+ test_sockmap_progs_query (BPF_SK_MSG_VERDICT );
404+ if (test__start_subtest ("sockmap stream_parser progs query" ))
405+ test_sockmap_progs_query (BPF_SK_SKB_STREAM_PARSER );
406+ if (test__start_subtest ("sockmap stream_verdict progs query" ))
407+ test_sockmap_progs_query (BPF_SK_SKB_STREAM_VERDICT );
408+ if (test__start_subtest ("sockmap skb_verdict progs query" ))
409+ test_sockmap_progs_query (BPF_SK_SKB_VERDICT );
344410}
0 commit comments