1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 static void test_global_data_number(struct bpf_object
*obj
, __u32 duration
)
9 map_fd
= bpf_find_map(__func__
, obj
, "result_number");
10 if (CHECK_FAIL(map_fd
< 0))
18 { "relocate .bss reference", 0, 0 },
19 { "relocate .data reference", 1, 42 },
20 { "relocate .rodata reference", 2, 24 },
21 { "relocate .bss reference", 3, 0 },
22 { "relocate .data reference", 4, 0xffeeff },
23 { "relocate .rodata reference", 5, 0xabab },
24 { "relocate .bss reference", 6, 1234 },
25 { "relocate .bss reference", 7, 0 },
26 { "relocate .rodata reference", 8, 0xab },
27 { "relocate .rodata reference", 9, 0x1111111111111111 },
28 { "relocate .rodata reference", 10, ~0 },
31 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++) {
32 err
= bpf_map_lookup_elem(map_fd
, &tests
[i
].key
, &num
);
33 CHECK(err
|| num
!= tests
[i
].num
, tests
[i
].name
,
34 "err %d result %lx expected %lx\n",
35 err
, num
, tests
[i
].num
);
39 static void test_global_data_string(struct bpf_object
*obj
, __u32 duration
)
44 map_fd
= bpf_find_map(__func__
, obj
, "result_string");
45 if (CHECK_FAIL(map_fd
< 0))
53 { "relocate .rodata reference", 0, "abcdefghijklmnopqrstuvwxyz" },
54 { "relocate .data reference", 1, "abcdefghijklmnopqrstuvwxyz" },
55 { "relocate .bss reference", 2, "" },
56 { "relocate .data reference", 3, "abcdexghijklmnopqrstuvwxyz" },
57 { "relocate .bss reference", 4, "\0\0hello" },
60 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++) {
61 err
= bpf_map_lookup_elem(map_fd
, &tests
[i
].key
, str
);
62 CHECK(err
|| memcmp(str
, tests
[i
].str
, sizeof(str
)),
63 tests
[i
].name
, "err %d result \'%s\' expected \'%s\'\n",
64 err
, str
, tests
[i
].str
);
74 static void test_global_data_struct(struct bpf_object
*obj
, __u32 duration
)
79 map_fd
= bpf_find_map(__func__
, obj
, "result_struct");
80 if (CHECK_FAIL(map_fd
< 0))
88 { "relocate .rodata reference", 0, { 42, 0xfefeefef, 0x1111111111111111ULL
, } },
89 { "relocate .bss reference", 1, { } },
90 { "relocate .rodata reference", 2, { } },
91 { "relocate .data reference", 3, { 41, 0xeeeeefef, 0x2111111111111111ULL
, } },
94 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++) {
95 err
= bpf_map_lookup_elem(map_fd
, &tests
[i
].key
, &val
);
96 CHECK(err
|| memcmp(&val
, &tests
[i
].val
, sizeof(val
)),
97 tests
[i
].name
, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n",
98 err
, val
.a
, val
.b
, val
.c
, tests
[i
].val
.a
, tests
[i
].val
.b
, tests
[i
].val
.c
);
102 static void test_global_data_rdonly(struct bpf_object
*obj
, __u32 duration
)
104 int err
= -ENOMEM
, map_fd
, zero
= 0;
108 map
= bpf_object__find_map_by_name(obj
, "test_glo.rodata");
109 if (CHECK_FAIL(!map
|| !bpf_map__is_internal(map
)))
112 map_fd
= bpf_map__fd(map
);
113 if (CHECK_FAIL(map_fd
< 0))
116 buff
= malloc(bpf_map__def(map
)->value_size
);
118 err
= bpf_map_update_elem(map_fd
, &zero
, buff
, 0);
120 CHECK(!err
|| errno
!= EPERM
, "test .rodata read-only map",
121 "err %d errno %d\n", err
, errno
);
124 void test_global_data(void)
126 const char *file
= "./test_global_data.o";
127 __u32 duration
= 0, retval
;
128 struct bpf_object
*obj
;
131 err
= bpf_prog_load(file
, BPF_PROG_TYPE_SCHED_CLS
, &obj
, &prog_fd
);
132 if (CHECK(err
, "load program", "error %d loading %s\n", err
, file
))
135 err
= bpf_prog_test_run(prog_fd
, 1, &pkt_v4
, sizeof(pkt_v4
),
136 NULL
, NULL
, &retval
, &duration
);
137 CHECK(err
|| retval
, "pass global data run",
138 "err %d errno %d retval %d duration %d\n",
139 err
, errno
, retval
, duration
);
141 test_global_data_number(obj
, duration
);
142 test_global_data_string(obj
, duration
);
143 test_global_data_struct(obj
, duration
);
144 test_global_data_rdonly(obj
, duration
);
146 bpf_object__close(obj
);