xref: /linux/tools/lib/bpf/libbpf.h (revision b7d3826c2ed6c3e626e7ae796c5df2c0d2551c6a)
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 enum libbpf_errno {
20 	__LIBBPF_ERRNO__START = 4000,
21 
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 */
37 	__LIBBPF_ERRNO__END,
38 };
39 
40 int libbpf_strerror(int err, char *buf, size_t size);
41 
42 /*
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.
46  */
47 typedef int (*libbpf_print_fn_t)(const char *, ...)
48 	__attribute__((format(printf, 1, 2)));
49 
50 void libbpf_set_print(libbpf_print_fn_t warn,
51 		      libbpf_print_fn_t info,
52 		      libbpf_print_fn_t debug);
53 
54 /* Hide internal to user */
55 struct bpf_object;
56 
57 struct bpf_object_open_attr {
58 	const char *file;
59 	enum bpf_prog_type prog_type;
60 };
61 
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_buffer(void *obj_buf,
65 					   size_t obj_buf_sz,
66 					   const char *name);
67 int bpf_object__pin(struct bpf_object *object, const char *path);
68 void bpf_object__close(struct bpf_object *object);
69 
70 /* Load/unload object into/from kernel */
71 int bpf_object__load(struct bpf_object *obj);
72 int bpf_object__unload(struct bpf_object *obj);
73 const char *bpf_object__name(struct bpf_object *obj);
74 unsigned int bpf_object__kversion(struct bpf_object *obj);
75 int bpf_object__btf_fd(const struct bpf_object *obj);
76 
77 struct bpf_program *
78 bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
79 
80 struct bpf_object *bpf_object__next(struct bpf_object *prev);
81 #define bpf_object__for_each_safe(pos, tmp)			\
82 	for ((pos) = bpf_object__next(NULL),		\
83 		(tmp) = bpf_object__next(pos);		\
84 	     (pos) != NULL;				\
85 	     (pos) = (tmp), (tmp) = bpf_object__next(tmp))
86 
87 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
88 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
89 			 bpf_object_clear_priv_t clear_priv);
90 void *bpf_object__priv(struct bpf_object *prog);
91 
92 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
93 			     enum bpf_attach_type *expected_attach_type);
94 int libbpf_attach_type_by_name(const char *name,
95 			       enum bpf_attach_type *attach_type);
96 
97 /* Accessors of bpf_program */
98 struct bpf_program;
99 struct bpf_program *bpf_program__next(struct bpf_program *prog,
100 				      struct bpf_object *obj);
101 
102 #define bpf_object__for_each_program(pos, obj)		\
103 	for ((pos) = bpf_program__next(NULL, (obj));	\
104 	     (pos) != NULL;				\
105 	     (pos) = bpf_program__next((pos), (obj)))
106 
107 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
108 					 void *);
109 
110 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
111 			  bpf_program_clear_priv_t clear_priv);
112 
113 void *bpf_program__priv(struct bpf_program *prog);
114 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex);
115 
116 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
117 
118 int bpf_program__load(struct bpf_program *prog, char *license,
119 		      __u32 kern_version);
120 int bpf_program__fd(struct bpf_program *prog);
121 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
122 			      int instance);
123 int bpf_program__pin(struct bpf_program *prog, const char *path);
124 void bpf_program__unload(struct bpf_program *prog);
125 
126 struct bpf_insn;
127 
128 /*
129  * Libbpf allows callers to adjust BPF programs before being loaded
130  * into kernel. One program in an object file can be transformed into
131  * multiple variants to be attached to different hooks.
132  *
133  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
134  * form an API for this purpose.
135  *
136  * - bpf_program_prep_t:
137  *   Defines a 'preprocessor', which is a caller defined function
138  *   passed to libbpf through bpf_program__set_prep(), and will be
139  *   called before program is loaded. The processor should adjust
140  *   the program one time for each instance according to the instance id
141  *   passed to it.
142  *
143  * - bpf_program__set_prep:
144  *   Attaches a preprocessor to a BPF program. The number of instances
145  *   that should be created is also passed through this function.
146  *
147  * - bpf_program__nth_fd:
148  *   After the program is loaded, get resulting FD of a given instance
149  *   of the BPF program.
150  *
151  * If bpf_program__set_prep() is not used, the program would be loaded
152  * without adjustment during bpf_object__load(). The program has only
153  * one instance. In this case bpf_program__fd(prog) is equal to
154  * bpf_program__nth_fd(prog, 0).
155  */
156 
157 struct bpf_prog_prep_result {
158 	/*
159 	 * If not NULL, load new instruction array.
160 	 * If set to NULL, don't load this instance.
161 	 */
162 	struct bpf_insn *new_insn_ptr;
163 	int new_insn_cnt;
164 
165 	/* If not NULL, result FD is written to it. */
166 	int *pfd;
167 };
168 
169 /*
170  * Parameters of bpf_program_prep_t:
171  *  - prog:	The bpf_program being loaded.
172  *  - n:	Index of instance being generated.
173  *  - insns:	BPF instructions array.
174  *  - insns_cnt:Number of instructions in insns.
175  *  - res:	Output parameter, result of transformation.
176  *
177  * Return value:
178  *  - Zero:	pre-processing success.
179  *  - Non-zero:	pre-processing error, stop loading.
180  */
181 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
182 				  struct bpf_insn *insns, int insns_cnt,
183 				  struct bpf_prog_prep_result *res);
184 
185 int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
186 			  bpf_program_prep_t prep);
187 
188 int bpf_program__nth_fd(struct bpf_program *prog, int n);
189 
190 /*
191  * Adjust type of BPF program. Default is kprobe.
192  */
193 int bpf_program__set_socket_filter(struct bpf_program *prog);
194 int bpf_program__set_tracepoint(struct bpf_program *prog);
195 int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
196 int bpf_program__set_kprobe(struct bpf_program *prog);
197 int bpf_program__set_sched_cls(struct bpf_program *prog);
198 int bpf_program__set_sched_act(struct bpf_program *prog);
199 int bpf_program__set_xdp(struct bpf_program *prog);
200 int bpf_program__set_perf_event(struct bpf_program *prog);
201 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);
202 void bpf_program__set_expected_attach_type(struct bpf_program *prog,
203 					   enum bpf_attach_type type);
204 
205 bool bpf_program__is_socket_filter(struct bpf_program *prog);
206 bool bpf_program__is_tracepoint(struct bpf_program *prog);
207 bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
208 bool bpf_program__is_kprobe(struct bpf_program *prog);
209 bool bpf_program__is_sched_cls(struct bpf_program *prog);
210 bool bpf_program__is_sched_act(struct bpf_program *prog);
211 bool bpf_program__is_xdp(struct bpf_program *prog);
212 bool bpf_program__is_perf_event(struct bpf_program *prog);
213 
214 /*
215  * No need for __attribute__((packed)), all members of 'bpf_map_def'
216  * are all aligned.  In addition, using __attribute__((packed))
217  * would trigger a -Wpacked warning message, and lead to an error
218  * if -Werror is set.
219  */
220 struct bpf_map_def {
221 	unsigned int type;
222 	unsigned int key_size;
223 	unsigned int value_size;
224 	unsigned int max_entries;
225 	unsigned int map_flags;
226 };
227 
228 /*
229  * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
230  * so no need to worry about a name clash.
231  */
232 struct bpf_map;
233 struct bpf_map *
234 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
235 
236 /*
237  * Get bpf_map through the offset of corresponding struct bpf_map_def
238  * in the BPF object file.
239  */
240 struct bpf_map *
241 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
242 
243 struct bpf_map *
244 bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
245 #define bpf_map__for_each(pos, obj)		\
246 	for ((pos) = bpf_map__next(NULL, (obj));	\
247 	     (pos) != NULL;				\
248 	     (pos) = bpf_map__next((pos), (obj)))
249 
250 int bpf_map__fd(struct bpf_map *map);
251 const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
252 const char *bpf_map__name(struct bpf_map *map);
253 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
254 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
255 
256 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
257 int bpf_map__set_priv(struct bpf_map *map, void *priv,
258 		      bpf_map_clear_priv_t clear_priv);
259 void *bpf_map__priv(struct bpf_map *map);
260 int bpf_map__reuse_fd(struct bpf_map *map, int fd);
261 bool bpf_map__is_offload_neutral(struct bpf_map *map);
262 void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
263 int bpf_map__pin(struct bpf_map *map, const char *path);
264 
265 long libbpf_get_error(const void *ptr);
266 
267 struct bpf_prog_load_attr {
268 	const char *file;
269 	enum bpf_prog_type prog_type;
270 	enum bpf_attach_type expected_attach_type;
271 	int ifindex;
272 };
273 
274 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
275 			struct bpf_object **pobj, int *prog_fd);
276 int bpf_prog_load(const char *file, enum bpf_prog_type type,
277 		  struct bpf_object **pobj, int *prog_fd);
278 
279 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
280 
281 enum bpf_perf_event_ret {
282 	LIBBPF_PERF_EVENT_DONE	= 0,
283 	LIBBPF_PERF_EVENT_ERROR	= -1,
284 	LIBBPF_PERF_EVENT_CONT	= -2,
285 };
286 
287 typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(void *event,
288 							  void *priv);
289 int bpf_perf_event_read_simple(void *mem, unsigned long size,
290 			       unsigned long page_size,
291 			       void **buf, size_t *buf_len,
292 			       bpf_perf_event_print_t fn, void *priv);
293 
294 struct nlattr;
295 typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
296 int libbpf_netlink_open(unsigned int *nl_pid);
297 int libbpf_nl_get_link(int sock, unsigned int nl_pid,
298 		       libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
299 int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
300 			libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
301 int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
302 			libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
303 int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
304 			 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
305 #endif /* __LIBBPF_LIBBPF_H */
306