1 /* eBPF mini library */
7 int bpf_create_map(enum bpf_map_type map_type
, int key_size
, int value_size
,
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
,
18 #define LOG_BUF_SIZE 65536
19 extern char bpf_log_buf
[LOG_BUF_SIZE
];
21 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
23 #define BPF_ALU64_REG(OP, DST, SRC) \
24 ((struct bpf_insn) { \
25 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
31 #define BPF_ALU32_REG(OP, DST, SRC) \
32 ((struct bpf_insn) { \
33 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
39 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
41 #define BPF_ALU64_IMM(OP, DST, IMM) \
42 ((struct bpf_insn) { \
43 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
49 #define BPF_ALU32_IMM(OP, DST, IMM) \
50 ((struct bpf_insn) { \
51 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
57 /* Short form of mov, dst_reg = src_reg */
59 #define BPF_MOV64_REG(DST, SRC) \
60 ((struct bpf_insn) { \
61 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
67 /* Short form of mov, dst_reg = imm32 */
69 #define BPF_MOV64_IMM(DST, IMM) \
70 ((struct bpf_insn) { \
71 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
77 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
78 #define BPF_LD_IMM64(DST, IMM) \
79 BPF_LD_IMM64_RAW(DST, 0, IMM)
81 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
82 ((struct bpf_insn) { \
83 .code = BPF_LD | BPF_DW | BPF_IMM, \
87 .imm = (__u32) (IMM) }), \
88 ((struct bpf_insn) { \
89 .code = 0, /* zero is reserved opcode */ \
93 .imm = ((__u64) (IMM)) >> 32 })
95 #define BPF_PSEUDO_MAP_FD 1
97 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
98 #define BPF_LD_MAP_FD(DST, MAP_FD) \
99 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
102 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
104 #define BPF_LD_ABS(SIZE, IMM) \
105 ((struct bpf_insn) { \
106 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
112 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
114 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
115 ((struct bpf_insn) { \
116 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
122 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
124 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
125 ((struct bpf_insn) { \
126 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
132 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
134 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
135 ((struct bpf_insn) { \
136 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
142 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
144 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
145 ((struct bpf_insn) { \
146 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
152 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
154 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
155 ((struct bpf_insn) { \
156 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
162 /* Raw code statement block */
164 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
165 ((struct bpf_insn) { \
174 #define BPF_EXIT_INSN() \
175 ((struct bpf_insn) { \
176 .code = BPF_JMP | BPF_EXIT, \
182 /* create RAW socket and bind to interface 'name' */
183 int open_raw_sock(const char *name
);