xref: /linux/tools/lib/bpf/bpf.h (revision 590a0088825016ca7ec53f1aef7e84e1211778d8)
11bc38b8fSAlexei Starovoitov /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
26061a3d6SEric Leblond 
3e3ed2fefSWang Nan /*
4e3ed2fefSWang Nan  * common eBPF ELF operations.
5e3ed2fefSWang Nan  *
6e3ed2fefSWang Nan  * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7e3ed2fefSWang Nan  * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8e3ed2fefSWang Nan  * Copyright (C) 2015 Huawei Inc.
9203d1cacSWang Nan  *
10203d1cacSWang Nan  * This program is free software; you can redistribute it and/or
11203d1cacSWang Nan  * modify it under the terms of the GNU Lesser General Public
12203d1cacSWang Nan  * License as published by the Free Software Foundation;
13203d1cacSWang Nan  * version 2.1 of the License (not later!)
14203d1cacSWang Nan  *
15203d1cacSWang Nan  * This program is distributed in the hope that it will be useful,
16203d1cacSWang Nan  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17203d1cacSWang Nan  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18203d1cacSWang Nan  * GNU Lesser General Public License for more details.
19203d1cacSWang Nan  *
20203d1cacSWang Nan  * You should have received a copy of the GNU Lesser General Public
21203d1cacSWang Nan  * License along with this program; if not,  see <http://www.gnu.org/licenses>
22e3ed2fefSWang Nan  */
23eff81908SAndrey Ignatov #ifndef __LIBBPF_BPF_H
24eff81908SAndrey Ignatov #define __LIBBPF_BPF_H
25e3ed2fefSWang Nan 
26e3ed2fefSWang Nan #include <linux/bpf.h>
27878a4d32SBjörn Töpel #include <stdbool.h>
287a5980f9SMickaël Salaün #include <stddef.h>
2932e621e5SDaniel T. Lee #include <stdint.h>
30e3ed2fefSWang Nan 
31544402d4SAndrii Nakryiko #include "libbpf_common.h"
32544402d4SAndrii Nakryiko 
338c4905b9SStanislav Fomichev #ifdef __cplusplus
348c4905b9SStanislav Fomichev extern "C" {
358c4905b9SStanislav Fomichev #endif
368c4905b9SStanislav Fomichev 
378a138aedSMartin KaFai Lau struct bpf_create_map_attr {
388a138aedSMartin KaFai Lau 	const char *name;
398a138aedSMartin KaFai Lau 	enum bpf_map_type map_type;
408a138aedSMartin KaFai Lau 	__u32 map_flags;
418a138aedSMartin KaFai Lau 	__u32 key_size;
428a138aedSMartin KaFai Lau 	__u32 value_size;
438a138aedSMartin KaFai Lau 	__u32 max_entries;
448a138aedSMartin KaFai Lau 	__u32 numa_node;
458a138aedSMartin KaFai Lau 	__u32 btf_fd;
4661746dbeSMartin KaFai Lau 	__u32 btf_key_type_id;
4761746dbeSMartin KaFai Lau 	__u32 btf_value_type_id;
48f0307a7eSDavid Beckett 	__u32 map_ifindex;
49*590a0088SMartin KaFai Lau 	union {
5091134d84SMartin KaFai Lau 		__u32 inner_map_fd;
51*590a0088SMartin KaFai Lau 		__u32 btf_vmlinux_value_type_id;
52*590a0088SMartin KaFai Lau 	};
538a138aedSMartin KaFai Lau };
548a138aedSMartin KaFai Lau 
55ab9e0848SAndrey Ignatov LIBBPF_API int
56ab9e0848SAndrey Ignatov bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
57ab9e0848SAndrey Ignatov LIBBPF_API int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
58ab9e0848SAndrey Ignatov 				   int key_size, int value_size,
59ab9e0848SAndrey Ignatov 				   int max_entries, __u32 map_flags, int node);
60ab9e0848SAndrey Ignatov LIBBPF_API int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
61ab9e0848SAndrey Ignatov 				   int key_size, int value_size,
62a5580c7fSJoe Stringer 				   int max_entries, __u32 map_flags);
63ab9e0848SAndrey Ignatov LIBBPF_API int bpf_create_map(enum bpf_map_type map_type, int key_size,
64ab9e0848SAndrey Ignatov 			      int value_size, int max_entries, __u32 map_flags);
65ab9e0848SAndrey Ignatov LIBBPF_API int bpf_create_map_in_map_node(enum bpf_map_type map_type,
66ab9e0848SAndrey Ignatov 					  const char *name, int key_size,
67ab9e0848SAndrey Ignatov 					  int inner_map_fd, int max_entries,
68ad17d0e6SMartin KaFai Lau 					  __u32 map_flags, int node);
69ab9e0848SAndrey Ignatov LIBBPF_API int bpf_create_map_in_map(enum bpf_map_type map_type,
70ab9e0848SAndrey Ignatov 				     const char *name, int key_size,
71ab9e0848SAndrey Ignatov 				     int inner_map_fd, int max_entries,
7288cda1c9SMartin KaFai Lau 				     __u32 map_flags);
73e3ed2fefSWang Nan 
74d7be143bSAndrey Ignatov struct bpf_load_program_attr {
75d7be143bSAndrey Ignatov 	enum bpf_prog_type prog_type;
76d7be143bSAndrey Ignatov 	enum bpf_attach_type expected_attach_type;
77d7be143bSAndrey Ignatov 	const char *name;
78d7be143bSAndrey Ignatov 	const struct bpf_insn *insns;
79d7be143bSAndrey Ignatov 	size_t insns_cnt;
80d7be143bSAndrey Ignatov 	const char *license;
81e7bf94dbSAlexei Starovoitov 	union {
82d7be143bSAndrey Ignatov 		__u32 kern_version;
83e7bf94dbSAlexei Starovoitov 		__u32 attach_prog_fd;
84e7bf94dbSAlexei Starovoitov 	};
8512a8654bSAlexei Starovoitov 	union {
86f0307a7eSDavid Beckett 		__u32 prog_ifindex;
8712a8654bSAlexei Starovoitov 		__u32 attach_btf_id;
8812a8654bSAlexei Starovoitov 	};
897e0d0fb5SYonghong Song 	__u32 prog_btf_fd;
907e0d0fb5SYonghong Song 	__u32 func_info_rec_size;
917e0d0fb5SYonghong Song 	const void *func_info;
927e0d0fb5SYonghong Song 	__u32 func_info_cnt;
933d650141SMartin KaFai Lau 	__u32 line_info_rec_size;
943d650141SMartin KaFai Lau 	const void *line_info;
953d650141SMartin KaFai Lau 	__u32 line_info_cnt;
96a4021a35SYonghong Song 	__u32 log_level;
9704656198SJiong Wang 	__u32 prog_flags;
98d7be143bSAndrey Ignatov };
99d7be143bSAndrey Ignatov 
100c034a177SJohn Fastabend /* Flags to direct loading requirements */
101c034a177SJohn Fastabend #define MAPS_RELAX_COMPAT	0x01
102c034a177SJohn Fastabend 
1037bf98369SWang Nan /* Recommend log buffer size */
1044519efa6SMcCabe, Robert J #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
105ab9e0848SAndrey Ignatov LIBBPF_API int
106ab9e0848SAndrey Ignatov bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
107d7be143bSAndrey Ignatov 		       char *log_buf, size_t log_buf_sz);
108ab9e0848SAndrey Ignatov LIBBPF_API int bpf_load_program(enum bpf_prog_type type,
109ab9e0848SAndrey Ignatov 				const struct bpf_insn *insns, size_t insns_cnt,
110ab9e0848SAndrey Ignatov 				const char *license, __u32 kern_version,
111ab9e0848SAndrey Ignatov 				char *log_buf, size_t log_buf_sz);
112ab9e0848SAndrey Ignatov LIBBPF_API int bpf_verify_program(enum bpf_prog_type type,
113ab9e0848SAndrey Ignatov 				  const struct bpf_insn *insns,
114e9ee9efcSDavid Miller 				  size_t insns_cnt, __u32 prog_flags,
11591045f5eSDavid S. Miller 				  const char *license, __u32 kern_version,
116ab9e0848SAndrey Ignatov 				  char *log_buf, size_t log_buf_sz,
117ab9e0848SAndrey Ignatov 				  int log_level);
1187bf98369SWang Nan 
119ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
12083d994d0SJoe Stringer 				   __u64 flags);
1219742da01SWang Nan 
122ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
123df5d22faSAlexei Starovoitov LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value,
124df5d22faSAlexei Starovoitov 					 __u64 flags);
12543b987d2SMauricio Vasquez B LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
12643b987d2SMauricio Vasquez B 					      void *value);
127ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
128ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
129d859900cSDaniel Borkmann LIBBPF_API int bpf_map_freeze(int fd);
130ab9e0848SAndrey Ignatov LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
131ab9e0848SAndrey Ignatov LIBBPF_API int bpf_obj_get(const char *pathname);
132cdbee383SAndrey Ignatov 
133cdbee383SAndrey Ignatov struct bpf_prog_attach_opts {
134cdbee383SAndrey Ignatov 	size_t sz; /* size of this struct for forward/backward compatibility */
135cdbee383SAndrey Ignatov 	unsigned int flags;
136cdbee383SAndrey Ignatov 	int replace_prog_fd;
137cdbee383SAndrey Ignatov };
138cdbee383SAndrey Ignatov #define bpf_prog_attach_opts__last_field replace_prog_fd
139cdbee383SAndrey Ignatov 
140ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
141ab9e0848SAndrey Ignatov 			       enum bpf_attach_type type, unsigned int flags);
142cdbee383SAndrey Ignatov LIBBPF_API int bpf_prog_attach_xattr(int prog_fd, int attachable_fd,
143cdbee383SAndrey Ignatov 				     enum bpf_attach_type type,
144cdbee383SAndrey Ignatov 				     const struct bpf_prog_attach_opts *opts);
145ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
146ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
147ab9e0848SAndrey Ignatov 				enum bpf_attach_type type);
14864a97591SLorenz Bauer 
14964a97591SLorenz Bauer struct bpf_prog_test_run_attr {
15064a97591SLorenz Bauer 	int prog_fd;
15164a97591SLorenz Bauer 	int repeat;
15264a97591SLorenz Bauer 	const void *data_in;
15364a97591SLorenz Bauer 	__u32 data_size_in;
15464a97591SLorenz Bauer 	void *data_out;      /* optional */
15564a97591SLorenz Bauer 	__u32 data_size_out; /* in: max length of data_out
15664a97591SLorenz Bauer 			      * out: length of data_out */
15764a97591SLorenz Bauer 	__u32 retval;        /* out: return code of the BPF program */
15864a97591SLorenz Bauer 	__u32 duration;      /* out: average per repetition in ns */
1595e903c65SStanislav Fomichev 	const void *ctx_in; /* optional */
1605e903c65SStanislav Fomichev 	__u32 ctx_size_in;
1615e903c65SStanislav Fomichev 	void *ctx_out;      /* optional */
1625e903c65SStanislav Fomichev 	__u32 ctx_size_out; /* in: max length of ctx_out
1635e903c65SStanislav Fomichev 			     * out: length of cxt_out */
16464a97591SLorenz Bauer };
16564a97591SLorenz Bauer 
16664a97591SLorenz Bauer LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);
16764a97591SLorenz Bauer 
16864a97591SLorenz Bauer /*
16964a97591SLorenz Bauer  * bpf_prog_test_run does not check that data_out is large enough. Consider
17064a97591SLorenz Bauer  * using bpf_prog_test_run_xattr instead.
17164a97591SLorenz Bauer  */
172ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
173ab9e0848SAndrey Ignatov 				 __u32 size, void *data_out, __u32 *size_out,
174ab9e0848SAndrey Ignatov 				 __u32 *retval, __u32 *duration);
175ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
176ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
17709d7c2e3SQuentin Monnet LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
178ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
179ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
180ab9e0848SAndrey Ignatov LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
181ab9e0848SAndrey Ignatov LIBBPF_API int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len);
182ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
183ab9e0848SAndrey Ignatov 			      __u32 query_flags, __u32 *attach_flags,
184ab9e0848SAndrey Ignatov 			      __u32 *prog_ids, __u32 *prog_cnt);
185ab9e0848SAndrey Ignatov LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
186ab9e0848SAndrey Ignatov LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf,
187ab9e0848SAndrey Ignatov 			    __u32 log_buf_size, bool do_log);
188ab9e0848SAndrey Ignatov LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
189ab9e0848SAndrey Ignatov 				 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
190ab9e0848SAndrey Ignatov 				 __u64 *probe_offset, __u64 *probe_addr);
1918c4905b9SStanislav Fomichev 
1928c4905b9SStanislav Fomichev #ifdef __cplusplus
1938c4905b9SStanislav Fomichev } /* extern "C" */
1948c4905b9SStanislav Fomichev #endif
1958c4905b9SStanislav Fomichev 
196eff81908SAndrey Ignatov #endif /* __LIBBPF_BPF_H */
197