]>
git.proxmox.com Git - mirror_iproute2.git/blob - include/bpf_util.h
2 * bpf_util.h BPF common code
4 * This program is free software; you can distribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Daniel Borkmann <daniel@iogearbox.net>
10 * Jiri Pirko <jiri@resnulli.us>
16 #include <linux/bpf.h>
17 #include <linux/filter.h>
18 #include <linux/magic.h>
19 #include <linux/elf-em.h>
20 #include <linux/if_alg.h>
25 #define BPF_ENV_UDS "TC_BPF_UDS"
26 #define BPF_ENV_MNT "TC_BPF_MNT"
29 # define BPF_MAX_LOG 4096
32 #define BPF_DIR_GLOBALS "globals"
35 # define BPF_FS_MAGIC 0xcafe4a11
38 #define BPF_DIR_MNT "/sys/fs/bpf"
41 # define TRACEFS_MAGIC 0x74726163
44 #define TRACE_DIR_MNT "/sys/kernel/tracing"
55 void (*cbpf_cb
)(void *nl
, const struct sock_filter
*ops
, int ops_len
);
56 void (*ebpf_cb
)(void *nl
, int fd
, const char *annotation
);
65 struct sock_filter
*ops
;
68 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
70 #define BPF_ALU64_REG(OP, DST, SRC) \
71 ((struct bpf_insn) { \
72 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
78 #define BPF_ALU32_REG(OP, DST, SRC) \
79 ((struct bpf_insn) { \
80 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
86 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
88 #define BPF_ALU64_IMM(OP, DST, IMM) \
89 ((struct bpf_insn) { \
90 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
96 #define BPF_ALU32_IMM(OP, DST, IMM) \
97 ((struct bpf_insn) { \
98 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
104 /* Short form of mov, dst_reg = src_reg */
106 #define BPF_MOV64_REG(DST, SRC) \
107 ((struct bpf_insn) { \
108 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
114 #define BPF_MOV32_REG(DST, SRC) \
115 ((struct bpf_insn) { \
116 .code = BPF_ALU | BPF_MOV | BPF_X, \
122 /* Short form of mov, dst_reg = imm32 */
124 #define BPF_MOV64_IMM(DST, IMM) \
125 ((struct bpf_insn) { \
126 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
132 #define BPF_MOV32_IMM(DST, IMM) \
133 ((struct bpf_insn) { \
134 .code = BPF_ALU | BPF_MOV | BPF_K, \
140 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
141 #define BPF_LD_IMM64(DST, IMM) \
142 BPF_LD_IMM64_RAW(DST, 0, IMM)
144 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
145 ((struct bpf_insn) { \
146 .code = BPF_LD | BPF_DW | BPF_IMM, \
150 .imm = (__u32) (IMM) }), \
151 ((struct bpf_insn) { \
152 .code = 0, /* zero is reserved opcode */ \
156 .imm = ((__u64) (IMM)) >> 32 })
158 #ifndef BPF_PSEUDO_MAP_FD
159 # define BPF_PSEUDO_MAP_FD 1
162 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
163 #define BPF_LD_MAP_FD(DST, MAP_FD) \
164 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
167 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
169 #define BPF_LD_ABS(SIZE, IMM) \
170 ((struct bpf_insn) { \
171 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
177 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
179 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
180 ((struct bpf_insn) { \
181 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
187 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
189 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
190 ((struct bpf_insn) { \
191 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
197 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
199 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
200 ((struct bpf_insn) { \
201 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
207 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
209 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
210 ((struct bpf_insn) { \
211 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
217 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
219 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
220 ((struct bpf_insn) { \
221 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
227 /* Raw code statement block */
229 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
230 ((struct bpf_insn) { \
239 #define BPF_EXIT_INSN() \
240 ((struct bpf_insn) { \
241 .code = BPF_JMP | BPF_EXIT, \
247 int bpf_parse_common(enum bpf_prog_type type
, struct bpf_cfg_in
*cfg
,
248 const struct bpf_cfg_ops
*ops
, void *nl
);
250 const char *bpf_prog_to_default_section(enum bpf_prog_type type
);
252 int bpf_graft_map(const char *map_path
, uint32_t *key
, int argc
, char **argv
);
253 int bpf_trace_pipe(void);
255 void bpf_print_ops(FILE *f
, struct rtattr
*bpf_ops
, __u16 len
);
257 int bpf_prog_load(enum bpf_prog_type type
, const struct bpf_insn
*insns
,
258 size_t size_insns
, const char *license
, char *log
,
261 int bpf_prog_attach_fd(int prog_fd
, int target_fd
, enum bpf_attach_type type
);
262 int bpf_prog_detach_fd(int target_fd
, enum bpf_attach_type type
);
264 int bpf_dump_prog_info(FILE *f
, uint32_t id
);
267 int bpf_send_map_fds(const char *path
, const char *obj
);
268 int bpf_recv_map_fds(const char *path
, int *fds
, struct bpf_map_aux
*aux
,
269 unsigned int entries
);
271 static inline int bpf_send_map_fds(const char *path
, const char *obj
)
276 static inline int bpf_recv_map_fds(const char *path
, int *fds
,
277 struct bpf_map_aux
*aux
,
278 unsigned int entries
)
282 #endif /* HAVE_ELF */
283 #endif /* __BPF_UTIL__ */