Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <test_progs.h>
0003 #include <network_helpers.h>
0004 
0005 #include "test_pkt_access.skel.h"
0006 
0007 static const __u32 duration;
0008 
0009 static void check_run_cnt(int prog_fd, __u64 run_cnt)
0010 {
0011     struct bpf_prog_info info = {};
0012     __u32 info_len = sizeof(info);
0013     int err;
0014 
0015     err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
0016     if (CHECK(err, "get_prog_info", "failed to get bpf_prog_info for fd %d\n", prog_fd))
0017         return;
0018 
0019     CHECK(run_cnt != info.run_cnt, "run_cnt",
0020           "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt);
0021 }
0022 
0023 void test_prog_run_opts(void)
0024 {
0025     struct test_pkt_access *skel;
0026     int err, stats_fd = -1, prog_fd;
0027     char buf[10] = {};
0028     __u64 run_cnt = 0;
0029 
0030     LIBBPF_OPTS(bpf_test_run_opts, topts,
0031         .repeat = 1,
0032         .data_in = &pkt_v4,
0033         .data_size_in = sizeof(pkt_v4),
0034         .data_out = buf,
0035         .data_size_out = 5,
0036     );
0037 
0038     stats_fd = bpf_enable_stats(BPF_STATS_RUN_TIME);
0039     if (!ASSERT_GE(stats_fd, 0, "enable_stats good fd"))
0040         return;
0041 
0042     skel = test_pkt_access__open_and_load();
0043     if (!ASSERT_OK_PTR(skel, "open_and_load"))
0044         goto cleanup;
0045 
0046     prog_fd = bpf_program__fd(skel->progs.test_pkt_access);
0047 
0048     err = bpf_prog_test_run_opts(prog_fd, &topts);
0049     ASSERT_EQ(errno, ENOSPC, "test_run errno");
0050     ASSERT_ERR(err, "test_run");
0051     ASSERT_OK(topts.retval, "test_run retval");
0052 
0053     ASSERT_EQ(topts.data_size_out, sizeof(pkt_v4), "test_run data_size_out");
0054     ASSERT_EQ(buf[5], 0, "overflow, BPF_PROG_TEST_RUN ignored size hint");
0055 
0056     run_cnt += topts.repeat;
0057     check_run_cnt(prog_fd, run_cnt);
0058 
0059     topts.data_out = NULL;
0060     topts.data_size_out = 0;
0061     topts.repeat = 2;
0062     errno = 0;
0063 
0064     err = bpf_prog_test_run_opts(prog_fd, &topts);
0065     ASSERT_OK(errno, "run_no_output errno");
0066     ASSERT_OK(err, "run_no_output err");
0067     ASSERT_OK(topts.retval, "run_no_output retval");
0068 
0069     run_cnt += topts.repeat;
0070     check_run_cnt(prog_fd, run_cnt);
0071 
0072 cleanup:
0073     if (skel)
0074         test_pkt_access__destroy(skel);
0075     if (stats_fd >= 0)
0076         close(stats_fd);
0077 }