1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
4 #include "test_progs.h"
5 #include "bpf_rlimit.h"
7 int error_cnt, pass_cnt;
9 bool verifier_stats = false;
11 struct ipv4_packet pkt_v4 = {
12 .eth.h_proto = __bpf_constant_htons(ETH_P_IP),
14 .iph.protocol = IPPROTO_TCP,
15 .iph.tot_len = __bpf_constant_htons(MAGIC_BYTES),
20 struct ipv6_packet pkt_v6 = {
21 .eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
22 .iph.nexthdr = IPPROTO_TCP,
23 .iph.payload_len = __bpf_constant_htons(MAGIC_BYTES),
28 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
32 map = bpf_object__find_map_by_name(obj, name);
34 printf("%s:FAIL:map '%s' not found\n", test, name);
38 return bpf_map__fd(map);
41 static bool is_jit_enabled(void)
43 const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable";
47 sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
48 if (sysctl_fd != -1) {
51 if (read(sysctl_fd, &tmpc, sizeof(tmpc)) == 1)
52 enabled = (tmpc != '0');
59 int compare_map_keys(int map1_fd, int map2_fd)
62 char val_buf[PERF_MAX_STACK_DEPTH *
63 sizeof(struct bpf_stack_build_id)];
66 err = bpf_map_get_next_key(map1_fd, NULL, &key);
69 err = bpf_map_lookup_elem(map2_fd, &key, val_buf);
73 while (bpf_map_get_next_key(map1_fd, &key, &next_key) == 0) {
74 err = bpf_map_lookup_elem(map2_fd, &next_key, val_buf);
86 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
88 __u32 key, next_key, *cur_key_p, *next_key_p;
89 char *val_buf1, *val_buf2;
92 val_buf1 = malloc(stack_trace_len);
93 val_buf2 = malloc(stack_trace_len);
96 while (bpf_map_get_next_key(smap_fd, cur_key_p, next_key_p) == 0) {
97 err = bpf_map_lookup_elem(smap_fd, next_key_p, val_buf1);
100 err = bpf_map_lookup_elem(amap_fd, next_key_p, val_buf2);
103 for (i = 0; i < stack_trace_len; i++) {
104 if (val_buf1[i] != val_buf2[i]) {
111 next_key_p = &next_key;
122 int extract_build_id(char *build_id, size_t size)
128 fp = popen("readelf -n ./urandom_read | grep 'Build ID'", "r");
132 if (getline(&line, &len, fp) == -1)
138 memcpy(build_id, line, len);
139 build_id[len] = '\0';
146 void *spin_lock_thread(void *arg)
148 __u32 duration, retval;
149 int err, prog_fd = *(u32 *) arg;
151 err = bpf_prog_test_run(prog_fd, 10000, &pkt_v4, sizeof(pkt_v4),
152 NULL, NULL, &retval, &duration);
153 CHECK(err || retval, "",
154 "err %d errno %d retval %d duration %d\n",
155 err, errno, retval, duration);
160 #include <prog_tests/tests.h>
163 int main(int ac, char **av)
167 jit_enabled = is_jit_enabled();
169 if (ac == 2 && strcmp(av[1], "-s") == 0)
170 verifier_stats = true;
173 #include <prog_tests/tests.h>
176 printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
177 return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;