Alexei Starovoitov | 3c731eb | 2014-09-26 00:17:07 -0700 | [diff] [blame] | 1 | /* eBPF mini library */ |
| 2 | #ifndef __LIBBPF_H |
| 3 | #define __LIBBPF_H |
| 4 | |
| 5 | struct bpf_insn; |
| 6 | |
| 7 | int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, |
Alexei Starovoitov | 89b9760 | 2016-03-07 21:57:20 -0800 | [diff] [blame] | 8 | int max_entries, int map_flags); |
Alexei Starovoitov | ffb65f2 | 2014-11-13 17:36:48 -0800 | [diff] [blame] | 9 | int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags); |
Alexei Starovoitov | 3c731eb | 2014-09-26 00:17:07 -0700 | [diff] [blame] | 10 | int bpf_lookup_elem(int fd, void *key, void *value); |
| 11 | int bpf_delete_elem(int fd, void *key); |
| 12 | int bpf_get_next_key(int fd, void *key, void *next_key); |
| 13 | |
| 14 | int bpf_prog_load(enum bpf_prog_type prog_type, |
| 15 | const struct bpf_insn *insns, int insn_len, |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 16 | const char *license, int kern_version); |
Alexei Starovoitov | 3c731eb | 2014-09-26 00:17:07 -0700 | [diff] [blame] | 17 | |
Daniel Borkmann | 42984d7 | 2015-10-29 14:58:10 +0100 | [diff] [blame] | 18 | int bpf_obj_pin(int fd, const char *pathname); |
| 19 | int bpf_obj_get(const char *pathname); |
| 20 | |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 21 | #define LOG_BUF_SIZE 65536 |
Alexei Starovoitov | 3c731eb | 2014-09-26 00:17:07 -0700 | [diff] [blame] | 22 | extern char bpf_log_buf[LOG_BUF_SIZE]; |
| 23 | |
| 24 | /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ |
| 25 | |
| 26 | #define BPF_ALU64_REG(OP, DST, SRC) \ |
| 27 | ((struct bpf_insn) { \ |
| 28 | .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ |
| 29 | .dst_reg = DST, \ |
| 30 | .src_reg = SRC, \ |
| 31 | .off = 0, \ |
| 32 | .imm = 0 }) |
| 33 | |
| 34 | #define BPF_ALU32_REG(OP, DST, SRC) \ |
| 35 | ((struct bpf_insn) { \ |
| 36 | .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ |
| 37 | .dst_reg = DST, \ |
| 38 | .src_reg = SRC, \ |
| 39 | .off = 0, \ |
| 40 | .imm = 0 }) |
| 41 | |
| 42 | /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ |
| 43 | |
| 44 | #define BPF_ALU64_IMM(OP, DST, IMM) \ |
| 45 | ((struct bpf_insn) { \ |
| 46 | .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \ |
| 47 | .dst_reg = DST, \ |
| 48 | .src_reg = 0, \ |
| 49 | .off = 0, \ |
| 50 | .imm = IMM }) |
| 51 | |
| 52 | #define BPF_ALU32_IMM(OP, DST, IMM) \ |
| 53 | ((struct bpf_insn) { \ |
| 54 | .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ |
| 55 | .dst_reg = DST, \ |
| 56 | .src_reg = 0, \ |
| 57 | .off = 0, \ |
| 58 | .imm = IMM }) |
| 59 | |
| 60 | /* Short form of mov, dst_reg = src_reg */ |
| 61 | |
| 62 | #define BPF_MOV64_REG(DST, SRC) \ |
| 63 | ((struct bpf_insn) { \ |
| 64 | .code = BPF_ALU64 | BPF_MOV | BPF_X, \ |
| 65 | .dst_reg = DST, \ |
| 66 | .src_reg = SRC, \ |
| 67 | .off = 0, \ |
| 68 | .imm = 0 }) |
| 69 | |
Alexei Starovoitov | bf50887 | 2015-10-07 22:23:23 -0700 | [diff] [blame] | 70 | #define BPF_MOV32_REG(DST, SRC) \ |
| 71 | ((struct bpf_insn) { \ |
| 72 | .code = BPF_ALU | BPF_MOV | BPF_X, \ |
| 73 | .dst_reg = DST, \ |
| 74 | .src_reg = SRC, \ |
| 75 | .off = 0, \ |
| 76 | .imm = 0 }) |
| 77 | |
Alexei Starovoitov | 3c731eb | 2014-09-26 00:17:07 -0700 | [diff] [blame] | 78 | /* Short form of mov, dst_reg = imm32 */ |
| 79 | |
| 80 | #define BPF_MOV64_IMM(DST, IMM) \ |
| 81 | ((struct bpf_insn) { \ |
| 82 | .code = BPF_ALU64 | BPF_MOV | BPF_K, \ |
| 83 | .dst_reg = DST, \ |
| 84 | .src_reg = 0, \ |
| 85 | .off = 0, \ |
| 86 | .imm = IMM }) |
| 87 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 88 | #define BPF_MOV32_IMM(DST, IMM) \ |
| 89 | ((struct bpf_insn) { \ |
| 90 | .code = BPF_ALU | BPF_MOV | BPF_K, \ |
| 91 | .dst_reg = DST, \ |
| 92 | .src_reg = 0, \ |
| 93 | .off = 0, \ |
| 94 | .imm = IMM }) |
| 95 | |
Alexei Starovoitov | 3c731eb | 2014-09-26 00:17:07 -0700 | [diff] [blame] | 96 | /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */ |
| 97 | #define BPF_LD_IMM64(DST, IMM) \ |
| 98 | BPF_LD_IMM64_RAW(DST, 0, IMM) |
| 99 | |
| 100 | #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \ |
| 101 | ((struct bpf_insn) { \ |
| 102 | .code = BPF_LD | BPF_DW | BPF_IMM, \ |
| 103 | .dst_reg = DST, \ |
| 104 | .src_reg = SRC, \ |
| 105 | .off = 0, \ |
| 106 | .imm = (__u32) (IMM) }), \ |
| 107 | ((struct bpf_insn) { \ |
| 108 | .code = 0, /* zero is reserved opcode */ \ |
| 109 | .dst_reg = 0, \ |
| 110 | .src_reg = 0, \ |
| 111 | .off = 0, \ |
| 112 | .imm = ((__u64) (IMM)) >> 32 }) |
| 113 | |
Daniel Borkmann | f1a66f8 | 2015-03-01 12:31:43 +0100 | [diff] [blame] | 114 | #ifndef BPF_PSEUDO_MAP_FD |
| 115 | # define BPF_PSEUDO_MAP_FD 1 |
| 116 | #endif |
Alexei Starovoitov | 3c731eb | 2014-09-26 00:17:07 -0700 | [diff] [blame] | 117 | |
| 118 | /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */ |
| 119 | #define BPF_LD_MAP_FD(DST, MAP_FD) \ |
| 120 | BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD) |
| 121 | |
| 122 | |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 123 | /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ |
| 124 | |
| 125 | #define BPF_LD_ABS(SIZE, IMM) \ |
| 126 | ((struct bpf_insn) { \ |
| 127 | .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ |
| 128 | .dst_reg = 0, \ |
| 129 | .src_reg = 0, \ |
| 130 | .off = 0, \ |
| 131 | .imm = IMM }) |
| 132 | |
Alexei Starovoitov | 3c731eb | 2014-09-26 00:17:07 -0700 | [diff] [blame] | 133 | /* Memory load, dst_reg = *(uint *) (src_reg + off16) */ |
| 134 | |
| 135 | #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \ |
| 136 | ((struct bpf_insn) { \ |
| 137 | .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ |
| 138 | .dst_reg = DST, \ |
| 139 | .src_reg = SRC, \ |
| 140 | .off = OFF, \ |
| 141 | .imm = 0 }) |
| 142 | |
| 143 | /* Memory store, *(uint *) (dst_reg + off16) = src_reg */ |
| 144 | |
| 145 | #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \ |
| 146 | ((struct bpf_insn) { \ |
| 147 | .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ |
| 148 | .dst_reg = DST, \ |
| 149 | .src_reg = SRC, \ |
| 150 | .off = OFF, \ |
| 151 | .imm = 0 }) |
| 152 | |
| 153 | /* Memory store, *(uint *) (dst_reg + off16) = imm32 */ |
| 154 | |
| 155 | #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \ |
| 156 | ((struct bpf_insn) { \ |
| 157 | .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \ |
| 158 | .dst_reg = DST, \ |
| 159 | .src_reg = 0, \ |
| 160 | .off = OFF, \ |
| 161 | .imm = IMM }) |
| 162 | |
| 163 | /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */ |
| 164 | |
| 165 | #define BPF_JMP_REG(OP, DST, SRC, OFF) \ |
| 166 | ((struct bpf_insn) { \ |
| 167 | .code = BPF_JMP | BPF_OP(OP) | BPF_X, \ |
| 168 | .dst_reg = DST, \ |
| 169 | .src_reg = SRC, \ |
| 170 | .off = OFF, \ |
| 171 | .imm = 0 }) |
| 172 | |
| 173 | /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ |
| 174 | |
| 175 | #define BPF_JMP_IMM(OP, DST, IMM, OFF) \ |
| 176 | ((struct bpf_insn) { \ |
| 177 | .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ |
| 178 | .dst_reg = DST, \ |
| 179 | .src_reg = 0, \ |
| 180 | .off = OFF, \ |
| 181 | .imm = IMM }) |
| 182 | |
| 183 | /* Raw code statement block */ |
| 184 | |
| 185 | #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \ |
| 186 | ((struct bpf_insn) { \ |
| 187 | .code = CODE, \ |
| 188 | .dst_reg = DST, \ |
| 189 | .src_reg = SRC, \ |
| 190 | .off = OFF, \ |
| 191 | .imm = IMM }) |
| 192 | |
| 193 | /* Program exit */ |
| 194 | |
| 195 | #define BPF_EXIT_INSN() \ |
| 196 | ((struct bpf_insn) { \ |
| 197 | .code = BPF_JMP | BPF_EXIT, \ |
| 198 | .dst_reg = 0, \ |
| 199 | .src_reg = 0, \ |
| 200 | .off = 0, \ |
| 201 | .imm = 0 }) |
| 202 | |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 203 | /* create RAW socket and bind to interface 'name' */ |
| 204 | int open_raw_sock(const char *name); |
| 205 | |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 206 | struct perf_event_attr; |
| 207 | int perf_event_open(struct perf_event_attr *attr, int pid, int cpu, |
| 208 | int group_fd, unsigned long flags); |
Alexei Starovoitov | 3c731eb | 2014-09-26 00:17:07 -0700 | [diff] [blame] | 209 | #endif |