1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
5 static void test_global_data_number(struct bpf_object *obj, __u32 duration)
10 map_fd = bpf_find_map(__func__, obj, "result_number");
11 if (CHECK_FAIL(map_fd < 0))
19 { "relocate .bss reference", 0, 0 },
20 { "relocate .data reference", 1, 42 },
21 { "relocate .rodata reference", 2, 24 },
22 { "relocate .bss reference", 3, 0 },
23 { "relocate .data reference", 4, 0xffeeff },
24 { "relocate .rodata reference", 5, 0xabab },
25 { "relocate .bss reference", 6, 1234 },
26 { "relocate .bss reference", 7, 0 },
27 { "relocate .rodata reference", 8, 0xab },
28 { "relocate .rodata reference", 9, 0x1111111111111111 },
29 { "relocate .rodata reference", 10, ~0 },
32 for (i = 0; i < ARRAY_SIZE(tests); i++) {
33 err = bpf_map_lookup_elem(map_fd, &tests[i].key, &num);
34 CHECK(err || num != tests[i].num, tests[i].name,
35 "err %d result %llx expected %llx\n",
36 err, num, tests[i].num);
40 static void test_global_data_string(struct bpf_object *obj, __u32 duration)
45 map_fd = bpf_find_map(__func__, obj, "result_string");
46 if (CHECK_FAIL(map_fd < 0))
54 { "relocate .rodata reference", 0, "abcdefghijklmnopqrstuvwxyz" },
55 { "relocate .data reference", 1, "abcdefghijklmnopqrstuvwxyz" },
56 { "relocate .bss reference", 2, "" },
57 { "relocate .data reference", 3, "abcdexghijklmnopqrstuvwxyz" },
58 { "relocate .bss reference", 4, "\0\0hello" },
61 for (i = 0; i < ARRAY_SIZE(tests); i++) {
62 err = bpf_map_lookup_elem(map_fd, &tests[i].key, str);
63 CHECK(err || memcmp(str, tests[i].str, sizeof(str)),
64 tests[i].name, "err %d result \'%s\' expected \'%s\'\n",
65 err, str, tests[i].str);
75 static void test_global_data_struct(struct bpf_object *obj, __u32 duration)
80 map_fd = bpf_find_map(__func__, obj, "result_struct");
81 if (CHECK_FAIL(map_fd < 0))
89 { "relocate .rodata reference", 0, { 42, 0xfefeefef, 0x1111111111111111ULL, } },
90 { "relocate .bss reference", 1, { } },
91 { "relocate .rodata reference", 2, { } },
92 { "relocate .data reference", 3, { 41, 0xeeeeefef, 0x2111111111111111ULL, } },
95 for (i = 0; i < ARRAY_SIZE(tests); i++) {
96 err = bpf_map_lookup_elem(map_fd, &tests[i].key, &val);
97 CHECK(err || memcmp(&val, &tests[i].val, sizeof(val)),
98 tests[i].name, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n",
99 err, val.a, val.b, val.c, tests[i].val.a, tests[i].val.b, tests[i].val.c);
103 static void test_global_data_rdonly(struct bpf_object *obj, __u32 duration)
105 int err = -ENOMEM, map_fd, zero = 0;
106 struct bpf_map *map, *map2;
109 map = bpf_object__find_map_by_name(obj, "test_glo.rodata");
110 if (!ASSERT_OK_PTR(map, "map"))
112 if (!ASSERT_TRUE(bpf_map__is_internal(map), "is_internal"))
115 /* ensure we can lookup internal maps by their ELF names */
116 map2 = bpf_object__find_map_by_name(obj, ".rodata");
117 if (!ASSERT_EQ(map, map2, "same_maps"))
120 map_fd = bpf_map__fd(map);
121 if (CHECK_FAIL(map_fd < 0))
124 buff = malloc(bpf_map__value_size(map));
126 err = bpf_map_update_elem(map_fd, &zero, buff, 0);
128 CHECK(!err || errno != EPERM, "test .rodata read-only map",
129 "err %d errno %d\n", err, errno);
132 void test_global_data(void)
134 const char *file = "./test_global_data.bpf.o";
135 struct bpf_object *obj;
137 LIBBPF_OPTS(bpf_test_run_opts, topts,
139 .data_size_in = sizeof(pkt_v4),
143 err = bpf_prog_test_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
144 if (!ASSERT_OK(err, "load program"))
147 err = bpf_prog_test_run_opts(prog_fd, &topts);
148 ASSERT_OK(err, "pass global data run err");
149 ASSERT_OK(topts.retval, "pass global data run retval");
151 test_global_data_number(obj, topts.duration);
152 test_global_data_string(obj, topts.duration);
153 test_global_data_struct(obj, topts.duration);
154 test_global_data_rdonly(obj, topts.duration);
156 bpf_object__close(obj);