xref: /linux/tools/lib/bpf/libbpf.h (revision 49f2cba3e57a4d71e3e7001cc2934b563ee495f4)
1 /* SPDX-License-Identifier: LGPL-2.1 */
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  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation;
13  * version 2.1 of the License (not later!)
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this program; if not,  see <http://www.gnu.org/licenses>
22  */
23 #ifndef __BPF_LIBBPF_H
24 #define __BPF_LIBBPF_H
25 
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <sys/types.h>  // for size_t
30 #include <linux/bpf.h>
31 
32 enum libbpf_errno {
33 	__LIBBPF_ERRNO__START = 4000,
34 
35 	/* Something wrong in libelf */
36 	LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
37 	LIBBPF_ERRNO__FORMAT,	/* BPF object format invalid */
38 	LIBBPF_ERRNO__KVERSION,	/* Incorrect or no 'version' section */
39 	LIBBPF_ERRNO__ENDIAN,	/* Endian mismatch */
40 	LIBBPF_ERRNO__INTERNAL,	/* Internal error in libbpf */
41 	LIBBPF_ERRNO__RELOC,	/* Relocation failed */
42 	LIBBPF_ERRNO__LOAD,	/* Load program failure for unknown reason */
43 	LIBBPF_ERRNO__VERIFY,	/* Kernel verifier blocks program loading */
44 	LIBBPF_ERRNO__PROG2BIG,	/* Program too big */
45 	LIBBPF_ERRNO__KVER,	/* Incorrect kernel version */
46 	LIBBPF_ERRNO__PROGTYPE,	/* Kernel doesn't support this program type */
47 	LIBBPF_ERRNO__WRNGPID,	/* Wrong pid in netlink message */
48 	LIBBPF_ERRNO__INVSEQ,	/* Invalid netlink sequence */
49 	__LIBBPF_ERRNO__END,
50 };
51 
52 int libbpf_strerror(int err, char *buf, size_t size);
53 
54 /*
55  * __printf is defined in include/linux/compiler-gcc.h. However,
56  * it would be better if libbpf.h didn't depend on Linux header files.
57  * So instead of __printf, here we use gcc attribute directly.
58  */
59 typedef int (*libbpf_print_fn_t)(const char *, ...)
60 	__attribute__((format(printf, 1, 2)));
61 
62 void libbpf_set_print(libbpf_print_fn_t warn,
63 		      libbpf_print_fn_t info,
64 		      libbpf_print_fn_t debug);
65 
66 /* Hide internal to user */
67 struct bpf_object;
68 
69 struct bpf_object *bpf_object__open(const char *path);
70 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
71 					   size_t obj_buf_sz,
72 					   const char *name);
73 int bpf_object__pin(struct bpf_object *object, const char *path);
74 void bpf_object__close(struct bpf_object *object);
75 
76 /* Load/unload object into/from kernel */
77 int bpf_object__load(struct bpf_object *obj);
78 int bpf_object__unload(struct bpf_object *obj);
79 const char *bpf_object__name(struct bpf_object *obj);
80 unsigned int bpf_object__kversion(struct bpf_object *obj);
81 int bpf_object__btf_fd(const struct bpf_object *obj);
82 
83 struct bpf_object *bpf_object__next(struct bpf_object *prev);
84 #define bpf_object__for_each_safe(pos, tmp)			\
85 	for ((pos) = bpf_object__next(NULL),		\
86 		(tmp) = bpf_object__next(pos);		\
87 	     (pos) != NULL;				\
88 	     (pos) = (tmp), (tmp) = bpf_object__next(tmp))
89 
90 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
91 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
92 			 bpf_object_clear_priv_t clear_priv);
93 void *bpf_object__priv(struct bpf_object *prog);
94 
95 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
96 			     enum bpf_attach_type *expected_attach_type);
97 
98 /* Accessors of bpf_program */
99 struct bpf_program;
100 struct bpf_program *bpf_program__next(struct bpf_program *prog,
101 				      struct bpf_object *obj);
102 
103 #define bpf_object__for_each_program(pos, obj)		\
104 	for ((pos) = bpf_program__next(NULL, (obj));	\
105 	     (pos) != NULL;				\
106 	     (pos) = bpf_program__next((pos), (obj)))
107 
108 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
109 					 void *);
110 
111 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
112 			  bpf_program_clear_priv_t clear_priv);
113 
114 void *bpf_program__priv(struct bpf_program *prog);
115 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex);
116 
117 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
118 
119 int bpf_program__fd(struct bpf_program *prog);
120 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
121 			      int instance);
122 int bpf_program__pin(struct bpf_program *prog, const char *path);
123 
124 struct bpf_insn;
125 
126 /*
127  * Libbpf allows callers to adjust BPF programs before being loaded
128  * into kernel. One program in an object file can be transformed into
129  * multiple variants to be attached to different hooks.
130  *
131  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
132  * form an API for this purpose.
133  *
134  * - bpf_program_prep_t:
135  *   Defines a 'preprocessor', which is a caller defined function
136  *   passed to libbpf through bpf_program__set_prep(), and will be
137  *   called before program is loaded. The processor should adjust
138  *   the program one time for each instance according to the instance id
139  *   passed to it.
140  *
141  * - bpf_program__set_prep:
142  *   Attaches a preprocessor to a BPF program. The number of instances
143  *   that should be created is also passed through this function.
144  *
145  * - bpf_program__nth_fd:
146  *   After the program is loaded, get resulting FD of a given instance
147  *   of the BPF program.
148  *
149  * If bpf_program__set_prep() is not used, the program would be loaded
150  * without adjustment during bpf_object__load(). The program has only
151  * one instance. In this case bpf_program__fd(prog) is equal to
152  * bpf_program__nth_fd(prog, 0).
153  */
154 
155 struct bpf_prog_prep_result {
156 	/*
157 	 * If not NULL, load new instruction array.
158 	 * If set to NULL, don't load this instance.
159 	 */
160 	struct bpf_insn *new_insn_ptr;
161 	int new_insn_cnt;
162 
163 	/* If not NULL, result FD is written to it. */
164 	int *pfd;
165 };
166 
167 /*
168  * Parameters of bpf_program_prep_t:
169  *  - prog:	The bpf_program being loaded.
170  *  - n:	Index of instance being generated.
171  *  - insns:	BPF instructions array.
172  *  - insns_cnt:Number of instructions in insns.
173  *  - res:	Output parameter, result of transformation.
174  *
175  * Return value:
176  *  - Zero:	pre-processing success.
177  *  - Non-zero:	pre-processing error, stop loading.
178  */
179 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
180 				  struct bpf_insn *insns, int insns_cnt,
181 				  struct bpf_prog_prep_result *res);
182 
183 int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
184 			  bpf_program_prep_t prep);
185 
186 int bpf_program__nth_fd(struct bpf_program *prog, int n);
187 
188 /*
189  * Adjust type of BPF program. Default is kprobe.
190  */
191 int bpf_program__set_socket_filter(struct bpf_program *prog);
192 int bpf_program__set_tracepoint(struct bpf_program *prog);
193 int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
194 int bpf_program__set_kprobe(struct bpf_program *prog);
195 int bpf_program__set_sched_cls(struct bpf_program *prog);
196 int bpf_program__set_sched_act(struct bpf_program *prog);
197 int bpf_program__set_xdp(struct bpf_program *prog);
198 int bpf_program__set_perf_event(struct bpf_program *prog);
199 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);
200 void bpf_program__set_expected_attach_type(struct bpf_program *prog,
201 					   enum bpf_attach_type type);
202 
203 bool bpf_program__is_socket_filter(struct bpf_program *prog);
204 bool bpf_program__is_tracepoint(struct bpf_program *prog);
205 bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
206 bool bpf_program__is_kprobe(struct bpf_program *prog);
207 bool bpf_program__is_sched_cls(struct bpf_program *prog);
208 bool bpf_program__is_sched_act(struct bpf_program *prog);
209 bool bpf_program__is_xdp(struct bpf_program *prog);
210 bool bpf_program__is_perf_event(struct bpf_program *prog);
211 
212 /*
213  * No need for __attribute__((packed)), all members of 'bpf_map_def'
214  * are all aligned.  In addition, using __attribute__((packed))
215  * would trigger a -Wpacked warning message, and lead to an error
216  * if -Werror is set.
217  */
218 struct bpf_map_def {
219 	unsigned int type;
220 	unsigned int key_size;
221 	unsigned int value_size;
222 	unsigned int max_entries;
223 	unsigned int map_flags;
224 };
225 
226 /*
227  * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
228  * so no need to worry about a name clash.
229  */
230 struct bpf_map;
231 struct bpf_map *
232 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
233 
234 /*
235  * Get bpf_map through the offset of corresponding struct bpf_map_def
236  * in the BPF object file.
237  */
238 struct bpf_map *
239 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
240 
241 struct bpf_map *
242 bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
243 #define bpf_map__for_each(pos, obj)		\
244 	for ((pos) = bpf_map__next(NULL, (obj));	\
245 	     (pos) != NULL;				\
246 	     (pos) = bpf_map__next((pos), (obj)))
247 
248 int bpf_map__fd(struct bpf_map *map);
249 const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
250 const char *bpf_map__name(struct bpf_map *map);
251 uint32_t bpf_map__btf_key_type_id(const struct bpf_map *map);
252 uint32_t bpf_map__btf_value_type_id(const struct bpf_map *map);
253 
254 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
255 int bpf_map__set_priv(struct bpf_map *map, void *priv,
256 		      bpf_map_clear_priv_t clear_priv);
257 void *bpf_map__priv(struct bpf_map *map);
258 void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
259 int bpf_map__pin(struct bpf_map *map, const char *path);
260 
261 long libbpf_get_error(const void *ptr);
262 
263 struct bpf_prog_load_attr {
264 	const char *file;
265 	enum bpf_prog_type prog_type;
266 	enum bpf_attach_type expected_attach_type;
267 	int ifindex;
268 };
269 
270 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
271 			struct bpf_object **pobj, int *prog_fd);
272 int bpf_prog_load(const char *file, enum bpf_prog_type type,
273 		  struct bpf_object **pobj, int *prog_fd);
274 
275 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
276 
277 enum bpf_perf_event_ret {
278 	LIBBPF_PERF_EVENT_DONE	= 0,
279 	LIBBPF_PERF_EVENT_ERROR	= -1,
280 	LIBBPF_PERF_EVENT_CONT	= -2,
281 };
282 
283 typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(void *event,
284 							  void *priv);
285 int bpf_perf_event_read_simple(void *mem, unsigned long size,
286 			       unsigned long page_size,
287 			       void **buf, size_t *buf_len,
288 			       bpf_perf_event_print_t fn, void *priv);
289 #endif
290