kfunc_call.c (2731B)
1// SPDX-License-Identifier: GPL-2.0 2/* Copyright (c) 2021 Facebook */ 3#include <test_progs.h> 4#include <network_helpers.h> 5#include "kfunc_call_test.lskel.h" 6#include "kfunc_call_test_subprog.skel.h" 7#include "kfunc_call_test_subprog.lskel.h" 8 9static void test_main(void) 10{ 11 struct kfunc_call_test_lskel *skel; 12 int prog_fd, err; 13 LIBBPF_OPTS(bpf_test_run_opts, topts, 14 .data_in = &pkt_v4, 15 .data_size_in = sizeof(pkt_v4), 16 .repeat = 1, 17 ); 18 19 skel = kfunc_call_test_lskel__open_and_load(); 20 if (!ASSERT_OK_PTR(skel, "skel")) 21 return; 22 23 prog_fd = skel->progs.kfunc_call_test1.prog_fd; 24 err = bpf_prog_test_run_opts(prog_fd, &topts); 25 ASSERT_OK(err, "bpf_prog_test_run(test1)"); 26 ASSERT_EQ(topts.retval, 12, "test1-retval"); 27 28 prog_fd = skel->progs.kfunc_call_test2.prog_fd; 29 err = bpf_prog_test_run_opts(prog_fd, &topts); 30 ASSERT_OK(err, "bpf_prog_test_run(test2)"); 31 ASSERT_EQ(topts.retval, 3, "test2-retval"); 32 33 prog_fd = skel->progs.kfunc_call_test_ref_btf_id.prog_fd; 34 err = bpf_prog_test_run_opts(prog_fd, &topts); 35 ASSERT_OK(err, "bpf_prog_test_run(test_ref_btf_id)"); 36 ASSERT_EQ(topts.retval, 0, "test_ref_btf_id-retval"); 37 38 kfunc_call_test_lskel__destroy(skel); 39} 40 41static void test_subprog(void) 42{ 43 struct kfunc_call_test_subprog *skel; 44 int prog_fd, err; 45 LIBBPF_OPTS(bpf_test_run_opts, topts, 46 .data_in = &pkt_v4, 47 .data_size_in = sizeof(pkt_v4), 48 .repeat = 1, 49 ); 50 51 skel = kfunc_call_test_subprog__open_and_load(); 52 if (!ASSERT_OK_PTR(skel, "skel")) 53 return; 54 55 prog_fd = bpf_program__fd(skel->progs.kfunc_call_test1); 56 err = bpf_prog_test_run_opts(prog_fd, &topts); 57 ASSERT_OK(err, "bpf_prog_test_run(test1)"); 58 ASSERT_EQ(topts.retval, 10, "test1-retval"); 59 ASSERT_NEQ(skel->data->active_res, -1, "active_res"); 60 ASSERT_EQ(skel->data->sk_state_res, BPF_TCP_CLOSE, "sk_state_res"); 61 62 kfunc_call_test_subprog__destroy(skel); 63} 64 65static void test_subprog_lskel(void) 66{ 67 struct kfunc_call_test_subprog_lskel *skel; 68 int prog_fd, err; 69 LIBBPF_OPTS(bpf_test_run_opts, topts, 70 .data_in = &pkt_v4, 71 .data_size_in = sizeof(pkt_v4), 72 .repeat = 1, 73 ); 74 75 skel = kfunc_call_test_subprog_lskel__open_and_load(); 76 if (!ASSERT_OK_PTR(skel, "skel")) 77 return; 78 79 prog_fd = skel->progs.kfunc_call_test1.prog_fd; 80 err = bpf_prog_test_run_opts(prog_fd, &topts); 81 ASSERT_OK(err, "bpf_prog_test_run(test1)"); 82 ASSERT_EQ(topts.retval, 10, "test1-retval"); 83 ASSERT_NEQ(skel->data->active_res, -1, "active_res"); 84 ASSERT_EQ(skel->data->sk_state_res, BPF_TCP_CLOSE, "sk_state_res"); 85 86 kfunc_call_test_subprog_lskel__destroy(skel); 87} 88 89void test_kfunc_call(void) 90{ 91 if (test__start_subtest("main")) 92 test_main(); 93 94 if (test__start_subtest("subprog")) 95 test_subprog(); 96 97 if (test__start_subtest("subprog_lskel")) 98 test_subprog_lskel(); 99}