]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - tools/lib/bpf/libbpf.h
Merge branches 'for-5.1/upstream-fixes', 'for-5.2/core', 'for-5.2/ish', 'for-5.2...
[mirror_ubuntu-kernels.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
6f1ae8b6 60LIBBPF_API void 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
ab9e0848
AI
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);
c034a177
JF
73struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
74 int flags);
ab9e0848
AI
75LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf,
76 size_t obj_buf_sz,
77 const char *name);
0c19a9fb
SF
78LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
79LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
80 const char *path);
81LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
82 const char *path);
83LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
84 const char *path);
ab9e0848
AI
85LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
86LIBBPF_API void bpf_object__close(struct bpf_object *object);
1a5e3fb1 87
52d3352e 88/* Load/unload object into/from kernel */
ab9e0848
AI
89LIBBPF_API int bpf_object__load(struct bpf_object *obj);
90LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
91LIBBPF_API const char *bpf_object__name(struct bpf_object *obj);
92LIBBPF_API unsigned int bpf_object__kversion(struct bpf_object *obj);
789f6bab
AI
93
94struct btf;
95LIBBPF_API struct btf *bpf_object__btf(struct bpf_object *obj);
ab9e0848 96LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
52d3352e 97
ab9e0848 98LIBBPF_API struct bpf_program *
6d4b198b
JK
99bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
100
ab9e0848 101LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
9a208eff
WN
102#define bpf_object__for_each_safe(pos, tmp) \
103 for ((pos) = bpf_object__next(NULL), \
104 (tmp) = bpf_object__next(pos); \
105 (pos) != NULL; \
106 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
107
10931d24 108typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
ab9e0848
AI
109LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
110 bpf_object_clear_priv_t clear_priv);
111LIBBPF_API void *bpf_object__priv(struct bpf_object *prog);
10931d24 112
ab9e0848
AI
113LIBBPF_API int
114libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
115 enum bpf_attach_type *expected_attach_type);
116LIBBPF_API int libbpf_attach_type_by_name(const char *name,
117 enum bpf_attach_type *attach_type);
b60df2a0 118
2eb57bb8 119/* Accessors of bpf_program */
aa9b1ac3 120struct bpf_program;
ab9e0848
AI
121LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
122 struct bpf_object *obj);
aa9b1ac3
WN
123
124#define bpf_object__for_each_program(pos, obj) \
125 for ((pos) = bpf_program__next(NULL, (obj)); \
126 (pos) != NULL; \
127 (pos) = bpf_program__next((pos), (obj)))
128
0c19a9fb
SF
129LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
130 struct bpf_object *obj);
131
aa9b1ac3
WN
132typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
133 void *);
134
ab9e0848
AI
135LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
136 bpf_program_clear_priv_t clear_priv);
aa9b1ac3 137
ab9e0848
AI
138LIBBPF_API void *bpf_program__priv(struct bpf_program *prog);
139LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
140 __u32 ifindex);
aa9b1ac3 141
ab9e0848
AI
142LIBBPF_API const char *bpf_program__title(struct bpf_program *prog,
143 bool needs_copy);
aa9b1ac3 144
ab9e0848
AI
145LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
146 __u32 kern_version);
147LIBBPF_API int bpf_program__fd(struct bpf_program *prog);
148LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
149 const char *path,
150 int instance);
0c19a9fb
SF
151LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
152 const char *path,
153 int instance);
ab9e0848 154LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
0c19a9fb 155LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
ab9e0848 156LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
aa9b1ac3 157
b580563e
WN
158struct bpf_insn;
159
160/*
161 * Libbpf allows callers to adjust BPF programs before being loaded
2eb57bb8
JK
162 * into kernel. One program in an object file can be transformed into
163 * multiple variants to be attached to different hooks.
b580563e
WN
164 *
165 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
2eb57bb8 166 * form an API for this purpose.
b580563e
WN
167 *
168 * - bpf_program_prep_t:
2eb57bb8 169 * Defines a 'preprocessor', which is a caller defined function
b580563e
WN
170 * passed to libbpf through bpf_program__set_prep(), and will be
171 * called before program is loaded. The processor should adjust
2eb57bb8 172 * the program one time for each instance according to the instance id
b580563e
WN
173 * passed to it.
174 *
175 * - bpf_program__set_prep:
2eb57bb8
JK
176 * Attaches a preprocessor to a BPF program. The number of instances
177 * that should be created is also passed through this function.
b580563e
WN
178 *
179 * - bpf_program__nth_fd:
2eb57bb8
JK
180 * After the program is loaded, get resulting FD of a given instance
181 * of the BPF program.
b580563e 182 *
2eb57bb8 183 * If bpf_program__set_prep() is not used, the program would be loaded
b580563e
WN
184 * without adjustment during bpf_object__load(). The program has only
185 * one instance. In this case bpf_program__fd(prog) is equal to
186 * bpf_program__nth_fd(prog, 0).
187 */
188
189struct bpf_prog_prep_result {
190 /*
191 * If not NULL, load new instruction array.
192 * If set to NULL, don't load this instance.
193 */
194 struct bpf_insn *new_insn_ptr;
195 int new_insn_cnt;
196
2eb57bb8 197 /* If not NULL, result FD is written to it. */
b580563e
WN
198 int *pfd;
199};
200
201/*
202 * Parameters of bpf_program_prep_t:
203 * - prog: The bpf_program being loaded.
204 * - n: Index of instance being generated.
205 * - insns: BPF instructions array.
206 * - insns_cnt:Number of instructions in insns.
207 * - res: Output parameter, result of transformation.
208 *
209 * Return value:
2eb57bb8
JK
210 * - Zero: pre-processing success.
211 * - Non-zero: pre-processing error, stop loading.
b580563e
WN
212 */
213typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
214 struct bpf_insn *insns, int insns_cnt,
215 struct bpf_prog_prep_result *res);
216
ab9e0848
AI
217LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
218 bpf_program_prep_t prep);
b580563e 219
ab9e0848 220LIBBPF_API int bpf_program__nth_fd(struct bpf_program *prog, int n);
b580563e 221
5f44e4c8 222/*
2eb57bb8 223 * Adjust type of BPF program. Default is kprobe.
5f44e4c8 224 */
ab9e0848
AI
225LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
226LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
227LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
228LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
229LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
230LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
231LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
232LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
233LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
234 enum bpf_prog_type type);
235LIBBPF_API void
236bpf_program__set_expected_attach_type(struct bpf_program *prog,
237 enum bpf_attach_type type);
238
239LIBBPF_API bool bpf_program__is_socket_filter(struct bpf_program *prog);
240LIBBPF_API bool bpf_program__is_tracepoint(struct bpf_program *prog);
241LIBBPF_API bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
242LIBBPF_API bool bpf_program__is_kprobe(struct bpf_program *prog);
243LIBBPF_API bool bpf_program__is_sched_cls(struct bpf_program *prog);
244LIBBPF_API bool bpf_program__is_sched_act(struct bpf_program *prog);
245LIBBPF_API bool bpf_program__is_xdp(struct bpf_program *prog);
246LIBBPF_API bool bpf_program__is_perf_event(struct bpf_program *prog);
5f44e4c8 247
34090915 248/*
2eb57bb8
JK
249 * No need for __attribute__((packed)), all members of 'bpf_map_def'
250 * are all aligned. In addition, using __attribute__((packed))
251 * would trigger a -Wpacked warning message, and lead to an error
252 * if -Werror is set.
34090915
WN
253 */
254struct bpf_map_def {
255 unsigned int type;
256 unsigned int key_size;
257 unsigned int value_size;
258 unsigned int max_entries;
fe9b5f77 259 unsigned int map_flags;
34090915
WN
260};
261
9d759a9b 262/*
2eb57bb8
JK
263 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
264 * so no need to worry about a name clash.
9d759a9b
WN
265 */
266struct bpf_map;
ab9e0848 267LIBBPF_API struct bpf_map *
a7fe0450 268bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
9d759a9b 269
f3cea32d
MF
270LIBBPF_API int
271bpf_object__find_map_fd_by_name(struct bpf_object *obj, const char *name);
272
5a6acad1
WN
273/*
274 * Get bpf_map through the offset of corresponding struct bpf_map_def
2eb57bb8 275 * in the BPF object file.
5a6acad1 276 */
ab9e0848 277LIBBPF_API struct bpf_map *
5a6acad1
WN
278bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
279
ab9e0848 280LIBBPF_API struct bpf_map *
9d759a9b 281bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
f74a53d9 282#define bpf_object__for_each_map(pos, obj) \
9d759a9b
WN
283 for ((pos) = bpf_map__next(NULL, (obj)); \
284 (pos) != NULL; \
285 (pos) = bpf_map__next((pos), (obj)))
f74a53d9 286#define bpf_map__for_each bpf_object__for_each_map
9d759a9b 287
0c19a9fb
SF
288LIBBPF_API struct bpf_map *
289bpf_map__prev(struct bpf_map *map, struct bpf_object *obj);
290
ab9e0848
AI
291LIBBPF_API int bpf_map__fd(struct bpf_map *map);
292LIBBPF_API const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
293LIBBPF_API const char *bpf_map__name(struct bpf_map *map);
294LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
295LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
9d759a9b
WN
296
297typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
ab9e0848
AI
298LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
299 bpf_map_clear_priv_t clear_priv);
300LIBBPF_API void *bpf_map__priv(struct bpf_map *map);
301LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
1a11a4c7 302LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
ab9e0848
AI
303LIBBPF_API bool bpf_map__is_offload_neutral(struct bpf_map *map);
304LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
305LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
0c19a9fb 306LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
9d759a9b 307
addb9fc9
NS
308LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
309
ab9e0848 310LIBBPF_API long libbpf_get_error(const void *ptr);
e28ff1a8 311
d7be143b
AI
312struct bpf_prog_load_attr {
313 const char *file;
314 enum bpf_prog_type prog_type;
315 enum bpf_attach_type expected_attach_type;
f0307a7e 316 int ifindex;
d7be143b
AI
317};
318
ab9e0848
AI
319LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
320 struct bpf_object **pobj, int *prog_fd);
321LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
322 struct bpf_object **pobj, int *prog_fd);
949abbe8 323
ab9e0848 324LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
50db9f07 325LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
d0cabbb0
JK
326
327enum bpf_perf_event_ret {
328 LIBBPF_PERF_EVENT_DONE = 0,
329 LIBBPF_PERF_EVENT_ERROR = -1,
330 LIBBPF_PERF_EVENT_CONT = -2,
331};
332
3dca2115
DB
333struct perf_event_header;
334typedef enum bpf_perf_event_ret
335 (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
336 void *private_data);
337LIBBPF_API enum bpf_perf_event_ret
338bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
339 void **copy_mem, size_t *copy_size,
340 bpf_perf_event_print_t fn, void *private_data);
36f1678d 341
36f1678d 342struct nlattr;
aae57780
AI
343typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
344int libbpf_netlink_open(unsigned int *nl_pid);
345int libbpf_nl_get_link(int sock, unsigned int nl_pid,
346 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
347int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
348 libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
349int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
350 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
351int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
352 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
8c4905b9 353
b053b439
MKL
354struct bpf_prog_linfo;
355struct bpf_prog_info;
356
357LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
358LIBBPF_API struct bpf_prog_linfo *
359bpf_prog_linfo__new(const struct bpf_prog_info *info);
360LIBBPF_API const struct bpf_line_info *
361bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
362 __u64 addr, __u32 func_idx, __u32 nr_skip);
363LIBBPF_API const struct bpf_line_info *
364bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
365 __u32 insn_off, __u32 nr_skip);
366
1bf4b058
QM
367/*
368 * Probe for supported system features
369 *
370 * Note that running many of these probes in a short amount of time can cause
371 * the kernel to reach the maximal size of lockable memory allowed for the
372 * user, causing subsequent probes to fail. In this case, the caller may want
373 * to adjust that limit with setrlimit().
374 */
375LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
376 __u32 ifindex);
f99e1663 377LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
2d3ea5e8
QM
378LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
379 enum bpf_prog_type prog_type, __u32 ifindex);
1bf4b058 380
34be1646
SL
381/*
382 * Get bpf_prog_info in continuous memory
383 *
384 * struct bpf_prog_info has multiple arrays. The user has option to choose
385 * arrays to fetch from kernel. The following APIs provide an uniform way to
386 * fetch these data. All arrays in bpf_prog_info are stored in a single
387 * continuous memory region. This makes it easy to store the info in a
388 * file.
389 *
390 * Before writing bpf_prog_info_linear to files, it is necessary to
391 * translate pointers in bpf_prog_info to offsets. Helper functions
392 * bpf_program__bpil_addr_to_offs() and bpf_program__bpil_offs_to_addr()
393 * are introduced to switch between pointers and offsets.
394 *
395 * Examples:
396 * # To fetch map_ids and prog_tags:
397 * __u64 arrays = (1UL << BPF_PROG_INFO_MAP_IDS) |
398 * (1UL << BPF_PROG_INFO_PROG_TAGS);
399 * struct bpf_prog_info_linear *info_linear =
400 * bpf_program__get_prog_info_linear(fd, arrays);
401 *
402 * # To save data in file
403 * bpf_program__bpil_addr_to_offs(info_linear);
404 * write(f, info_linear, sizeof(*info_linear) + info_linear->data_len);
405 *
406 * # To read data from file
407 * read(f, info_linear, <proper_size>);
408 * bpf_program__bpil_offs_to_addr(info_linear);
409 */
410enum bpf_prog_info_array {
411 BPF_PROG_INFO_FIRST_ARRAY = 0,
412 BPF_PROG_INFO_JITED_INSNS = 0,
413 BPF_PROG_INFO_XLATED_INSNS,
414 BPF_PROG_INFO_MAP_IDS,
415 BPF_PROG_INFO_JITED_KSYMS,
416 BPF_PROG_INFO_JITED_FUNC_LENS,
417 BPF_PROG_INFO_FUNC_INFO,
418 BPF_PROG_INFO_LINE_INFO,
419 BPF_PROG_INFO_JITED_LINE_INFO,
420 BPF_PROG_INFO_PROG_TAGS,
421 BPF_PROG_INFO_LAST_ARRAY,
422};
423
424struct bpf_prog_info_linear {
425 /* size of struct bpf_prog_info, when the tool is compiled */
426 __u32 info_len;
427 /* total bytes allocated for data, round up to 8 bytes */
428 __u32 data_len;
429 /* which arrays are included in data */
430 __u64 arrays;
431 struct bpf_prog_info info;
432 __u8 data[];
433};
434
435LIBBPF_API struct bpf_prog_info_linear *
436bpf_program__get_prog_info_linear(int fd, __u64 arrays);
437
438LIBBPF_API void
439bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
440
441LIBBPF_API void
442bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
443
8c4905b9
SF
444#ifdef __cplusplus
445} /* extern "C" */
446#endif
447
eff81908 448#endif /* __LIBBPF_LIBBPF_H */