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