2019-03-02 06:42:19 +03:00
// SPDX-License-Identifier: GPL-2.0
# include <test_progs.h>
2020-05-08 20:46:09 +03:00
# include <network_helpers.h>
2019-03-02 06:42:19 +03:00
2021-03-03 13:18:15 +03:00
# include "test_pkt_access.skel.h"
static const __u32 duration ;
static void check_run_cnt ( int prog_fd , __u64 run_cnt )
2019-03-02 06:42:19 +03:00
{
2021-03-03 13:18:15 +03:00
struct bpf_prog_info info = { } ;
__u32 info_len = sizeof ( info ) ;
2019-03-02 06:42:19 +03:00
int err ;
2021-03-03 13:18:15 +03:00
err = bpf_obj_get_info_by_fd ( prog_fd , & info , & info_len ) ;
if ( CHECK ( err , " get_prog_info " , " failed to get bpf_prog_info for fd %d \n " , prog_fd ) )
return ;
CHECK ( run_cnt ! = info . run_cnt , " run_cnt " ,
" incorrect number of repetitions, want %llu have %llu \n " , run_cnt , info . run_cnt ) ;
}
void test_prog_run_xattr ( void )
{
struct test_pkt_access * skel ;
int err , stats_fd = - 1 ;
char buf [ 10 ] = { } ;
__u64 run_cnt = 0 ;
2019-03-02 06:42:19 +03:00
struct bpf_prog_test_run_attr tattr = {
. repeat = 1 ,
. data_in = & pkt_v4 ,
. data_size_in = sizeof ( pkt_v4 ) ,
. data_out = buf ,
. data_size_out = 5 ,
} ;
2021-03-03 13:18:15 +03:00
stats_fd = bpf_enable_stats ( BPF_STATS_RUN_TIME ) ;
if ( CHECK_ATTR ( stats_fd < 0 , " enable_stats " , " failed %d \n " , errno ) )
2019-03-02 06:42:19 +03:00
return ;
2021-03-03 13:18:15 +03:00
skel = test_pkt_access__open_and_load ( ) ;
if ( CHECK_ATTR ( ! skel , " open_and_load " , " failed \n " ) )
goto cleanup ;
tattr . prog_fd = bpf_program__fd ( skel - > progs . test_pkt_access ) ;
2019-03-02 06:42:19 +03:00
err = bpf_prog_test_run_xattr ( & tattr ) ;
2021-05-25 06:59:32 +03:00
CHECK_ATTR ( err > = 0 | | errno ! = ENOSPC | | tattr . retval , " run " ,
2019-03-02 06:42:19 +03:00
" err %d errno %d retval %d \n " , err , errno , tattr . retval ) ;
CHECK_ATTR ( tattr . data_size_out ! = sizeof ( pkt_v4 ) , " data_size_out " ,
2020-08-13 23:49:38 +03:00
" incorrect output size, want %zu have %u \n " ,
2019-03-02 06:42:19 +03:00
sizeof ( pkt_v4 ) , tattr . data_size_out ) ;
CHECK_ATTR ( buf [ 5 ] ! = 0 , " overflow " ,
" BPF_PROG_TEST_RUN ignored size hint \n " ) ;
2021-03-03 13:18:15 +03:00
run_cnt + = tattr . repeat ;
check_run_cnt ( tattr . prog_fd , run_cnt ) ;
2019-03-02 06:42:19 +03:00
tattr . data_out = NULL ;
tattr . data_size_out = 0 ;
2021-03-03 13:18:15 +03:00
tattr . repeat = 2 ;
2019-03-02 06:42:19 +03:00
errno = 0 ;
err = bpf_prog_test_run_xattr ( & tattr ) ;
CHECK_ATTR ( err | | errno | | tattr . retval , " run_no_output " ,
" err %d errno %d retval %d \n " , err , errno , tattr . retval ) ;
tattr . data_size_out = 1 ;
err = bpf_prog_test_run_xattr ( & tattr ) ;
CHECK_ATTR ( err ! = - EINVAL , " run_wrong_size_out " , " err %d \n " , err ) ;
2021-03-03 13:18:15 +03:00
run_cnt + = tattr . repeat ;
check_run_cnt ( tattr . prog_fd , run_cnt ) ;
cleanup :
if ( skel )
test_pkt_access__destroy ( skel ) ;
2021-05-25 06:59:32 +03:00
if ( stats_fd > = 0 )
2021-03-03 13:18:15 +03:00
close ( stats_fd ) ;
2019-03-02 06:42:19 +03:00
}