Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame^] | 1 | #ifdef HAVE_CONFIG_H |
| 2 | # include "config.h" |
| 3 | #endif |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdint.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/syscall.h> |
| 9 | |
| 10 | #if defined HAVE_LINUX_BPF_H && defined __NR_bpf |
| 11 | # include <linux/bpf.h> |
| 12 | |
| 13 | static const struct bpf_insn insns[] = { |
| 14 | { .code = BPF_JMP | BPF_EXIT } |
| 15 | }; |
| 16 | |
| 17 | static char log_buf[4096]; |
| 18 | |
| 19 | static int |
| 20 | map_create(void) |
| 21 | { |
| 22 | union bpf_attr attr = { |
| 23 | .key_size = 4, |
| 24 | .value_size = 8, |
| 25 | .max_entries = 256 |
| 26 | }; |
| 27 | return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); |
| 28 | } |
| 29 | |
| 30 | static int |
| 31 | map_any(int cmd) |
| 32 | { |
| 33 | union bpf_attr attr = { |
| 34 | .map_fd = -1, |
| 35 | .key = 0xdeadbeef, |
| 36 | .value = 0xbadc0ded |
| 37 | }; |
| 38 | return syscall(__NR_bpf, cmd, &attr, sizeof(attr)); |
| 39 | } |
| 40 | |
| 41 | static int |
| 42 | prog_load(void) |
| 43 | { |
| 44 | union bpf_attr attr = { |
| 45 | .insn_cnt = sizeof(insns) / sizeof(insns[0]), |
| 46 | .insns = (unsigned long) insns, |
| 47 | .license = (unsigned long) "GPL", |
| 48 | .log_level = 42, |
| 49 | .log_size = sizeof(log_buf), |
| 50 | .log_buf = (unsigned long) log_buf |
| 51 | }; |
| 52 | return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); |
| 53 | } |
| 54 | |
| 55 | int |
| 56 | main(void) |
| 57 | { |
| 58 | if (!map_create()) |
| 59 | return 77; |
| 60 | printf("bpf\\(BPF_MAP_CREATE, " |
| 61 | "\\{map_type=BPF_MAP_TYPE_UNSPEC, key_size=4, value_size=8, max_entries=256\\}, " |
| 62 | "%u\\) += -1 .*\n", |
| 63 | (unsigned) sizeof(union bpf_attr)); |
| 64 | |
| 65 | if (!map_any(BPF_MAP_LOOKUP_ELEM)) |
| 66 | return 77; |
| 67 | printf("bpf\\(BPF_MAP_LOOKUP_ELEM, " |
| 68 | "\\{map_fd=-1, key=0xdeadbeef\\}, %u\\) += -1 .*\n", |
| 69 | (unsigned) sizeof(union bpf_attr)); |
| 70 | |
| 71 | if (!map_any(BPF_MAP_UPDATE_ELEM)) |
| 72 | return 77; |
| 73 | printf("bpf\\(BPF_MAP_UPDATE_ELEM, " |
| 74 | "\\{map_fd=-1, key=0xdeadbeef, value=0xbadc0ded, flags=BPF_ANY\\}, " |
| 75 | "%u\\) += -1 .*\n", |
| 76 | (unsigned) sizeof(union bpf_attr)); |
| 77 | |
| 78 | if (!map_any(BPF_MAP_DELETE_ELEM)) |
| 79 | return 77; |
| 80 | printf("bpf\\(BPF_MAP_DELETE_ELEM, " |
| 81 | "\\{map_fd=-1, key=0xdeadbeef\\}, %u\\) += -1 .*\n", |
| 82 | (unsigned) sizeof(union bpf_attr)); |
| 83 | |
| 84 | if (!map_any(BPF_MAP_GET_NEXT_KEY)) |
| 85 | return 77; |
| 86 | printf("bpf\\(BPF_MAP_GET_NEXT_KEY, " |
| 87 | "\\{map_fd=-1, key=0xdeadbeef\\}, %u\\) += -1 .*\n", |
| 88 | (unsigned) sizeof(union bpf_attr)); |
| 89 | |
| 90 | if (!prog_load()) |
| 91 | return 77; |
| 92 | printf("bpf\\(BPF_PROG_LOAD, " |
| 93 | "\\{prog_type=BPF_PROG_TYPE_UNSPEC, insn_cnt=1, insns=%p, " |
| 94 | "license=\"GPL\", log_level=42, log_size=4096, log_buf=%p, " |
| 95 | "kern_version=0\\}, %u\\) += -1 .*\n", |
| 96 | insns, log_buf, (unsigned) sizeof(union bpf_attr)); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | #else |
| 102 | |
| 103 | int |
| 104 | main(void) |
| 105 | { |
| 106 | return 77; |
| 107 | } |
| 108 | |
| 109 | #endif |