1
2
3
4
5
6
7
8
9
10#ifndef __LIBBPF_LIBBPF_H
11#define __LIBBPF_LIBBPF_H
12
13#include <stdarg.h>
14#include <stdio.h>
15#include <stdint.h>
16#include <stdbool.h>
17#include <sys/types.h>
18#include <linux/bpf.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#ifndef LIBBPF_API
25#define LIBBPF_API __attribute__((visibility("default")))
26#endif
27
28enum libbpf_errno {
29 __LIBBPF_ERRNO__START = 4000,
30
31
32 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
33 LIBBPF_ERRNO__FORMAT,
34 LIBBPF_ERRNO__KVERSION,
35 LIBBPF_ERRNO__ENDIAN,
36 LIBBPF_ERRNO__INTERNAL,
37 LIBBPF_ERRNO__RELOC,
38 LIBBPF_ERRNO__LOAD,
39 LIBBPF_ERRNO__VERIFY,
40 LIBBPF_ERRNO__PROG2BIG,
41 LIBBPF_ERRNO__KVER,
42 LIBBPF_ERRNO__PROGTYPE,
43 LIBBPF_ERRNO__WRNGPID,
44 LIBBPF_ERRNO__INVSEQ,
45 LIBBPF_ERRNO__NLPARSE,
46 __LIBBPF_ERRNO__END,
47};
48
49LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
50
51enum libbpf_print_level {
52 LIBBPF_WARN,
53 LIBBPF_INFO,
54 LIBBPF_DEBUG,
55};
56
57typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
58 const char *, va_list ap);
59
60LIBBPF_API void libbpf_set_print(libbpf_print_fn_t fn);
61
62
63struct bpf_object;
64
65struct bpf_object_open_attr {
66 const char *file;
67 enum bpf_prog_type prog_type;
68};
69
70LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
71LIBBPF_API struct bpf_object *
72bpf_object__open_xattr(struct bpf_object_open_attr *attr);
73struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
74 int flags);
75LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf,
76 size_t obj_buf_sz,
77 const char *name);
78int bpf_object__section_size(const struct bpf_object *obj, const char *name,
79 __u32 *size);
80int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
81 __u32 *off);
82LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
83LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
84 const char *path);
85LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
86 const char *path);
87LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
88 const char *path);
89LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
90LIBBPF_API void bpf_object__close(struct bpf_object *object);
91
92struct bpf_object_load_attr {
93 struct bpf_object *obj;
94 int log_level;
95};
96
97
98LIBBPF_API int bpf_object__load(struct bpf_object *obj);
99LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
100LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
101LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
102LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
103
104struct btf;
105LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
106LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
107
108LIBBPF_API struct bpf_program *
109bpf_object__find_program_by_title(const struct bpf_object *obj,
110 const char *title);
111
112LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
113#define bpf_object__for_each_safe(pos, tmp) \
114 for ((pos) = bpf_object__next(NULL), \
115 (tmp) = bpf_object__next(pos); \
116 (pos) != NULL; \
117 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
118
119typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
120LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
121 bpf_object_clear_priv_t clear_priv);
122LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
123
124LIBBPF_API int
125libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
126 enum bpf_attach_type *expected_attach_type);
127LIBBPF_API int libbpf_attach_type_by_name(const char *name,
128 enum bpf_attach_type *attach_type);
129
130
131struct bpf_program;
132LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
133 const struct bpf_object *obj);
134
135#define bpf_object__for_each_program(pos, obj) \
136 for ((pos) = bpf_program__next(NULL, (obj)); \
137 (pos) != NULL; \
138 (pos) = bpf_program__next((pos), (obj)))
139
140LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
141 const struct bpf_object *obj);
142
143typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
144
145LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
146 bpf_program_clear_priv_t clear_priv);
147
148LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
149LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
150 __u32 ifindex);
151
152LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog,
153 bool needs_copy);
154
155LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
156 __u32 kern_version);
157LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
158LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
159 const char *path,
160 int instance);
161LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
162 const char *path,
163 int instance);
164LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
165LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
166LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
167
168struct bpf_link;
169
170LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
171
172LIBBPF_API struct bpf_link *
173bpf_program__attach_perf_event(struct bpf_program *prog, int pfd);
174LIBBPF_API struct bpf_link *
175bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe,
176 const char *func_name);
177LIBBPF_API struct bpf_link *
178bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe,
179 pid_t pid, const char *binary_path,
180 size_t func_offset);
181LIBBPF_API struct bpf_link *
182bpf_program__attach_tracepoint(struct bpf_program *prog,
183 const char *tp_category,
184 const char *tp_name);
185LIBBPF_API struct bpf_link *
186bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
187 const char *tp_name);
188
189struct bpf_insn;
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220struct bpf_prog_prep_result {
221
222
223
224
225 struct bpf_insn *new_insn_ptr;
226 int new_insn_cnt;
227
228
229 int *pfd;
230};
231
232
233
234
235
236
237
238
239
240
241
242
243
244typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
245 struct bpf_insn *insns, int insns_cnt,
246 struct bpf_prog_prep_result *res);
247
248LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
249 bpf_program_prep_t prep);
250
251LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
252
253
254
255
256LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
257LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
258LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
259LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
260LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
261LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
262LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
263LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
264LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
265 enum bpf_prog_type type);
266LIBBPF_API void
267bpf_program__set_expected_attach_type(struct bpf_program *prog,
268 enum bpf_attach_type type);
269
270LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
271LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
272LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
273LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
274LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
275LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
276LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
277LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
278
279
280
281
282
283
284
285struct bpf_map_def {
286 unsigned int type;
287 unsigned int key_size;
288 unsigned int value_size;
289 unsigned int max_entries;
290 unsigned int map_flags;
291};
292
293
294
295
296
297struct bpf_map;
298LIBBPF_API struct bpf_map *
299bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
300
301LIBBPF_API int
302bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
303
304
305
306
307
308LIBBPF_API struct bpf_map *
309bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
310
311LIBBPF_API struct bpf_map *
312bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
313#define bpf_object__for_each_map(pos, obj) \
314 for ((pos) = bpf_map__next(NULL, (obj)); \
315 (pos) != NULL; \
316 (pos) = bpf_map__next((pos), (obj)))
317#define bpf_map__for_each bpf_object__for_each_map
318
319LIBBPF_API struct bpf_map *
320bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
321
322LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
323LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
324LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
325LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
326LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
327
328typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
329LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
330 bpf_map_clear_priv_t clear_priv);
331LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
332LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
333LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
334LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
335LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
336LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
337LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
338LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
339
340LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
341
342LIBBPF_API long libbpf_get_error(const void *ptr);
343
344struct bpf_prog_load_attr {
345 const char *file;
346 enum bpf_prog_type prog_type;
347 enum bpf_attach_type expected_attach_type;
348 int ifindex;
349 int log_level;
350 int prog_flags;
351};
352
353LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
354 struct bpf_object **pobj, int *prog_fd);
355LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
356 struct bpf_object **pobj, int *prog_fd);
357
358LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
359LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
360
361struct perf_buffer;
362
363typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
364 void *data, __u32 size);
365typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
366
367
368struct perf_buffer_opts {
369
370 perf_buffer_sample_fn sample_cb;
371
372 perf_buffer_lost_fn lost_cb;
373
374 void *ctx;
375};
376
377LIBBPF_API struct perf_buffer *
378perf_buffer__new(int map_fd, size_t page_cnt,
379 const struct perf_buffer_opts *opts);
380
381enum bpf_perf_event_ret {
382 LIBBPF_PERF_EVENT_DONE = 0,
383 LIBBPF_PERF_EVENT_ERROR = -1,
384 LIBBPF_PERF_EVENT_CONT = -2,
385};
386
387struct perf_event_header;
388
389typedef enum bpf_perf_event_ret
390(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
391
392
393struct perf_buffer_raw_opts {
394
395 struct perf_event_attr *attr;
396
397 perf_buffer_event_fn event_cb;
398
399 void *ctx;
400
401
402
403 int cpu_cnt;
404
405 int *cpus;
406
407 int *map_keys;
408};
409
410LIBBPF_API struct perf_buffer *
411perf_buffer__new_raw(int map_fd, size_t page_cnt,
412 const struct perf_buffer_raw_opts *opts);
413
414LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
415LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
416
417typedef enum bpf_perf_event_ret
418 (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
419 void *private_data);
420LIBBPF_API enum bpf_perf_event_ret
421bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
422 void **copy_mem, size_t *copy_size,
423 bpf_perf_event_print_t fn, void *private_data);
424
425struct nlattr;
426typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
427int libbpf_netlink_open(unsigned int *nl_pid);
428int libbpf_nl_get_link(int sock, unsigned int nl_pid,
429 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
430int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
431 libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
432int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
433 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
434int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
435 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
436
437struct bpf_prog_linfo;
438struct bpf_prog_info;
439
440LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
441LIBBPF_API struct bpf_prog_linfo *
442bpf_prog_linfo__new(const struct bpf_prog_info *info);
443LIBBPF_API const struct bpf_line_info *
444bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
445 __u64 addr, __u32 func_idx, __u32 nr_skip);
446LIBBPF_API const struct bpf_line_info *
447bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
448 __u32 insn_off, __u32 nr_skip);
449
450
451
452
453
454
455
456
457
458LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
459 __u32 ifindex);
460LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
461LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
462 enum bpf_prog_type prog_type, __u32 ifindex);
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493enum bpf_prog_info_array {
494 BPF_PROG_INFO_FIRST_ARRAY = 0,
495 BPF_PROG_INFO_JITED_INSNS = 0,
496 BPF_PROG_INFO_XLATED_INSNS,
497 BPF_PROG_INFO_MAP_IDS,
498 BPF_PROG_INFO_JITED_KSYMS,
499 BPF_PROG_INFO_JITED_FUNC_LENS,
500 BPF_PROG_INFO_FUNC_INFO,
501 BPF_PROG_INFO_LINE_INFO,
502 BPF_PROG_INFO_JITED_LINE_INFO,
503 BPF_PROG_INFO_PROG_TAGS,
504 BPF_PROG_INFO_LAST_ARRAY,
505};
506
507struct bpf_prog_info_linear {
508
509 __u32 info_len;
510
511 __u32 data_len;
512
513 __u64 arrays;
514 struct bpf_prog_info info;
515 __u8 data[];
516};
517
518LIBBPF_API struct bpf_prog_info_linear *
519bpf_program__get_prog_info_linear(int fd, __u64 arrays);
520
521LIBBPF_API void
522bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
523
524LIBBPF_API void
525bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541LIBBPF_API int libbpf_num_possible_cpus(void);
542
543#ifdef __cplusplus
544}
545#endif
546
547#endif
548