xref: /openbmc/linux/tools/lib/bpf/libbpf.h (revision 5b628549)
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 <stdio.h>
14 #include <stdint.h>
15 #include <stdbool.h>
16 #include <sys/types.h>  // for size_t
17 #include <linux/bpf.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #ifndef LIBBPF_API
24 #define LIBBPF_API __attribute__((visibility("default")))
25 #endif
26 
27 enum libbpf_errno {
28 	__LIBBPF_ERRNO__START = 4000,
29 
30 	/* Something wrong in libelf */
31 	LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
32 	LIBBPF_ERRNO__FORMAT,	/* BPF object format invalid */
33 	LIBBPF_ERRNO__KVERSION,	/* Incorrect or no 'version' section */
34 	LIBBPF_ERRNO__ENDIAN,	/* Endian mismatch */
35 	LIBBPF_ERRNO__INTERNAL,	/* Internal error in libbpf */
36 	LIBBPF_ERRNO__RELOC,	/* Relocation failed */
37 	LIBBPF_ERRNO__LOAD,	/* Load program failure for unknown reason */
38 	LIBBPF_ERRNO__VERIFY,	/* Kernel verifier blocks program loading */
39 	LIBBPF_ERRNO__PROG2BIG,	/* Program too big */
40 	LIBBPF_ERRNO__KVER,	/* Incorrect kernel version */
41 	LIBBPF_ERRNO__PROGTYPE,	/* Kernel doesn't support this program type */
42 	LIBBPF_ERRNO__WRNGPID,	/* Wrong pid in netlink message */
43 	LIBBPF_ERRNO__INVSEQ,	/* Invalid netlink sequence */
44 	LIBBPF_ERRNO__NLPARSE,	/* netlink parsing error */
45 	__LIBBPF_ERRNO__END,
46 };
47 
48 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
49 
50 enum libbpf_print_level {
51         LIBBPF_WARN,
52         LIBBPF_INFO,
53         LIBBPF_DEBUG,
54 };
55 
56 typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
57 				 const char *, va_list ap);
58 
59 LIBBPF_API void libbpf_set_print(libbpf_print_fn_t fn);
60 
61 /* Hide internal to user */
62 struct bpf_object;
63 
64 struct bpf_object_open_attr {
65 	const char *file;
66 	enum bpf_prog_type prog_type;
67 };
68 
69 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
70 LIBBPF_API struct bpf_object *
71 bpf_object__open_xattr(struct bpf_object_open_attr *attr);
72 struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
73 					    int flags);
74 LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf,
75 						      size_t obj_buf_sz,
76 						      const char *name);
77 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
78 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
79 				      const char *path);
80 LIBBPF_API int bpf_object__pin_programs(struct bpf_object *obj,
81 					const char *path);
82 LIBBPF_API int bpf_object__unpin_programs(struct bpf_object *obj,
83 					  const char *path);
84 LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
85 LIBBPF_API void bpf_object__close(struct bpf_object *object);
86 
87 /* Load/unload object into/from kernel */
88 LIBBPF_API int bpf_object__load(struct bpf_object *obj);
89 LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
90 LIBBPF_API const char *bpf_object__name(struct bpf_object *obj);
91 LIBBPF_API unsigned int bpf_object__kversion(struct bpf_object *obj);
92 
93 struct btf;
94 LIBBPF_API struct btf *bpf_object__btf(struct bpf_object *obj);
95 LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
96 
97 LIBBPF_API struct bpf_program *
98 bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
99 
100 LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
101 #define bpf_object__for_each_safe(pos, tmp)			\
102 	for ((pos) = bpf_object__next(NULL),		\
103 		(tmp) = bpf_object__next(pos);		\
104 	     (pos) != NULL;				\
105 	     (pos) = (tmp), (tmp) = bpf_object__next(tmp))
106 
107 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
108 LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
109 				    bpf_object_clear_priv_t clear_priv);
110 LIBBPF_API void *bpf_object__priv(struct bpf_object *prog);
111 
112 LIBBPF_API int
113 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
114 			 enum bpf_attach_type *expected_attach_type);
115 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
116 					  enum bpf_attach_type *attach_type);
117 
118 /* Accessors of bpf_program */
119 struct bpf_program;
120 LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
121 						 struct bpf_object *obj);
122 
123 #define bpf_object__for_each_program(pos, obj)		\
124 	for ((pos) = bpf_program__next(NULL, (obj));	\
125 	     (pos) != NULL;				\
126 	     (pos) = bpf_program__next((pos), (obj)))
127 
128 LIBBPF_API struct bpf_program *bpf_program__prev(struct bpf_program *prog,
129 						 struct bpf_object *obj);
130 
131 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
132 					 void *);
133 
134 LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
135 				     bpf_program_clear_priv_t clear_priv);
136 
137 LIBBPF_API void *bpf_program__priv(struct bpf_program *prog);
138 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
139 					 __u32 ifindex);
140 
141 LIBBPF_API const char *bpf_program__title(struct bpf_program *prog,
142 					  bool needs_copy);
143 
144 LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
145 				 __u32 kern_version);
146 LIBBPF_API int bpf_program__fd(struct bpf_program *prog);
147 LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
148 					 const char *path,
149 					 int instance);
150 LIBBPF_API int bpf_program__unpin_instance(struct bpf_program *prog,
151 					   const char *path,
152 					   int instance);
153 LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
154 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
155 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
156 
157 struct bpf_insn;
158 
159 /*
160  * Libbpf allows callers to adjust BPF programs before being loaded
161  * into kernel. One program in an object file can be transformed into
162  * multiple variants to be attached to different hooks.
163  *
164  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
165  * form an API for this purpose.
166  *
167  * - bpf_program_prep_t:
168  *   Defines a 'preprocessor', which is a caller defined function
169  *   passed to libbpf through bpf_program__set_prep(), and will be
170  *   called before program is loaded. The processor should adjust
171  *   the program one time for each instance according to the instance id
172  *   passed to it.
173  *
174  * - bpf_program__set_prep:
175  *   Attaches a preprocessor to a BPF program. The number of instances
176  *   that should be created is also passed through this function.
177  *
178  * - bpf_program__nth_fd:
179  *   After the program is loaded, get resulting FD of a given instance
180  *   of the BPF program.
181  *
182  * If bpf_program__set_prep() is not used, the program would be loaded
183  * without adjustment during bpf_object__load(). The program has only
184  * one instance. In this case bpf_program__fd(prog) is equal to
185  * bpf_program__nth_fd(prog, 0).
186  */
187 
188 struct bpf_prog_prep_result {
189 	/*
190 	 * If not NULL, load new instruction array.
191 	 * If set to NULL, don't load this instance.
192 	 */
193 	struct bpf_insn *new_insn_ptr;
194 	int new_insn_cnt;
195 
196 	/* If not NULL, result FD is written to it. */
197 	int *pfd;
198 };
199 
200 /*
201  * Parameters of bpf_program_prep_t:
202  *  - prog:	The bpf_program being loaded.
203  *  - n:	Index of instance being generated.
204  *  - insns:	BPF instructions array.
205  *  - insns_cnt:Number of instructions in insns.
206  *  - res:	Output parameter, result of transformation.
207  *
208  * Return value:
209  *  - Zero:	pre-processing success.
210  *  - Non-zero:	pre-processing error, stop loading.
211  */
212 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
213 				  struct bpf_insn *insns, int insns_cnt,
214 				  struct bpf_prog_prep_result *res);
215 
216 LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
217 				     bpf_program_prep_t prep);
218 
219 LIBBPF_API int bpf_program__nth_fd(struct bpf_program *prog, int n);
220 
221 /*
222  * Adjust type of BPF program. Default is kprobe.
223  */
224 LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
225 LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
226 LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
227 LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
228 LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
229 LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
230 LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
231 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
232 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
233 				      enum bpf_prog_type type);
234 LIBBPF_API void
235 bpf_program__set_expected_attach_type(struct bpf_program *prog,
236 				      enum bpf_attach_type type);
237 
238 LIBBPF_API bool bpf_program__is_socket_filter(struct bpf_program *prog);
239 LIBBPF_API bool bpf_program__is_tracepoint(struct bpf_program *prog);
240 LIBBPF_API bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
241 LIBBPF_API bool bpf_program__is_kprobe(struct bpf_program *prog);
242 LIBBPF_API bool bpf_program__is_sched_cls(struct bpf_program *prog);
243 LIBBPF_API bool bpf_program__is_sched_act(struct bpf_program *prog);
244 LIBBPF_API bool bpf_program__is_xdp(struct bpf_program *prog);
245 LIBBPF_API bool bpf_program__is_perf_event(struct bpf_program *prog);
246 
247 /*
248  * No need for __attribute__((packed)), all members of 'bpf_map_def'
249  * are all aligned.  In addition, using __attribute__((packed))
250  * would trigger a -Wpacked warning message, and lead to an error
251  * if -Werror is set.
252  */
253 struct bpf_map_def {
254 	unsigned int type;
255 	unsigned int key_size;
256 	unsigned int value_size;
257 	unsigned int max_entries;
258 	unsigned int map_flags;
259 };
260 
261 /*
262  * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
263  * so no need to worry about a name clash.
264  */
265 struct bpf_map;
266 LIBBPF_API struct bpf_map *
267 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
268 
269 LIBBPF_API int
270 bpf_object__find_map_fd_by_name(struct bpf_object *obj, const char *name);
271 
272 /*
273  * Get bpf_map through the offset of corresponding struct bpf_map_def
274  * in the BPF object file.
275  */
276 LIBBPF_API struct bpf_map *
277 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
278 
279 LIBBPF_API struct bpf_map *
280 bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
281 #define bpf_object__for_each_map(pos, obj)		\
282 	for ((pos) = bpf_map__next(NULL, (obj));	\
283 	     (pos) != NULL;				\
284 	     (pos) = bpf_map__next((pos), (obj)))
285 #define bpf_map__for_each bpf_object__for_each_map
286 
287 LIBBPF_API struct bpf_map *
288 bpf_map__prev(struct bpf_map *map, struct bpf_object *obj);
289 
290 LIBBPF_API int bpf_map__fd(struct bpf_map *map);
291 LIBBPF_API const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
292 LIBBPF_API const char *bpf_map__name(struct bpf_map *map);
293 LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
294 LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
295 
296 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
297 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
298 				 bpf_map_clear_priv_t clear_priv);
299 LIBBPF_API void *bpf_map__priv(struct bpf_map *map);
300 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
301 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
302 LIBBPF_API bool bpf_map__is_offload_neutral(struct bpf_map *map);
303 LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
304 LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
305 LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path);
306 
307 LIBBPF_API int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd);
308 
309 LIBBPF_API long libbpf_get_error(const void *ptr);
310 
311 struct bpf_prog_load_attr {
312 	const char *file;
313 	enum bpf_prog_type prog_type;
314 	enum bpf_attach_type expected_attach_type;
315 	int ifindex;
316 };
317 
318 LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
319 				   struct bpf_object **pobj, int *prog_fd);
320 LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
321 			     struct bpf_object **pobj, int *prog_fd);
322 
323 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
324 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
325 
326 enum bpf_perf_event_ret {
327 	LIBBPF_PERF_EVENT_DONE	= 0,
328 	LIBBPF_PERF_EVENT_ERROR	= -1,
329 	LIBBPF_PERF_EVENT_CONT	= -2,
330 };
331 
332 struct perf_event_header;
333 typedef enum bpf_perf_event_ret
334 	(*bpf_perf_event_print_t)(struct perf_event_header *hdr,
335 				  void *private_data);
336 LIBBPF_API enum bpf_perf_event_ret
337 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
338 			   void **copy_mem, size_t *copy_size,
339 			   bpf_perf_event_print_t fn, void *private_data);
340 
341 struct nlattr;
342 typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
343 int libbpf_netlink_open(unsigned int *nl_pid);
344 int libbpf_nl_get_link(int sock, unsigned int nl_pid,
345 		       libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
346 int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
347 			libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
348 int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
349 			libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
350 int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
351 			 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
352 
353 struct bpf_prog_linfo;
354 struct bpf_prog_info;
355 
356 LIBBPF_API void bpf_prog_linfo__free(struct bpf_prog_linfo *prog_linfo);
357 LIBBPF_API struct bpf_prog_linfo *
358 bpf_prog_linfo__new(const struct bpf_prog_info *info);
359 LIBBPF_API const struct bpf_line_info *
360 bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
361 				__u64 addr, __u32 func_idx, __u32 nr_skip);
362 LIBBPF_API const struct bpf_line_info *
363 bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
364 		      __u32 insn_off, __u32 nr_skip);
365 
366 /*
367  * Probe for supported system features
368  *
369  * Note that running many of these probes in a short amount of time can cause
370  * the kernel to reach the maximal size of lockable memory allowed for the
371  * user, causing subsequent probes to fail. In this case, the caller may want
372  * to adjust that limit with setrlimit().
373  */
374 LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type,
375 				    __u32 ifindex);
376 LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
377 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
378 				 enum bpf_prog_type prog_type, __u32 ifindex);
379 
380 #ifdef __cplusplus
381 } /* extern "C" */
382 #endif
383 
384 #endif /* __LIBBPF_LIBBPF_H */
385