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 static __u32 duration;
0006 
0007 static void test_global_func_args0(struct bpf_object *obj)
0008 {
0009     int err, i, map_fd, actual_value;
0010     const char *map_name = "values";
0011 
0012     map_fd = bpf_find_map(__func__, obj, map_name);
0013     if (CHECK(map_fd < 0, "bpf_find_map", "cannot find BPF map %s: %s\n",
0014         map_name, strerror(errno)))
0015         return;
0016 
0017     struct {
0018         const char *descr;
0019         int expected_value;
0020     } tests[] = {
0021         {"passing NULL pointer", 0},
0022         {"returning value", 1},
0023         {"reading local variable", 100 },
0024         {"writing local variable", 101 },
0025         {"reading global variable", 42 },
0026         {"writing global variable", 43 },
0027         {"writing to pointer-to-pointer", 1 },
0028     };
0029 
0030     for (i = 0; i < ARRAY_SIZE(tests); ++i) {
0031         const int expected_value = tests[i].expected_value;
0032 
0033         err = bpf_map_lookup_elem(map_fd, &i, &actual_value);
0034 
0035         CHECK(err || actual_value != expected_value, tests[i].descr,
0036              "err %d result %d expected %d\n", err, actual_value, expected_value);
0037     }
0038 }
0039 
0040 void test_global_func_args(void)
0041 {
0042     const char *file = "./test_global_func_args.o";
0043     struct bpf_object *obj;
0044     int err, prog_fd;
0045     LIBBPF_OPTS(bpf_test_run_opts, topts,
0046         .data_in = &pkt_v4,
0047         .data_size_in = sizeof(pkt_v4),
0048         .repeat = 1,
0049     );
0050 
0051     err = bpf_prog_test_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd);
0052     if (CHECK(err, "load program", "error %d loading %s\n", err, file))
0053         return;
0054 
0055     err = bpf_prog_test_run_opts(prog_fd, &topts);
0056     ASSERT_OK(err, "test_run");
0057     ASSERT_OK(topts.retval, "test_run retval");
0058 
0059     test_global_func_args0(obj);
0060 
0061     bpf_object__close(obj);
0062 }