]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - tools/lib/bpf/libbpf.h
Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[mirror_ubuntu-jammy-kernel.git] / tools / lib / bpf / libbpf.h
CommitLineData
1bc38b8f 1/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
6061a3d6 2
1b76c13e
WN
3/*
4 * Common eBPF ELF object loading operations.
5 *
6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8 * Copyright (C) 2015 Huawei Inc.
9 */
eff81908
AI
10#ifndef __LIBBPF_LIBBPF_H
11#define __LIBBPF_LIBBPF_H
1b76c13e 12
dfcbc2f2 13#include <stdarg.h>
1a5e3fb1 14#include <stdio.h>
e28ff1a8 15#include <stdint.h>
aa9b1ac3 16#include <stdbool.h>
5a6acad1 17#include <sys/types.h> // for size_t
dd26b7f5 18#include <linux/bpf.h>
6371ca3b 19
8c4905b9
SF
20#ifdef __cplusplus
21extern "C" {
22#endif
23
ab9e0848
AI
24#ifndef LIBBPF_API
25#define LIBBPF_API __attribute__((visibility("default")))
26#endif
27
6371ca3b
WN
28enum libbpf_errno {
29 __LIBBPF_ERRNO__START = 4000,
30
31 /* Something wrong in libelf */
32 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
33 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
34 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
de8a63bd 35 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
6371ca3b
WN
36 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
37 LIBBPF_ERRNO__RELOC, /* Relocation failed */
38 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
39 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
40 LIBBPF_ERRNO__PROG2BIG, /* Program too big */
41 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
705fa219 42 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
949abbe8
EL
43 LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
44 LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
36f1678d 45 LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
6371ca3b
WN
46 __LIBBPF_ERRNO__END,
47};
48
ab9e0848 49LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
1a5e3fb1 50
8461ef8b
YS
51enum libbpf_print_level {
52 LIBBPF_WARN,
53 LIBBPF_INFO,
54 LIBBPF_DEBUG,
55};
56
6f1ae8b6 57typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
a8a1f7d0 58 const char *, va_list ap);
b3f59d66 59
e87fd8ba 60LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
b3f59d66 61
1a5e3fb1
WN
62/* Hide internal to user */
63struct bpf_object;
64
07f2d4ea
JK
65struct bpf_object_open_attr {
66 const char *file;
67 enum bpf_prog_type prog_type;
68};
69
2ce8450e
AN
70/* Helper macro to declare and initialize libbpf options struct
71 *
72 * This dance with uninitialized declaration, followed by memset to zero,
73 * followed by assignment using compound literal syntax is done to preserve
74 * ability to use a nice struct field initialization syntax and **hopefully**
75 * have all the padding bytes initialized to zero. It's not guaranteed though,
76 * when copying literal, that compiler won't copy garbage in literal's padding
77 * bytes, but that's the best way I've found and it seems to work in practice.
e00aca65
AN
78 *
79 * Macro declares opts struct of given type and name, zero-initializes,
80 * including any extra padding, it with memset() and then assigns initial
81 * values provided by users in struct initializer-syntax as varargs.
2ce8450e 82 */
e00aca65
AN
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 })
2ce8450e
AN
91
92struct bpf_object_open_opts {
93 /* size of this struct, for forward/backward compatiblity */
94 size_t sz;
95 /* object name override, if provided:
96 * - for object open from file, this will override setting object
97 * name from file path's base name;
98 * - for object open from memory buffer, this will specify an object
99 * name and will override default "<addr>-<buf-size>" name;
100 */
101 const char *object_name;
102 /* parse map definitions non-strictly, allowing extra attributes/data */
103 bool relaxed_maps;
62561eb4
AN
104 /* process CO-RE relocations non-strictly, allowing them to fail */
105 bool relaxed_core_relocs;
57a00f41
THJ
106 /* maps that set the 'pinning' attribute in their definition will have
107 * their pin_path attribute set to a file in this directory, and be
108 * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
109 */
110 const char *pin_root_path;
e7bf94db 111 __u32 attach_prog_fd;
2ce8450e 112};
e7bf94db 113#define bpf_object_open_opts__last_field attach_prog_fd
2ce8450e 114
ab9e0848
AI
115LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
116LIBBPF_API struct bpf_object *
2ce8450e
AN
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/* deprecated bpf_object__open variants */
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 *
ab9e0848 127bpf_object__open_xattr(struct bpf_object_open_attr *attr);
2ce8450e 128
1713d68b
DB
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);
4580b25f 133
57a00f41
THJ
134enum libbpf_pin_type {
135 LIBBPF_PIN_NONE,
136 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
137 LIBBPF_PIN_BY_NAME,
138};
139
4580b25f
THJ
140/* pin_maps and unpin_maps can both be called with a NULL path, in which case
141 * they will use the pin_path attribute of each map (and ignore all maps that
142 * don't have a pin_path set).
143 */
0c19a9fb
SF
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);
ab9e0848
AI
151LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
152LIBBPF_API void bpf_object__close(struct bpf_object *object);
1a5e3fb1 153
60276f98
QM
154struct bpf_object_load_attr {
155 struct bpf_object *obj;
156 int log_level;
ddc7c304 157 const char *target_btf_path;
60276f98
QM
158};
159
52d3352e 160/* Load/unload object into/from kernel */
ab9e0848 161LIBBPF_API int bpf_object__load(struct bpf_object *obj);
60276f98 162LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
ab9e0848 163LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
a324aae3
AN
164LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
165LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
789f6bab
AI
166
167struct btf;
a324aae3 168LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
ab9e0848 169LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
52d3352e 170
ab9e0848 171LIBBPF_API struct bpf_program *
a324aae3
AN
172bpf_object__find_program_by_title(const struct bpf_object *obj,
173 const char *title);
6d4b198b 174
ab9e0848 175LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
9a208eff
WN
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
10931d24 182typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
ab9e0848
AI
183LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
184 bpf_object_clear_priv_t clear_priv);
a324aae3 185LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
10931d24 186
ab9e0848
AI
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);
b8c54ea4
AS
192LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
193 enum bpf_attach_type attach_type);
b60df2a0 194
2eb57bb8 195/* Accessors of bpf_program */
aa9b1ac3 196struct bpf_program;
ab9e0848 197LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
a324aae3 198 const struct bpf_object *obj);
aa9b1ac3
WN
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
0c19a9fb 205LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
a324aae3 206 const struct bpf_object *obj);
0c19a9fb 207
a324aae3 208typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, void *);
aa9b1ac3 209
ab9e0848
AI
210LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
211 bpf_program_clear_priv_t clear_priv);
aa9b1ac3 212
a324aae3 213LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
ab9e0848
AI
214LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
215 __u32 ifindex);
aa9b1ac3 216
a324aae3 217LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog,
ab9e0848 218 bool needs_copy);
aa9b1ac3 219
1a734efe
THJ
220/* returns program size in bytes */
221LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
222
ab9e0848
AI
223LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
224 __u32 kern_version);
a324aae3 225LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
ab9e0848
AI
226LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
227 const char *path,
228 int instance);
0c19a9fb
SF
229LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
230 const char *path,
231 int instance);
ab9e0848 232LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
0c19a9fb 233LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
ab9e0848 234LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
aa9b1ac3 235
1c2e9efc
AN
236struct bpf_link;
237
238LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
239
63f2f5ee
AN
240LIBBPF_API struct bpf_link *
241bpf_program__attach_perf_event(struct bpf_program *prog, int pfd);
b2650027
AN
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);
f6de59c1
AN
249LIBBPF_API struct bpf_link *
250bpf_program__attach_tracepoint(struct bpf_program *prog,
251 const char *tp_category,
252 const char *tp_name);
84bf5e1f
AN
253LIBBPF_API struct bpf_link *
254bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
255 const char *tp_name);
63f2f5ee 256
b8c54ea4
AS
257LIBBPF_API struct bpf_link *
258bpf_program__attach_trace(struct bpf_program *prog);
b580563e
WN
259struct bpf_insn;
260
261/*
262 * Libbpf allows callers to adjust BPF programs before being loaded
2eb57bb8
JK
263 * into kernel. One program in an object file can be transformed into
264 * multiple variants to be attached to different hooks.
b580563e
WN
265 *
266 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
2eb57bb8 267 * form an API for this purpose.
b580563e
WN
268 *
269 * - bpf_program_prep_t:
2eb57bb8 270 * Defines a 'preprocessor', which is a caller defined function
b580563e
WN
271 * passed to libbpf through bpf_program__set_prep(), and will be
272 * called before program is loaded. The processor should adjust
2eb57bb8 273 * the program one time for each instance according to the instance id
b580563e
WN
274 * passed to it.
275 *
276 * - bpf_program__set_prep:
2eb57bb8
JK
277 * Attaches a preprocessor to a BPF program. The number of instances
278 * that should be created is also passed through this function.
b580563e
WN
279 *
280 * - bpf_program__nth_fd:
2eb57bb8
JK
281 * After the program is loaded, get resulting FD of a given instance
282 * of the BPF program.
b580563e 283 *
2eb57bb8 284 * If bpf_program__set_prep() is not used, the program would be loaded
b580563e
WN
285 * without adjustment during bpf_object__load(). The program has only
286 * one instance. In this case bpf_program__fd(prog) is equal to
287 * bpf_program__nth_fd(prog, 0).
288 */
289
290struct bpf_prog_prep_result {
291 /*
292 * If not NULL, load new instruction array.
293 * If set to NULL, don't load this instance.
294 */
295 struct bpf_insn *new_insn_ptr;
296 int new_insn_cnt;
297
2eb57bb8 298 /* If not NULL, result FD is written to it. */
b580563e
WN
299 int *pfd;
300};
301
302/*
303 * Parameters of bpf_program_prep_t:
304 * - prog: The bpf_program being loaded.
305 * - n: Index of instance being generated.
306 * - insns: BPF instructions array.
307 * - insns_cnt:Number of instructions in insns.
308 * - res: Output parameter, result of transformation.
309 *
310 * Return value:
2eb57bb8
JK
311 * - Zero: pre-processing success.
312 * - Non-zero: pre-processing error, stop loading.
b580563e
WN
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
ab9e0848
AI
318LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
319 bpf_program_prep_t prep);
b580563e 320
a324aae3 321LIBBPF_API int bpf_program__nth_fd(const struct bpf_program *prog, int n);
b580563e 322
5f44e4c8 323/*
2eb57bb8 324 * Adjust type of BPF program. Default is kprobe.
5f44e4c8 325 */
ab9e0848
AI
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);
12a8654b 334LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
f1eead9e
AN
335
336LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
ab9e0848
AI
337LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
338 enum bpf_prog_type type);
f1eead9e
AN
339
340LIBBPF_API enum bpf_attach_type
341bpf_program__get_expected_attach_type(struct bpf_program *prog);
ab9e0848
AI
342LIBBPF_API void
343bpf_program__set_expected_attach_type(struct bpf_program *prog,
344 enum bpf_attach_type type);
345
a324aae3
AN
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);
12a8654b 354LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
5f44e4c8 355
34090915 356/*
2eb57bb8
JK
357 * No need for __attribute__((packed)), all members of 'bpf_map_def'
358 * are all aligned. In addition, using __attribute__((packed))
359 * would trigger a -Wpacked warning message, and lead to an error
360 * if -Werror is set.
34090915
WN
361 */
362struct bpf_map_def {
363 unsigned int type;
364 unsigned int key_size;
365 unsigned int value_size;
366 unsigned int max_entries;
fe9b5f77 367 unsigned int map_flags;
34090915
WN
368};
369
9d759a9b 370/*
2eb57bb8
JK
371 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
372 * so no need to worry about a name clash.
9d759a9b
WN
373 */
374struct bpf_map;
ab9e0848 375LIBBPF_API struct bpf_map *
a324aae3 376bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);
9d759a9b 377
f3cea32d 378LIBBPF_API int
a324aae3 379bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name);
f3cea32d 380
5a6acad1
WN
381/*
382 * Get bpf_map through the offset of corresponding struct bpf_map_def
2eb57bb8 383 * in the BPF object file.
5a6acad1 384 */
ab9e0848 385LIBBPF_API struct bpf_map *
5a6acad1
WN
386bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
387
ab9e0848 388LIBBPF_API struct bpf_map *
a324aae3 389bpf_map__next(const struct bpf_map *map, const struct bpf_object *obj);
f74a53d9 390#define bpf_object__for_each_map(pos, obj) \
9d759a9b
WN
391 for ((pos) = bpf_map__next(NULL, (obj)); \
392 (pos) != NULL; \
393 (pos) = bpf_map__next((pos), (obj)))
f74a53d9 394#define bpf_map__for_each bpf_object__for_each_map
9d759a9b 395
0c19a9fb 396LIBBPF_API struct bpf_map *
a324aae3 397bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
0c19a9fb 398
a324aae3
AN
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);
ab9e0848
AI
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);
9d759a9b
WN
404
405typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
ab9e0848
AI
406LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
407 bpf_map_clear_priv_t clear_priv);
a324aae3 408LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
ab9e0848 409LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
1a11a4c7 410LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
a324aae3
AN
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);
ab9e0848 413LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
4580b25f
THJ
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);
ab9e0848 417LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
0c19a9fb 418LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
9d759a9b 419
addb9fc9
NS
420LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
421
ab9e0848 422LIBBPF_API long libbpf_get_error(const void *ptr);
e28ff1a8 423
d7be143b
AI
424struct bpf_prog_load_attr {
425 const char *file;
426 enum bpf_prog_type prog_type;
427 enum bpf_attach_type expected_attach_type;
f0307a7e 428 int ifindex;
da11b417 429 int log_level;
04656198 430 int prog_flags;
d7be143b
AI
431};
432
ab9e0848
AI
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);
949abbe8 437
473f4e13
THJ
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
ab9e0848 446LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
50db9f07 447LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
473f4e13
THJ
448LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
449 size_t info_size, __u32 flags);
d0cabbb0 450
fb84b822
AN
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/* common use perf buffer options */
458struct perf_buffer_opts {
459 /* if specified, sample_cb is called for each sample */
460 perf_buffer_sample_fn sample_cb;
461 /* if specified, lost_cb is called for each batch of lost samples */
462 perf_buffer_lost_fn lost_cb;
463 /* ctx is provided to sample_cb and lost_cb */
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
d0cabbb0
JK
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
3dca2115 477struct perf_event_header;
fb84b822
AN
478
479typedef enum bpf_perf_event_ret
480(*perf_buffer_event_fn)(void *ctx, int cpu, struct perf_event_header *event);
481
482/* raw perf buffer options, giving most power and control */
483struct perf_buffer_raw_opts {
484 /* perf event attrs passed directly into perf_event_open() */
485 struct perf_event_attr *attr;
486 /* raw event callback */
487 perf_buffer_event_fn event_cb;
488 /* ctx is provided to event_cb */
489 void *ctx;
490 /* if cpu_cnt == 0, open all on all possible CPUs (up to the number of
491 * max_entries of given PERF_EVENT_ARRAY map)
492 */
493 int cpu_cnt;
494 /* if cpu_cnt > 0, cpus is an array of CPUs to open ring buffers on */
495 int *cpus;
496 /* if cpu_cnt > 0, map_keys specify map keys to set per-CPU FDs for */
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
3dca2115
DB
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);
36f1678d 514
36f1678d 515struct nlattr;
aae57780
AI
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);
8c4905b9 526
b053b439
MKL
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
1bf4b058
QM
540/*
541 * Probe for supported system features
542 *
543 * Note that running many of these probes in a short amount of time can cause
544 * the kernel to reach the maximal size of lockable memory allowed for the
545 * user, causing subsequent probes to fail. In this case, the caller may want
546 * to adjust that limit with setrlimit().
547 */
548LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
549 __u32 ifindex);
f99e1663 550LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
2d3ea5e8
QM
551LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
552 enum bpf_prog_type prog_type, __u32 ifindex);
1bf4b058 553
34be1646
SL
554/*
555 * Get bpf_prog_info in continuous memory
556 *
557 * struct bpf_prog_info has multiple arrays. The user has option to choose
558 * arrays to fetch from kernel. The following APIs provide an uniform way to
559 * fetch these data. All arrays in bpf_prog_info are stored in a single
560 * continuous memory region. This makes it easy to store the info in a
561 * file.
562 *
563 * Before writing bpf_prog_info_linear to files, it is necessary to
564 * translate pointers in bpf_prog_info to offsets. Helper functions
565 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
566 * are introduced to switch between pointers and offsets.
567 *
568 * Examples:
569 * # To fetch map_ids and prog_tags:
570 * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
571 * (1UL << BPF_PROG_INFO_PROG_TAGS);
572 * struct bpf_prog_info_linear *info_linear =
573 * bpf_program__get_prog_info_linear(fd, arrays);
574 *
575 * # To save data in file
576 * bpf_program__bpil_addr_to_offs(info_linear);
577 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
578 *
579 * # To read data from file
580 * read(f, info_linear, <proper_size>);
581 * bpf_program__bpil_offs_to_addr(info_linear);
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 /* size of struct bpf_prog_info, when the tool is compiled */
599 __u32 info_len;
600 /* total bytes allocated for data, round up to 8 bytes */
601 __u32 data_len;
602 /* which arrays are included in data */
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
6446b315
HL
617/*
618 * A helper function to get the number of possible CPUs before looking up
619 * per-CPU maps. Negative errno is returned on failure.
620 *
621 * Example usage:
622 *
623 * int ncpus = libbpf_num_possible_cpus();
624 * if (ncpus < 0) {
625 * // error handling
626 * }
627 * long values[ncpus];
628 * bpf_map_lookup_elem(per_cpu_map_fd, key, values);
629 *
630 */
631LIBBPF_API int libbpf_num_possible_cpus(void);
632
8c4905b9
SF
633#ifdef __cplusplus
634} /* extern "C" */
635#endif
636
eff81908 637#endif /* __LIBBPF_LIBBPF_H */