1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
4 #include "test_progs.h"
5 #include "bpf_rlimit.h"
8 int error_cnt, pass_cnt;
10 bool verifier_stats = false;
12 struct ipv4_packet pkt_v4 = {
13 .eth.h_proto = __bpf_constant_htons(ETH_P_IP),
15 .iph.protocol = IPPROTO_TCP,
16 .iph.tot_len = __bpf_constant_htons(MAGIC_BYTES),
21 struct ipv6_packet pkt_v6 = {
22 .eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
23 .iph.nexthdr = IPPROTO_TCP,
24 .iph.payload_len = __bpf_constant_htons(MAGIC_BYTES),
29 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
33 map = bpf_object__find_map_by_name(obj, name);
35 printf("%s:FAIL:map '%s' not found\n", test, name);
39 return bpf_map__fd(map);
42 static bool is_jit_enabled(void)
44 const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable";
48 sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
49 if (sysctl_fd != -1) {
52 if (read(sysctl_fd, &tmpc, sizeof(tmpc)) == 1)
53 enabled = (tmpc != '0');
60 int compare_map_keys(int map1_fd, int map2_fd)
63 char val_buf[PERF_MAX_STACK_DEPTH *
64 sizeof(struct bpf_stack_build_id)];
67 err = bpf_map_get_next_key(map1_fd, NULL, &key);
70 err = bpf_map_lookup_elem(map2_fd, &key, val_buf);
74 while (bpf_map_get_next_key(map1_fd, &key, &next_key) == 0) {
75 err = bpf_map_lookup_elem(map2_fd, &next_key, val_buf);
87 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
89 __u32 key, next_key, *cur_key_p, *next_key_p;
90 char *val_buf1, *val_buf2;
93 val_buf1 = malloc(stack_trace_len);
94 val_buf2 = malloc(stack_trace_len);
97 while (bpf_map_get_next_key(smap_fd, cur_key_p, next_key_p) == 0) {
98 err = bpf_map_lookup_elem(smap_fd, next_key_p, val_buf1);
101 err = bpf_map_lookup_elem(amap_fd, next_key_p, val_buf2);
104 for (i = 0; i < stack_trace_len; i++) {
105 if (val_buf1[i] != val_buf2[i]) {
112 next_key_p = &next_key;
123 int extract_build_id(char *build_id, size_t size)
129 fp = popen("readelf -n ./urandom_read | grep 'Build ID'", "r");
133 if (getline(&line, &len, fp) == -1)
139 memcpy(build_id, line, len);
140 build_id[len] = '\0';
147 void *spin_lock_thread(void *arg)
149 __u32 duration, retval;
150 int err, prog_fd = *(u32 *) arg;
152 err = bpf_prog_test_run(prog_fd, 10000, &pkt_v4, sizeof(pkt_v4),
153 NULL, NULL, &retval, &duration);
154 CHECK(err || retval, "",
155 "err %d errno %d retval %d duration %d\n",
156 err, errno, retval, duration);
160 /* extern declarations for test funcs */
161 #define DEFINE_TEST(name) extern void test_##name();
162 #include <prog_tests/tests.h>
165 struct prog_test_def {
166 const char *test_name;
167 void (*run_test)(void);
170 static struct prog_test_def prog_test_defs[] = {
171 #define DEFINE_TEST(name) { \
172 .test_name = #name, \
173 .run_test = &test_##name, \
175 #include <prog_tests/tests.h>
179 const char *argp_program_version = "test_progs 0.1";
180 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
181 const char argp_program_doc[] = "BPF selftests test runner";
184 ARG_VERIFIER_STATS = 's',
187 static const struct argp_option opts[] = {
188 { "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
189 "Output verifier statistics", },
197 static struct test_env env = {};
199 static error_t parse_arg(int key, char *arg, struct argp_state *state)
201 struct test_env *env = state->input;
204 case ARG_VERIFIER_STATS:
205 env->verifier_stats = true;
213 return ARGP_ERR_UNKNOWN;
219 int main(int argc, char **argv)
221 static const struct argp argp = {
224 .doc = argp_program_doc,
226 const struct prog_test_def *def;
229 err = argp_parse(&argp, argc, argv, 0, NULL, &env);
235 jit_enabled = is_jit_enabled();
237 verifier_stats = env.verifier_stats;
239 for (i = 0; i < ARRAY_SIZE(prog_test_defs); i++) {
240 def = &prog_test_defs[i];
244 printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
245 return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;