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