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