1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
4 * Common eBPF ELF object loading operations.
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.
10 #ifndef __LIBBPF_LIBBPF_H
11 #define __LIBBPF_LIBBPF_H
16 #include <sys/types.h> // for size_t
17 #include <linux/bpf.h>
20 __LIBBPF_ERRNO__START = 4000,
22 /* Something wrong in libelf */
23 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
24 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
25 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
26 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
27 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
28 LIBBPF_ERRNO__RELOC, /* Relocation failed */
29 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
30 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
31 LIBBPF_ERRNO__PROG2BIG, /* Program too big */
32 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
33 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
34 LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
35 LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
36 LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
40 int libbpf_strerror(int err, char *buf, size_t size);
43 * __printf is defined in include/linux/compiler-gcc.h. However,
44 * it would be better if libbpf.h didn't depend on Linux header files.
45 * So instead of __printf, here we use gcc attribute directly.
47 typedef int (*libbpf_print_fn_t)(const char *, ...)
48 __attribute__((format(printf, 1, 2)));
50 void libbpf_set_print(libbpf_print_fn_t warn,
51 libbpf_print_fn_t info,
52 libbpf_print_fn_t debug);
54 /* Hide internal to user */
57 struct bpf_object_open_attr {
59 enum bpf_prog_type prog_type;
62 struct bpf_object *bpf_object__open(const char *path);
63 struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr);
64 struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
66 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
69 int bpf_object__pin(struct bpf_object *object, const char *path);
70 void bpf_object__close(struct bpf_object *object);
72 /* Load/unload object into/from kernel */
73 int bpf_object__load(struct bpf_object *obj);
74 int bpf_object__unload(struct bpf_object *obj);
75 const char *bpf_object__name(struct bpf_object *obj);
76 unsigned int bpf_object__kversion(struct bpf_object *obj);
77 int bpf_object__btf_fd(const struct bpf_object *obj);
80 bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
82 struct bpf_object *bpf_object__next(struct bpf_object *prev);
83 #define bpf_object__for_each_safe(pos, tmp) \
84 for ((pos) = bpf_object__next(NULL), \
85 (tmp) = bpf_object__next(pos); \
87 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
89 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
90 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
91 bpf_object_clear_priv_t clear_priv);
92 void *bpf_object__priv(struct bpf_object *prog);
94 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
95 enum bpf_attach_type *expected_attach_type);
96 int libbpf_attach_type_by_name(const char *name,
97 enum bpf_attach_type *attach_type);
99 /* Accessors of bpf_program */
101 struct bpf_program *bpf_program__next(struct bpf_program *prog,
102 struct bpf_object *obj);
104 #define bpf_object__for_each_program(pos, obj) \
105 for ((pos) = bpf_program__next(NULL, (obj)); \
107 (pos) = bpf_program__next((pos), (obj)))
109 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
112 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
113 bpf_program_clear_priv_t clear_priv);
115 void *bpf_program__priv(struct bpf_program *prog);
116 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex);
118 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
120 int bpf_program__load(struct bpf_program *prog, char *license,
122 int bpf_program__fd(struct bpf_program *prog);
123 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
125 int bpf_program__pin(struct bpf_program *prog, const char *path);
126 void bpf_program__unload(struct bpf_program *prog);
131 * Libbpf allows callers to adjust BPF programs before being loaded
132 * into kernel. One program in an object file can be transformed into
133 * multiple variants to be attached to different hooks.
135 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
136 * form an API for this purpose.
138 * - bpf_program_prep_t:
139 * Defines a 'preprocessor', which is a caller defined function
140 * passed to libbpf through bpf_program__set_prep(), and will be
141 * called before program is loaded. The processor should adjust
142 * the program one time for each instance according to the instance id
145 * - bpf_program__set_prep:
146 * Attaches a preprocessor to a BPF program. The number of instances
147 * that should be created is also passed through this function.
149 * - bpf_program__nth_fd:
150 * After the program is loaded, get resulting FD of a given instance
151 * of the BPF program.
153 * If bpf_program__set_prep() is not used, the program would be loaded
154 * without adjustment during bpf_object__load(). The program has only
155 * one instance. In this case bpf_program__fd(prog) is equal to
156 * bpf_program__nth_fd(prog, 0).
159 struct bpf_prog_prep_result {
161 * If not NULL, load new instruction array.
162 * If set to NULL, don't load this instance.
164 struct bpf_insn *new_insn_ptr;
167 /* If not NULL, result FD is written to it. */
172 * Parameters of bpf_program_prep_t:
173 * - prog: The bpf_program being loaded.
174 * - n: Index of instance being generated.
175 * - insns: BPF instructions array.
176 * - insns_cnt:Number of instructions in insns.
177 * - res: Output parameter, result of transformation.
180 * - Zero: pre-processing success.
181 * - Non-zero: pre-processing error, stop loading.
183 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
184 struct bpf_insn *insns, int insns_cnt,
185 struct bpf_prog_prep_result *res);
187 int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
188 bpf_program_prep_t prep);
190 int bpf_program__nth_fd(struct bpf_program *prog, int n);
193 * Adjust type of BPF program. Default is kprobe.
195 int bpf_program__set_socket_filter(struct bpf_program *prog);
196 int bpf_program__set_tracepoint(struct bpf_program *prog);
197 int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
198 int bpf_program__set_kprobe(struct bpf_program *prog);
199 int bpf_program__set_sched_cls(struct bpf_program *prog);
200 int bpf_program__set_sched_act(struct bpf_program *prog);
201 int bpf_program__set_xdp(struct bpf_program *prog);
202 int bpf_program__set_perf_event(struct bpf_program *prog);
203 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);
204 void bpf_program__set_expected_attach_type(struct bpf_program *prog,
205 enum bpf_attach_type type);
207 bool bpf_program__is_socket_filter(struct bpf_program *prog);
208 bool bpf_program__is_tracepoint(struct bpf_program *prog);
209 bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
210 bool bpf_program__is_kprobe(struct bpf_program *prog);
211 bool bpf_program__is_sched_cls(struct bpf_program *prog);
212 bool bpf_program__is_sched_act(struct bpf_program *prog);
213 bool bpf_program__is_xdp(struct bpf_program *prog);
214 bool bpf_program__is_perf_event(struct bpf_program *prog);
217 * No need for __attribute__((packed)), all members of 'bpf_map_def'
218 * are all aligned. In addition, using __attribute__((packed))
219 * would trigger a -Wpacked warning message, and lead to an error
224 unsigned int key_size;
225 unsigned int value_size;
226 unsigned int max_entries;
227 unsigned int map_flags;
231 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
232 * so no need to worry about a name clash.
236 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
239 * Get bpf_map through the offset of corresponding struct bpf_map_def
240 * in the BPF object file.
243 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
246 bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
247 #define bpf_map__for_each(pos, obj) \
248 for ((pos) = bpf_map__next(NULL, (obj)); \
250 (pos) = bpf_map__next((pos), (obj)))
252 int bpf_map__fd(struct bpf_map *map);
253 const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
254 const char *bpf_map__name(struct bpf_map *map);
255 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
256 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
258 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
259 int bpf_map__set_priv(struct bpf_map *map, void *priv,
260 bpf_map_clear_priv_t clear_priv);
261 void *bpf_map__priv(struct bpf_map *map);
262 int bpf_map__reuse_fd(struct bpf_map *map, int fd);
263 bool bpf_map__is_offload_neutral(struct bpf_map *map);
264 void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
265 int bpf_map__pin(struct bpf_map *map, const char *path);
267 long libbpf_get_error(const void *ptr);
269 struct bpf_prog_load_attr {
271 enum bpf_prog_type prog_type;
272 enum bpf_attach_type expected_attach_type;
276 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
277 struct bpf_object **pobj, int *prog_fd);
278 int bpf_prog_load(const char *file, enum bpf_prog_type type,
279 struct bpf_object **pobj, int *prog_fd);
281 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
283 enum bpf_perf_event_ret {
284 LIBBPF_PERF_EVENT_DONE = 0,
285 LIBBPF_PERF_EVENT_ERROR = -1,
286 LIBBPF_PERF_EVENT_CONT = -2,
289 typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(void *event,
291 int bpf_perf_event_read_simple(void *mem, unsigned long size,
292 unsigned long page_size,
293 void **buf, size_t *buf_len,
294 bpf_perf_event_print_t fn, void *priv);
297 typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
298 int libbpf_netlink_open(unsigned int *nl_pid);
299 int libbpf_nl_get_link(int sock, unsigned int nl_pid,
300 libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
301 int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
302 libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
303 int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
304 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
305 int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
306 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
307 #endif /* __LIBBPF_LIBBPF_H */