1 /* eBPF mini library */
7 int bpf_create_map(enum bpf_map_type map_type
, int key_size
, int value_size
,
8 int max_entries
, int map_flags
);
9 int bpf_update_elem(int fd
, void *key
, void *value
, unsigned long long flags
);
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
);
14 int bpf_prog_load(enum bpf_prog_type prog_type
,
15 const struct bpf_insn
*insns
, int insn_len
,
16 const char *license
, int kern_version
);
18 int bpf_obj_pin(int fd
, const char *pathname
);
19 int bpf_obj_get(const char *pathname
);
21 #define LOG_BUF_SIZE 65536
22 extern char bpf_log_buf
[LOG_BUF_SIZE
];
24 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
26 #define BPF_ALU64_REG(OP, DST, SRC) \
27 ((struct bpf_insn) { \
28 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
34 #define BPF_ALU32_REG(OP, DST, SRC) \
35 ((struct bpf_insn) { \
36 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
42 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
44 #define BPF_ALU64_IMM(OP, DST, IMM) \
45 ((struct bpf_insn) { \
46 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
52 #define BPF_ALU32_IMM(OP, DST, IMM) \
53 ((struct bpf_insn) { \
54 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
60 /* Short form of mov, dst_reg = src_reg */
62 #define BPF_MOV64_REG(DST, SRC) \
63 ((struct bpf_insn) { \
64 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
70 #define BPF_MOV32_REG(DST, SRC) \
71 ((struct bpf_insn) { \
72 .code = BPF_ALU | BPF_MOV | BPF_X, \
78 /* Short form of mov, dst_reg = imm32 */
80 #define BPF_MOV64_IMM(DST, IMM) \
81 ((struct bpf_insn) { \
82 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
88 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
89 #define BPF_LD_IMM64(DST, IMM) \
90 BPF_LD_IMM64_RAW(DST, 0, IMM)
92 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
93 ((struct bpf_insn) { \
94 .code = BPF_LD | BPF_DW | BPF_IMM, \
98 .imm = (__u32) (IMM) }), \
99 ((struct bpf_insn) { \
100 .code = 0, /* zero is reserved opcode */ \
104 .imm = ((__u64) (IMM)) >> 32 })
106 #ifndef BPF_PSEUDO_MAP_FD
107 # define BPF_PSEUDO_MAP_FD 1
110 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
111 #define BPF_LD_MAP_FD(DST, MAP_FD) \
112 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
115 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
117 #define BPF_LD_ABS(SIZE, IMM) \
118 ((struct bpf_insn) { \
119 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
125 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
127 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
128 ((struct bpf_insn) { \
129 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
135 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
137 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
138 ((struct bpf_insn) { \
139 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
145 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
147 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
148 ((struct bpf_insn) { \
149 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
155 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
157 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
158 ((struct bpf_insn) { \
159 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
165 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
167 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
168 ((struct bpf_insn) { \
169 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
175 /* Raw code statement block */
177 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
178 ((struct bpf_insn) { \
187 #define BPF_EXIT_INSN() \
188 ((struct bpf_insn) { \
189 .code = BPF_JMP | BPF_EXIT, \
195 /* create RAW socket and bind to interface 'name' */
196 int open_raw_sock(const char *name
);
198 struct perf_event_attr
;
199 int perf_event_open(struct perf_event_attr
*attr
, int pid
, int cpu
,
200 int group_fd
, unsigned long flags
);