1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#ifndef __BPF_LIBBPF_H
24#define __BPF_LIBBPF_H
25
26#include <stdio.h>
27#include <stdint.h>
28#include <stdbool.h>
29#include <sys/types.h>
30#include <linux/bpf.h>
31
32enum libbpf_errno {
33 __LIBBPF_ERRNO__START = 4000,
34
35
36 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
37 LIBBPF_ERRNO__FORMAT,
38 LIBBPF_ERRNO__KVERSION,
39 LIBBPF_ERRNO__ENDIAN,
40 LIBBPF_ERRNO__INTERNAL,
41 LIBBPF_ERRNO__RELOC,
42 LIBBPF_ERRNO__LOAD,
43 LIBBPF_ERRNO__VERIFY,
44 LIBBPF_ERRNO__PROG2BIG,
45 LIBBPF_ERRNO__KVER,
46 LIBBPF_ERRNO__PROGTYPE,
47 LIBBPF_ERRNO__WRNGPID,
48 LIBBPF_ERRNO__INVSEQ,
49 __LIBBPF_ERRNO__END,
50};
51
52int libbpf_strerror(int err, char *buf, size_t size);
53
54
55
56
57
58
59typedef int (*libbpf_print_fn_t)(const char *, ...)
60 __attribute__((format(printf, 1, 2)));
61
62void libbpf_set_print(libbpf_print_fn_t warn,
63 libbpf_print_fn_t info,
64 libbpf_print_fn_t debug);
65
66
67struct bpf_object;
68
69struct bpf_object *bpf_object__open(const char *path);
70struct bpf_object *bpf_object__open_buffer(void *obj_buf,
71 size_t obj_buf_sz,
72 const char *name);
73int bpf_object__pin(struct bpf_object *object, const char *path);
74void bpf_object__close(struct bpf_object *object);
75
76
77int bpf_object__load(struct bpf_object *obj);
78int bpf_object__unload(struct bpf_object *obj);
79const char *bpf_object__name(struct bpf_object *obj);
80unsigned int bpf_object__kversion(struct bpf_object *obj);
81
82struct bpf_object *bpf_object__next(struct bpf_object *prev);
83#define bpf_object__for_each_safe(pos, tmp) \
84 for ((pos) = bpf_object__next(NULL), \
85 (tmp) = bpf_object__next(pos); \
86 (pos) != NULL; \
87 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
88
89typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
90int bpf_object__set_priv(struct bpf_object *obj, void *priv,
91 bpf_object_clear_priv_t clear_priv);
92void *bpf_object__priv(struct bpf_object *prog);
93
94
95struct bpf_program;
96struct bpf_program *bpf_program__next(struct bpf_program *prog,
97 struct bpf_object *obj);
98
99#define bpf_object__for_each_program(pos, obj) \
100 for ((pos) = bpf_program__next(NULL, (obj)); \
101 (pos) != NULL; \
102 (pos) = bpf_program__next((pos), (obj)))
103
104typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
105 void *);
106
107int bpf_program__set_priv(struct bpf_program *prog, void *priv,
108 bpf_program_clear_priv_t clear_priv);
109
110void *bpf_program__priv(struct bpf_program *prog);
111
112const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
113
114int bpf_program__fd(struct bpf_program *prog);
115int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
116 int instance);
117int bpf_program__pin(struct bpf_program *prog, const char *path);
118
119struct bpf_insn;
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150struct bpf_prog_prep_result {
151
152
153
154
155 struct bpf_insn *new_insn_ptr;
156 int new_insn_cnt;
157
158
159 int *pfd;
160};
161
162
163
164
165
166
167
168
169
170
171
172
173
174typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
175 struct bpf_insn *insns, int insns_cnt,
176 struct bpf_prog_prep_result *res);
177
178int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
179 bpf_program_prep_t prep);
180
181int bpf_program__nth_fd(struct bpf_program *prog, int n);
182
183
184
185
186int bpf_program__set_socket_filter(struct bpf_program *prog);
187int bpf_program__set_tracepoint(struct bpf_program *prog);
188int bpf_program__set_kprobe(struct bpf_program *prog);
189int bpf_program__set_sched_cls(struct bpf_program *prog);
190int bpf_program__set_sched_act(struct bpf_program *prog);
191int bpf_program__set_xdp(struct bpf_program *prog);
192int bpf_program__set_perf_event(struct bpf_program *prog);
193void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);
194
195bool bpf_program__is_socket_filter(struct bpf_program *prog);
196bool bpf_program__is_tracepoint(struct bpf_program *prog);
197bool bpf_program__is_kprobe(struct bpf_program *prog);
198bool bpf_program__is_sched_cls(struct bpf_program *prog);
199bool bpf_program__is_sched_act(struct bpf_program *prog);
200bool bpf_program__is_xdp(struct bpf_program *prog);
201bool bpf_program__is_perf_event(struct bpf_program *prog);
202
203
204
205
206
207
208
209struct bpf_map_def {
210 unsigned int type;
211 unsigned int key_size;
212 unsigned int value_size;
213 unsigned int max_entries;
214 unsigned int map_flags;
215};
216
217
218
219
220
221struct bpf_map;
222struct bpf_map *
223bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
224
225
226
227
228
229struct bpf_map *
230bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
231
232struct bpf_map *
233bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
234#define bpf_map__for_each(pos, obj) \
235 for ((pos) = bpf_map__next(NULL, (obj)); \
236 (pos) != NULL; \
237 (pos) = bpf_map__next((pos), (obj)))
238
239int bpf_map__fd(struct bpf_map *map);
240const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
241const char *bpf_map__name(struct bpf_map *map);
242
243typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
244int bpf_map__set_priv(struct bpf_map *map, void *priv,
245 bpf_map_clear_priv_t clear_priv);
246void *bpf_map__priv(struct bpf_map *map);
247int bpf_map__pin(struct bpf_map *map, const char *path);
248
249long libbpf_get_error(const void *ptr);
250
251struct bpf_prog_load_attr {
252 const char *file;
253 enum bpf_prog_type prog_type;
254 enum bpf_attach_type expected_attach_type;
255};
256
257int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
258 struct bpf_object **pobj, int *prog_fd);
259int bpf_prog_load(const char *file, enum bpf_prog_type type,
260 struct bpf_object **pobj, int *prog_fd);
261
262int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
263#endif
264