xref: /openbmc/linux/tools/lib/bpf/bpf.h (revision 47327e198d42c77322dbe175817499d2d7ddc26a)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 /*
4  * common eBPF ELF 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 __LIBBPF_BPF_H
24 #define __LIBBPF_BPF_H
25 
26 #include <linux/bpf.h>
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 
31 #include "libbpf_common.h"
32 #include "libbpf_legacy.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 struct bpf_create_map_attr {
39 	const char *name;
40 	enum bpf_map_type map_type;
41 	__u32 map_flags;
42 	__u32 key_size;
43 	__u32 value_size;
44 	__u32 max_entries;
45 	__u32 numa_node;
46 	__u32 btf_fd;
47 	__u32 btf_key_type_id;
48 	__u32 btf_value_type_id;
49 	__u32 map_ifindex;
50 	union {
51 		__u32 inner_map_fd;
52 		__u32 btf_vmlinux_value_type_id;
53 	};
54 };
55 
56 LIBBPF_API int
57 bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
58 LIBBPF_API int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
59 				   int key_size, int value_size,
60 				   int max_entries, __u32 map_flags, int node);
61 LIBBPF_API int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
62 				   int key_size, int value_size,
63 				   int max_entries, __u32 map_flags);
64 LIBBPF_API int bpf_create_map(enum bpf_map_type map_type, int key_size,
65 			      int value_size, int max_entries, __u32 map_flags);
66 LIBBPF_API int bpf_create_map_in_map_node(enum bpf_map_type map_type,
67 					  const char *name, int key_size,
68 					  int inner_map_fd, int max_entries,
69 					  __u32 map_flags, int node);
70 LIBBPF_API int bpf_create_map_in_map(enum bpf_map_type map_type,
71 				     const char *name, int key_size,
72 				     int inner_map_fd, int max_entries,
73 				     __u32 map_flags);
74 
75 struct bpf_prog_load_opts {
76 	size_t sz; /* size of this struct for forward/backward compatibility */
77 
78 	/* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns
79 	 * -EAGAIN. This field determines how many attempts libbpf has to
80 	 *  make. If not specified, libbpf will use default value of 5.
81 	 */
82 	int attempts;
83 
84 	enum bpf_attach_type expected_attach_type;
85 	__u32 prog_btf_fd;
86 	__u32 prog_flags;
87 	__u32 prog_ifindex;
88 	__u32 kern_version;
89 
90 	__u32 attach_btf_id;
91 	__u32 attach_prog_fd;
92 	__u32 attach_btf_obj_fd;
93 
94 	const int *fd_array;
95 
96 	/* .BTF.ext func info data */
97 	const void *func_info;
98 	__u32 func_info_cnt;
99 	__u32 func_info_rec_size;
100 
101 	/* .BTF.ext line info data */
102 	const void *line_info;
103 	__u32 line_info_cnt;
104 	__u32 line_info_rec_size;
105 
106 	/* verifier log options */
107 	__u32 log_level;
108 	__u32 log_size;
109 	char *log_buf;
110 };
111 #define bpf_prog_load_opts__last_field log_buf
112 
113 LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
114 			     const char *prog_name, const char *license,
115 			     const struct bpf_insn *insns, size_t insn_cnt,
116 			     const struct bpf_prog_load_opts *opts);
117 /* this "specialization" should go away in libbpf 1.0 */
118 LIBBPF_API int bpf_prog_load_v0_6_0(enum bpf_prog_type prog_type,
119 				    const char *prog_name, const char *license,
120 				    const struct bpf_insn *insns, size_t insn_cnt,
121 				    const struct bpf_prog_load_opts *opts);
122 
123 /* This is an elaborate way to not conflict with deprecated bpf_prog_load()
124  * API, defined in libbpf.h. Once we hit libbpf 1.0, all this will be gone.
125  * With this approach, if someone is calling bpf_prog_load() with
126  * 4 arguments, they will use the deprecated API, which keeps backwards
127  * compatibility (both source code and binary). If bpf_prog_load() is called
128  * with 6 arguments, though, it gets redirected to __bpf_prog_load.
129  * So looking forward to libbpf 1.0 when this hack will be gone and
130  * __bpf_prog_load() will be called just bpf_prog_load().
131  */
132 #ifndef bpf_prog_load
133 #define bpf_prog_load(...) ___libbpf_overload(___bpf_prog_load, __VA_ARGS__)
134 #define ___bpf_prog_load4(file, type, pobj, prog_fd) \
135 	bpf_prog_load_deprecated(file, type, pobj, prog_fd)
136 #define ___bpf_prog_load6(prog_type, prog_name, license, insns, insn_cnt, opts) \
137 	bpf_prog_load(prog_type, prog_name, license, insns, insn_cnt, opts)
138 #endif /* bpf_prog_load */
139 
140 struct bpf_load_program_attr {
141 	enum bpf_prog_type prog_type;
142 	enum bpf_attach_type expected_attach_type;
143 	const char *name;
144 	const struct bpf_insn *insns;
145 	size_t insns_cnt;
146 	const char *license;
147 	union {
148 		__u32 kern_version;
149 		__u32 attach_prog_fd;
150 	};
151 	union {
152 		__u32 prog_ifindex;
153 		__u32 attach_btf_id;
154 	};
155 	__u32 prog_btf_fd;
156 	__u32 func_info_rec_size;
157 	const void *func_info;
158 	__u32 func_info_cnt;
159 	__u32 line_info_rec_size;
160 	const void *line_info;
161 	__u32 line_info_cnt;
162 	__u32 log_level;
163 	__u32 prog_flags;
164 };
165 
166 /* Flags to direct loading requirements */
167 #define MAPS_RELAX_COMPAT	0x01
168 
169 /* Recommend log buffer size */
170 #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
171 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
172 LIBBPF_API int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
173 				      char *log_buf, size_t log_buf_sz);
174 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
175 LIBBPF_API int bpf_load_program(enum bpf_prog_type type,
176 				const struct bpf_insn *insns, size_t insns_cnt,
177 				const char *license, __u32 kern_version,
178 				char *log_buf, size_t log_buf_sz);
179 LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
180 LIBBPF_API int bpf_verify_program(enum bpf_prog_type type,
181 				  const struct bpf_insn *insns,
182 				  size_t insns_cnt, __u32 prog_flags,
183 				  const char *license, __u32 kern_version,
184 				  char *log_buf, size_t log_buf_sz,
185 				  int log_level);
186 
187 LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
188 				   __u64 flags);
189 
190 LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
191 LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value,
192 					 __u64 flags);
193 LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
194 					      void *value);
195 LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key,
196 						    void *value, __u64 flags);
197 LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
198 LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
199 LIBBPF_API int bpf_map_freeze(int fd);
200 
201 struct bpf_map_batch_opts {
202 	size_t sz; /* size of this struct for forward/backward compatibility */
203 	__u64 elem_flags;
204 	__u64 flags;
205 };
206 #define bpf_map_batch_opts__last_field flags
207 
208 LIBBPF_API int bpf_map_delete_batch(int fd, void *keys,
209 				    __u32 *count,
210 				    const struct bpf_map_batch_opts *opts);
211 LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
212 				    void *keys, void *values, __u32 *count,
213 				    const struct bpf_map_batch_opts *opts);
214 LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
215 					void *out_batch, void *keys,
216 					void *values, __u32 *count,
217 					const struct bpf_map_batch_opts *opts);
218 LIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values,
219 				    __u32 *count,
220 				    const struct bpf_map_batch_opts *opts);
221 
222 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
223 LIBBPF_API int bpf_obj_get(const char *pathname);
224 
225 struct bpf_prog_attach_opts {
226 	size_t sz; /* size of this struct for forward/backward compatibility */
227 	unsigned int flags;
228 	int replace_prog_fd;
229 };
230 #define bpf_prog_attach_opts__last_field replace_prog_fd
231 
232 LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
233 			       enum bpf_attach_type type, unsigned int flags);
234 LIBBPF_API int bpf_prog_attach_xattr(int prog_fd, int attachable_fd,
235 				     enum bpf_attach_type type,
236 				     const struct bpf_prog_attach_opts *opts);
237 LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
238 LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
239 				enum bpf_attach_type type);
240 
241 union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
242 struct bpf_link_create_opts {
243 	size_t sz; /* size of this struct for forward/backward compatibility */
244 	__u32 flags;
245 	union bpf_iter_link_info *iter_info;
246 	__u32 iter_info_len;
247 	__u32 target_btf_id;
248 	union {
249 		struct {
250 			__u64 bpf_cookie;
251 		} perf_event;
252 	};
253 	size_t :0;
254 };
255 #define bpf_link_create_opts__last_field perf_event
256 
257 LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
258 			       enum bpf_attach_type attach_type,
259 			       const struct bpf_link_create_opts *opts);
260 
261 LIBBPF_API int bpf_link_detach(int link_fd);
262 
263 struct bpf_link_update_opts {
264 	size_t sz; /* size of this struct for forward/backward compatibility */
265 	__u32 flags;	   /* extra flags */
266 	__u32 old_prog_fd; /* expected old program FD */
267 };
268 #define bpf_link_update_opts__last_field old_prog_fd
269 
270 LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
271 			       const struct bpf_link_update_opts *opts);
272 
273 LIBBPF_API int bpf_iter_create(int link_fd);
274 
275 struct bpf_prog_test_run_attr {
276 	int prog_fd;
277 	int repeat;
278 	const void *data_in;
279 	__u32 data_size_in;
280 	void *data_out;      /* optional */
281 	__u32 data_size_out; /* in: max length of data_out
282 			      * out: length of data_out */
283 	__u32 retval;        /* out: return code of the BPF program */
284 	__u32 duration;      /* out: average per repetition in ns */
285 	const void *ctx_in; /* optional */
286 	__u32 ctx_size_in;
287 	void *ctx_out;      /* optional */
288 	__u32 ctx_size_out; /* in: max length of ctx_out
289 			     * out: length of cxt_out */
290 };
291 
292 LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);
293 
294 /*
295  * bpf_prog_test_run does not check that data_out is large enough. Consider
296  * using bpf_prog_test_run_xattr instead.
297  */
298 LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
299 				 __u32 size, void *data_out, __u32 *size_out,
300 				 __u32 *retval, __u32 *duration);
301 LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
302 LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
303 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
304 LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
305 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
306 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
307 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
308 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
309 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
310 LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
311 			      __u32 query_flags, __u32 *attach_flags,
312 			      __u32 *prog_ids, __u32 *prog_cnt);
313 LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
314 LIBBPF_API int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf,
315 			    __u32 log_buf_size, bool do_log);
316 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
317 				 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
318 				 __u64 *probe_offset, __u64 *probe_addr);
319 
320 enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
321 LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
322 
323 struct bpf_prog_bind_opts {
324 	size_t sz; /* size of this struct for forward/backward compatibility */
325 	__u32 flags;
326 };
327 #define bpf_prog_bind_opts__last_field flags
328 
329 LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd,
330 				 const struct bpf_prog_bind_opts *opts);
331 
332 struct bpf_test_run_opts {
333 	size_t sz; /* size of this struct for forward/backward compatibility */
334 	const void *data_in; /* optional */
335 	void *data_out;      /* optional */
336 	__u32 data_size_in;
337 	__u32 data_size_out; /* in: max length of data_out
338 			      * out: length of data_out
339 			      */
340 	const void *ctx_in; /* optional */
341 	void *ctx_out;      /* optional */
342 	__u32 ctx_size_in;
343 	__u32 ctx_size_out; /* in: max length of ctx_out
344 			     * out: length of cxt_out
345 			     */
346 	__u32 retval;        /* out: return code of the BPF program */
347 	int repeat;
348 	__u32 duration;      /* out: average per repetition in ns */
349 	__u32 flags;
350 	__u32 cpu;
351 };
352 #define bpf_test_run_opts__last_field cpu
353 
354 LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
355 				      struct bpf_test_run_opts *opts);
356 
357 #ifdef __cplusplus
358 } /* extern "C" */
359 #endif
360 
361 #endif /* __LIBBPF_BPF_H */
362