1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) 2021. Huawei Technologies Co., Ltd */ 3 #include <test_progs.h> 4 #include "dummy_st_ops.skel.h" 5 6 /* Need to keep consistent with definition in include/linux/bpf.h */ 7 struct bpf_dummy_ops_state { 8 int val; 9 }; 10 11 static void test_dummy_st_ops_attach(void) 12 { 13 struct dummy_st_ops *skel; 14 struct bpf_link *link; 15 16 skel = dummy_st_ops__open_and_load(); 17 if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) 18 return; 19 20 link = bpf_map__attach_struct_ops(skel->maps.dummy_1); 21 ASSERT_EQ(libbpf_get_error(link), -EOPNOTSUPP, "dummy_st_ops_attach"); 22 23 dummy_st_ops__destroy(skel); 24 } 25 26 static void test_dummy_init_ret_value(void) 27 { 28 __u64 args[1] = {0}; 29 LIBBPF_OPTS(bpf_test_run_opts, attr, 30 .ctx_in = args, 31 .ctx_size_in = sizeof(args), 32 ); 33 struct dummy_st_ops *skel; 34 int fd, err; 35 36 skel = dummy_st_ops__open_and_load(); 37 if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) 38 return; 39 40 fd = bpf_program__fd(skel->progs.test_1); 41 err = bpf_prog_test_run_opts(fd, &attr); 42 ASSERT_OK(err, "test_run"); 43 ASSERT_EQ(attr.retval, 0xf2f3f4f5, "test_ret"); 44 45 dummy_st_ops__destroy(skel); 46 } 47 48 static void test_dummy_init_ptr_arg(void) 49 { 50 int exp_retval = 0xbeef; 51 struct bpf_dummy_ops_state in_state = { 52 .val = exp_retval, 53 }; 54 __u64 args[1] = {(unsigned long)&in_state}; 55 LIBBPF_OPTS(bpf_test_run_opts, attr, 56 .ctx_in = args, 57 .ctx_size_in = sizeof(args), 58 ); 59 struct dummy_st_ops *skel; 60 int fd, err; 61 62 skel = dummy_st_ops__open_and_load(); 63 if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) 64 return; 65 66 fd = bpf_program__fd(skel->progs.test_1); 67 err = bpf_prog_test_run_opts(fd, &attr); 68 ASSERT_OK(err, "test_run"); 69 ASSERT_EQ(in_state.val, 0x5a, "test_ptr_ret"); 70 ASSERT_EQ(attr.retval, exp_retval, "test_ret"); 71 72 dummy_st_ops__destroy(skel); 73 } 74 75 static void test_dummy_multiple_args(void) 76 { 77 __u64 args[5] = {0, -100, 0x8a5f, 'c', 0x1234567887654321ULL}; 78 LIBBPF_OPTS(bpf_test_run_opts, attr, 79 .ctx_in = args, 80 .ctx_size_in = sizeof(args), 81 ); 82 struct dummy_st_ops *skel; 83 int fd, err; 84 size_t i; 85 char name[8]; 86 87 skel = dummy_st_ops__open_and_load(); 88 if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load")) 89 return; 90 91 fd = bpf_program__fd(skel->progs.test_2); 92 err = bpf_prog_test_run_opts(fd, &attr); 93 ASSERT_OK(err, "test_run"); 94 for (i = 0; i < ARRAY_SIZE(args); i++) { 95 snprintf(name, sizeof(name), "arg %zu", i); 96 ASSERT_EQ(skel->bss->test_2_args[i], args[i], name); 97 } 98 99 dummy_st_ops__destroy(skel); 100 } 101 102 void test_dummy_st_ops(void) 103 { 104 if (test__start_subtest("dummy_st_ops_attach")) 105 test_dummy_st_ops_attach(); 106 if (test__start_subtest("dummy_init_ret_value")) 107 test_dummy_init_ret_value(); 108 if (test__start_subtest("dummy_init_ptr_arg")) 109 test_dummy_init_ptr_arg(); 110 if (test__start_subtest("dummy_multiple_args")) 111 test_dummy_multiple_args(); 112 } 113