xref: /aosp_15_r20/external/libbpf/src/libbpf_legacy.h (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2*f7c14bbaSAndroid Build Coastguard Worker 
3*f7c14bbaSAndroid Build Coastguard Worker /*
4*f7c14bbaSAndroid Build Coastguard Worker  * Libbpf legacy APIs (either discouraged or deprecated, as mentioned in [0])
5*f7c14bbaSAndroid Build Coastguard Worker  *
6*f7c14bbaSAndroid Build Coastguard Worker  *   [0] https://docs.google.com/document/d/1UyjTZuPFWiPFyKk1tV5an11_iaRuec6U-ZESZ54nNTY
7*f7c14bbaSAndroid Build Coastguard Worker  *
8*f7c14bbaSAndroid Build Coastguard Worker  * Copyright (C) 2021 Facebook
9*f7c14bbaSAndroid Build Coastguard Worker  */
10*f7c14bbaSAndroid Build Coastguard Worker #ifndef __LIBBPF_LEGACY_BPF_H
11*f7c14bbaSAndroid Build Coastguard Worker #define __LIBBPF_LEGACY_BPF_H
12*f7c14bbaSAndroid Build Coastguard Worker 
13*f7c14bbaSAndroid Build Coastguard Worker #include <linux/bpf.h>
14*f7c14bbaSAndroid Build Coastguard Worker #include <stdbool.h>
15*f7c14bbaSAndroid Build Coastguard Worker #include <stddef.h>
16*f7c14bbaSAndroid Build Coastguard Worker #include <stdint.h>
17*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_common.h"
18*f7c14bbaSAndroid Build Coastguard Worker 
19*f7c14bbaSAndroid Build Coastguard Worker #ifdef __cplusplus
20*f7c14bbaSAndroid Build Coastguard Worker extern "C" {
21*f7c14bbaSAndroid Build Coastguard Worker #endif
22*f7c14bbaSAndroid Build Coastguard Worker 
23*f7c14bbaSAndroid Build Coastguard Worker /* As of libbpf 1.0 libbpf_set_strict_mode() and enum libbpf_struct_mode have
24*f7c14bbaSAndroid Build Coastguard Worker  * no effect. But they are left in libbpf_legacy.h so that applications that
25*f7c14bbaSAndroid Build Coastguard Worker  * prepared for libbpf 1.0 before final release by using
26*f7c14bbaSAndroid Build Coastguard Worker  * libbpf_set_strict_mode() still work with libbpf 1.0+ without any changes.
27*f7c14bbaSAndroid Build Coastguard Worker  */
28*f7c14bbaSAndroid Build Coastguard Worker enum libbpf_strict_mode {
29*f7c14bbaSAndroid Build Coastguard Worker 	/* Turn on all supported strict features of libbpf to simulate libbpf
30*f7c14bbaSAndroid Build Coastguard Worker 	 * v1.0 behavior.
31*f7c14bbaSAndroid Build Coastguard Worker 	 * This will be the default behavior in libbpf v1.0.
32*f7c14bbaSAndroid Build Coastguard Worker 	 */
33*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_STRICT_ALL = 0xffffffff,
34*f7c14bbaSAndroid Build Coastguard Worker 
35*f7c14bbaSAndroid Build Coastguard Worker 	/*
36*f7c14bbaSAndroid Build Coastguard Worker 	 * Disable any libbpf 1.0 behaviors. This is the default before libbpf
37*f7c14bbaSAndroid Build Coastguard Worker 	 * v1.0. It won't be supported anymore in v1.0, please update your
38*f7c14bbaSAndroid Build Coastguard Worker 	 * code so that it handles LIBBPF_STRICT_ALL mode before libbpf v1.0.
39*f7c14bbaSAndroid Build Coastguard Worker 	 */
40*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_STRICT_NONE = 0x00,
41*f7c14bbaSAndroid Build Coastguard Worker 	/*
42*f7c14bbaSAndroid Build Coastguard Worker 	 * Return NULL pointers on error, not ERR_PTR(err).
43*f7c14bbaSAndroid Build Coastguard Worker 	 * Additionally, libbpf also always sets errno to corresponding Exx
44*f7c14bbaSAndroid Build Coastguard Worker 	 * (positive) error code.
45*f7c14bbaSAndroid Build Coastguard Worker 	 */
46*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_STRICT_CLEAN_PTRS = 0x01,
47*f7c14bbaSAndroid Build Coastguard Worker 	/*
48*f7c14bbaSAndroid Build Coastguard Worker 	 * Return actual error codes from low-level APIs directly, not just -1.
49*f7c14bbaSAndroid Build Coastguard Worker 	 * Additionally, libbpf also always sets errno to corresponding Exx
50*f7c14bbaSAndroid Build Coastguard Worker 	 * (positive) error code.
51*f7c14bbaSAndroid Build Coastguard Worker 	 */
52*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_STRICT_DIRECT_ERRS = 0x02,
53*f7c14bbaSAndroid Build Coastguard Worker 	/*
54*f7c14bbaSAndroid Build Coastguard Worker 	 * Enforce strict BPF program section (SEC()) names.
55*f7c14bbaSAndroid Build Coastguard Worker 	 * E.g., while prefiously SEC("xdp_whatever") or SEC("perf_event_blah") were
56*f7c14bbaSAndroid Build Coastguard Worker 	 * allowed, with LIBBPF_STRICT_SEC_PREFIX this will become
57*f7c14bbaSAndroid Build Coastguard Worker 	 * unrecognized by libbpf and would have to be just SEC("xdp") and
58*f7c14bbaSAndroid Build Coastguard Worker 	 * SEC("xdp") and SEC("perf_event").
59*f7c14bbaSAndroid Build Coastguard Worker 	 *
60*f7c14bbaSAndroid Build Coastguard Worker 	 * Note, in this mode the program pin path will be based on the
61*f7c14bbaSAndroid Build Coastguard Worker 	 * function name instead of section name.
62*f7c14bbaSAndroid Build Coastguard Worker 	 *
63*f7c14bbaSAndroid Build Coastguard Worker 	 * Additionally, routines in the .text section are always considered
64*f7c14bbaSAndroid Build Coastguard Worker 	 * sub-programs. Legacy behavior allows for a single routine in .text
65*f7c14bbaSAndroid Build Coastguard Worker 	 * to be a program.
66*f7c14bbaSAndroid Build Coastguard Worker 	 */
67*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_STRICT_SEC_NAME = 0x04,
68*f7c14bbaSAndroid Build Coastguard Worker 	/*
69*f7c14bbaSAndroid Build Coastguard Worker 	 * Disable the global 'bpf_objects_list'. Maintaining this list adds
70*f7c14bbaSAndroid Build Coastguard Worker 	 * a race condition to bpf_object__open() and bpf_object__close().
71*f7c14bbaSAndroid Build Coastguard Worker 	 * Clients can maintain it on their own if it is valuable for them.
72*f7c14bbaSAndroid Build Coastguard Worker 	 */
73*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_STRICT_NO_OBJECT_LIST = 0x08,
74*f7c14bbaSAndroid Build Coastguard Worker 	/*
75*f7c14bbaSAndroid Build Coastguard Worker 	 * Automatically bump RLIMIT_MEMLOCK using setrlimit() before the
76*f7c14bbaSAndroid Build Coastguard Worker 	 * first BPF program or map creation operation. This is done only if
77*f7c14bbaSAndroid Build Coastguard Worker 	 * kernel is too old to support memcg-based memory accounting for BPF
78*f7c14bbaSAndroid Build Coastguard Worker 	 * subsystem. By default, RLIMIT_MEMLOCK limit is set to RLIM_INFINITY,
79*f7c14bbaSAndroid Build Coastguard Worker 	 * but it can be overriden with libbpf_set_memlock_rlim() API.
80*f7c14bbaSAndroid Build Coastguard Worker 	 * Note that libbpf_set_memlock_rlim() needs to be called before
81*f7c14bbaSAndroid Build Coastguard Worker 	 * the very first bpf_prog_load(), bpf_map_create() or bpf_object__load()
82*f7c14bbaSAndroid Build Coastguard Worker 	 * operation.
83*f7c14bbaSAndroid Build Coastguard Worker 	 */
84*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK = 0x10,
85*f7c14bbaSAndroid Build Coastguard Worker 	/*
86*f7c14bbaSAndroid Build Coastguard Worker 	 * Error out on any SEC("maps") map definition, which are deprecated
87*f7c14bbaSAndroid Build Coastguard Worker 	 * in favor of BTF-defined map definitions in SEC(".maps").
88*f7c14bbaSAndroid Build Coastguard Worker 	 */
89*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_STRICT_MAP_DEFINITIONS = 0x20,
90*f7c14bbaSAndroid Build Coastguard Worker 
91*f7c14bbaSAndroid Build Coastguard Worker 	__LIBBPF_STRICT_LAST,
92*f7c14bbaSAndroid Build Coastguard Worker };
93*f7c14bbaSAndroid Build Coastguard Worker 
94*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API int libbpf_set_strict_mode(enum libbpf_strict_mode mode);
95*f7c14bbaSAndroid Build Coastguard Worker 
96*f7c14bbaSAndroid Build Coastguard Worker /**
97*f7c14bbaSAndroid Build Coastguard Worker  * @brief **libbpf_get_error()** extracts the error code from the passed
98*f7c14bbaSAndroid Build Coastguard Worker  * pointer
99*f7c14bbaSAndroid Build Coastguard Worker  * @param ptr pointer returned from libbpf API function
100*f7c14bbaSAndroid Build Coastguard Worker  * @return error code; or 0 if no error occured
101*f7c14bbaSAndroid Build Coastguard Worker  *
102*f7c14bbaSAndroid Build Coastguard Worker  * Note, as of libbpf 1.0 this function is not necessary and not recommended
103*f7c14bbaSAndroid Build Coastguard Worker  * to be used. Libbpf doesn't return error code embedded into the pointer
104*f7c14bbaSAndroid Build Coastguard Worker  * itself. Instead, NULL is returned on error and error code is passed through
105*f7c14bbaSAndroid Build Coastguard Worker  * thread-local errno variable. **libbpf_get_error()** is just returning -errno
106*f7c14bbaSAndroid Build Coastguard Worker  * value if it receives NULL, which is correct only if errno hasn't been
107*f7c14bbaSAndroid Build Coastguard Worker  * modified between libbpf API call and corresponding **libbpf_get_error()**
108*f7c14bbaSAndroid Build Coastguard Worker  * call. Prefer to check return for NULL and use errno directly.
109*f7c14bbaSAndroid Build Coastguard Worker  *
110*f7c14bbaSAndroid Build Coastguard Worker  * This API is left in libbpf 1.0 to allow applications that were 1.0-ready
111*f7c14bbaSAndroid Build Coastguard Worker  * before final libbpf 1.0 without needing to change them.
112*f7c14bbaSAndroid Build Coastguard Worker  */
113*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API long libbpf_get_error(const void *ptr);
114*f7c14bbaSAndroid Build Coastguard Worker 
115*f7c14bbaSAndroid Build Coastguard Worker #define DECLARE_LIBBPF_OPTS LIBBPF_OPTS
116*f7c14bbaSAndroid Build Coastguard Worker 
117*f7c14bbaSAndroid Build Coastguard Worker /* "Discouraged" APIs which don't follow consistent libbpf naming patterns.
118*f7c14bbaSAndroid Build Coastguard Worker  * They are normally a trivial aliases or wrappers for proper APIs and are
119*f7c14bbaSAndroid Build Coastguard Worker  * left to minimize unnecessary disruption for users of libbpf. But they
120*f7c14bbaSAndroid Build Coastguard Worker  * shouldn't be used going forward.
121*f7c14bbaSAndroid Build Coastguard Worker  */
122*f7c14bbaSAndroid Build Coastguard Worker 
123*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program;
124*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map;
125*f7c14bbaSAndroid Build Coastguard Worker struct btf;
126*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext;
127*f7c14bbaSAndroid Build Coastguard Worker 
128*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
129*f7c14bbaSAndroid Build Coastguard Worker 
130*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
131*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog);
132*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
133*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
134*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size);
135*f7c14bbaSAndroid Build Coastguard Worker 
136*f7c14bbaSAndroid Build Coastguard Worker #ifdef __cplusplus
137*f7c14bbaSAndroid Build Coastguard Worker } /* extern "C" */
138*f7c14bbaSAndroid Build Coastguard Worker #endif
139*f7c14bbaSAndroid Build Coastguard Worker 
140*f7c14bbaSAndroid Build Coastguard Worker #endif /* __LIBBPF_LEGACY_BPF_H */
141