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 libbpf_print_fn_t 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83#define DECLARE_LIBBPF_OPTS(TYPE, NAME, ...) \
84 struct TYPE NAME = ({ \
85 memset(&NAME, 0, sizeof(struct TYPE)); \
86 (struct TYPE) { \
87 .sz = sizeof(struct TYPE), \
88 __VA_ARGS__ \
89 }; \
90 })
91
92struct bpf_object_open_opts {
93
94 size_t sz;
95
96
97
98
99
100
101 const char *object_name;
102
103 bool relaxed_maps;
104
105 bool relaxed_core_relocs;
106
107
108
109
110 const char *pin_root_path;
111 __u32 attach_prog_fd;
112};
113#define bpf_object_open_opts__last_field attach_prog_fd
114
115LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
116LIBBPF_API struct bpf_object *
117bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts);
118LIBBPF_API struct bpf_object *
119bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
120 struct bpf_object_open_opts *opts);
121
122
123LIBBPF_API struct bpf_object *
124bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
125 const char *name);
126LIBBPF_API struct bpf_object *
127bpf_object__open_xattr(struct bpf_object_open_attr *attr);
128
129int bpf_object__section_size(const struct bpf_object *obj, const char *name,
130 __u32 *size);
131int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
132 __u32 *off);
133
134enum libbpf_pin_type {
135 LIBBPF_PIN_NONE,
136
137 LIBBPF_PIN_BY_NAME,
138};
139
140
141
142
143
144LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
145LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
146 const char *path);
147LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
148 const char *path);
149LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
150 const char *path);
151LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
152LIBBPF_API void bpf_object__close(struct bpf_object *object);
153
154struct bpf_object_load_attr {
155 struct bpf_object *obj;
156 int log_level;
157 const char *target_btf_path;
158};
159
160
161LIBBPF_API int bpf_object__load(struct bpf_object *obj);
162LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
163LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
164LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
165LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
166
167struct btf;
168LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
169LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
170
171LIBBPF_API struct bpf_program *
172bpf_object__find_program_by_title(const struct bpf_object *obj,
173 const char *title);
174
175LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
176#define bpf_object__for_each_safe(pos, tmp) \
177 for ((pos) = bpf_object__next(NULL), \
178 (tmp) = bpf_object__next(pos); \
179 (pos) != NULL; \
180 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
181
182typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
183LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
184 bpf_object_clear_priv_t clear_priv);
185LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
186
187LIBBPF_API int
188libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
189 enum bpf_attach_type *expected_attach_type);
190LIBBPF_API int libbpf_attach_type_by_name(const char *name,
191 enum bpf_attach_type *attach_type);
192LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
193 enum bpf_attach_type attach_type);
194
195
196struct bpf_program;
197LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
198 const struct bpf_object *obj);
199
200#define bpf_object__for_each_program(pos, obj) \
201 for ((pos) = bpf_program__next(NULL, (obj)); \
202 (pos) != NULL; \
203 (pos) = bpf_program__next((pos), (obj)))
204
205LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
206 const struct bpf_object *obj);
207
208typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
209
210LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
211 bpf_program_clear_priv_t clear_priv);
212
213LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
214LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
215 __u32 ifindex);
216
217LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog,
218 bool needs_copy);
219
220
221LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
222
223LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
224 __u32 kern_version);
225LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
226LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
227 const char *path,
228 int instance);
229LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
230 const char *path,
231 int instance);
232LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
233LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
234LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
235
236struct bpf_link;
237
238LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
239
240LIBBPF_API struct bpf_link *
241bpf_program__attach_perf_event(struct bpf_program *prog, int pfd);
242LIBBPF_API struct bpf_link *
243bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe,
244 const char *func_name);
245LIBBPF_API struct bpf_link *
246bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe,
247 pid_t pid, const char *binary_path,
248 size_t func_offset);
249LIBBPF_API struct bpf_link *
250bpf_program__attach_tracepoint(struct bpf_program *prog,
251 const char *tp_category,
252 const char *tp_name);
253LIBBPF_API struct bpf_link *
254bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
255 const char *tp_name);
256
257LIBBPF_API struct bpf_link *
258bpf_program__attach_trace(struct bpf_program *prog);
259struct bpf_insn;
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290struct bpf_prog_prep_result {
291
292
293
294
295 struct bpf_insn *new_insn_ptr;
296 int new_insn_cnt;
297
298
299 int *pfd;
300};
301
302
303
304
305
306
307
308
309
310
311
312
313
314typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
315 struct bpf_insn *insns, int insns_cnt,
316 struct bpf_prog_prep_result *res);
317
318LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
319 bpf_program_prep_t prep);
320
321LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
322
323
324
325
326LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
327LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
328LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
329LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
330LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
331LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
332LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
333LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
334LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
335
336LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
337LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
338 enum bpf_prog_type type);
339
340LIBBPF_API enum bpf_attach_type
341bpf_program__get_expected_attach_type(struct bpf_program *prog);
342LIBBPF_API void
343bpf_program__set_expected_attach_type(struct bpf_program *prog,
344 enum bpf_attach_type type);
345
346LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog);
347LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog);
348LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog);
349LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog);
350LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog);
351LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog);
352LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
353LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
354LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
355
356
357
358
359
360
361
362struct bpf_map_def {
363 unsigned int type;
364 unsigned int key_size;
365 unsigned int value_size;
366 unsigned int max_entries;
367 unsigned int map_flags;
368};
369
370
371
372
373
374struct bpf_map;
375LIBBPF_API struct bpf_map *
376bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
377
378LIBBPF_API int
379bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
380
381
382
383
384
385LIBBPF_API struct bpf_map *
386bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
387
388LIBBPF_API struct bpf_map *
389bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
390#define bpf_object__for_each_map(pos, obj) \
391 for ((pos) = bpf_map__next(NULL, (obj)); \
392 (pos) != NULL; \
393 (pos) = bpf_map__next((pos), (obj)))
394#define bpf_map__for_each bpf_object__for_each_map
395
396LIBBPF_API struct bpf_map *
397bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
398
399LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
400LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
401LIBBPF_API const char *bpf_map__name(const struct bpf_map *map);
402LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
403LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
404
405typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
406LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
407 bpf_map_clear_priv_t clear_priv);
408LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
409LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
410LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
411LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
412LIBBPF_API bool bpf_map__is_internal(const struct bpf_map *map);
413LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
414LIBBPF_API int bpf_map__set_pin_path(struct bpf_map *map, const char *path);
415LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
416LIBBPF_API bool bpf_map__is_pinned(const struct bpf_map *map);
417LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
418LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
419
420LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
421
422LIBBPF_API long libbpf_get_error(const void *ptr);
423
424struct bpf_prog_load_attr {
425 const char *file;
426 enum bpf_prog_type prog_type;
427 enum bpf_attach_type expected_attach_type;
428 int ifindex;
429 int log_level;
430 int prog_flags;
431};
432
433LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
434 struct bpf_object **pobj, int *prog_fd);
435LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
436 struct bpf_object **pobj, int *prog_fd);
437
438struct xdp_link_info {
439 __u32 prog_id;
440 __u32 drv_prog_id;
441 __u32 hw_prog_id;
442 __u32 skb_prog_id;
443 __u8 attach_mode;
444};
445
446LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
447LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
448LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
449 size_t info_size, __u32 flags);
450
451struct perf_buffer;
452
453typedef void (*perf_buffer_sample_fn)(void *ctx, int cpu,
454 void *data, __u32 size);
455typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
456
457
458struct perf_buffer_opts {
459
460 perf_buffer_sample_fn sample_cb;
461
462 perf_buffer_lost_fn lost_cb;
463
464 void *ctx;
465};
466
467LIBBPF_API struct perf_buffer *
468perf_buffer__new(int map_fd, size_t page_cnt,
469 const struct perf_buffer_opts *opts);
470
471enum bpf_perf_event_ret {
472 LIBBPF_PERF_EVENT_DONE = 0,
473 LIBBPF_PERF_EVENT_ERROR = -1,
474 LIBBPF_PERF_EVENT_CONT = -2,
475};
476
477struct perf_event_header;
478
479typedef enum bpf_perf_event_ret
480(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
481
482
483struct perf_buffer_raw_opts {
484
485 struct perf_event_attr *attr;
486
487 perf_buffer_event_fn event_cb;
488
489 void *ctx;
490
491
492
493 int cpu_cnt;
494
495 int *cpus;
496
497 int *map_keys;
498};
499
500LIBBPF_API struct perf_buffer *
501perf_buffer__new_raw(int map_fd, size_t page_cnt,
502 const struct perf_buffer_raw_opts *opts);
503
504LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
505LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
506
507typedef enum bpf_perf_event_ret
508 (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
509 void *private_data);
510LIBBPF_API enum bpf_perf_event_ret
511bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
512 void **copy_mem, size_t *copy_size,
513 bpf_perf_event_print_t fn, void *private_data);
514
515struct nlattr;
516typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
517int libbpf_netlink_open(unsigned int *nl_pid);
518int libbpf_nl_get_link(int sock, unsigned int nl_pid,
519 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
520int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
521 libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
522int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
523 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
524int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
525 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
526
527struct bpf_prog_linfo;
528struct bpf_prog_info;
529
530LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
531LIBBPF_API struct bpf_prog_linfo *
532bpf_prog_linfo__new(const struct bpf_prog_info *info);
533LIBBPF_API const struct bpf_line_info *
534bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
535 __u64 addr, __u32 func_idx, __u32 nr_skip);
536LIBBPF_API const struct bpf_line_info *
537bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
538 __u32 insn_off, __u32 nr_skip);
539
540
541
542
543
544
545
546
547
548LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
549 __u32 ifindex);
550LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
551LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
552 enum bpf_prog_type prog_type, __u32 ifindex);
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583enum bpf_prog_info_array {
584 BPF_PROG_INFO_FIRST_ARRAY = 0,
585 BPF_PROG_INFO_JITED_INSNS = 0,
586 BPF_PROG_INFO_XLATED_INSNS,
587 BPF_PROG_INFO_MAP_IDS,
588 BPF_PROG_INFO_JITED_KSYMS,
589 BPF_PROG_INFO_JITED_FUNC_LENS,
590 BPF_PROG_INFO_FUNC_INFO,
591 BPF_PROG_INFO_LINE_INFO,
592 BPF_PROG_INFO_JITED_LINE_INFO,
593 BPF_PROG_INFO_PROG_TAGS,
594 BPF_PROG_INFO_LAST_ARRAY,
595};
596
597struct bpf_prog_info_linear {
598
599 __u32 info_len;
600
601 __u32 data_len;
602
603 __u64 arrays;
604 struct bpf_prog_info info;
605 __u8 data[];
606};
607
608LIBBPF_API struct bpf_prog_info_linear *
609bpf_program__get_prog_info_linear(int fd, __u64 arrays);
610
611LIBBPF_API void
612bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
613
614LIBBPF_API void
615bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631LIBBPF_API int libbpf_num_possible_cpus(void);
632
633#ifdef __cplusplus
634}
635#endif
636
637#endif
638