xref: /linux/tools/lib/bpf/bpf.h (revision 4e15507fea70c0c312d79610efa46b6853ccf8e0)
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;
49590a0088SMartin KaFai Lau 	union {
5091134d84SMartin KaFai Lau 		__u32 inner_map_fd;
51590a0088SMartin KaFai Lau 		__u32 btf_vmlinux_value_type_id;
52590a0088SMartin 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);
1302ab3d86eSYonghong Song 
1312ab3d86eSYonghong Song struct bpf_map_batch_opts {
1322ab3d86eSYonghong Song 	size_t sz; /* size of this struct for forward/backward compatibility */
1332ab3d86eSYonghong Song 	__u64 elem_flags;
1342ab3d86eSYonghong Song 	__u64 flags;
1352ab3d86eSYonghong Song };
1362ab3d86eSYonghong Song #define bpf_map_batch_opts__last_field flags
1372ab3d86eSYonghong Song 
1382ab3d86eSYonghong Song LIBBPF_API int bpf_map_delete_batch(int fd, void *keys,
1392ab3d86eSYonghong Song 				    __u32 *count,
1402ab3d86eSYonghong Song 				    const struct bpf_map_batch_opts *opts);
1412ab3d86eSYonghong Song LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
1422ab3d86eSYonghong Song 				    void *keys, void *values, __u32 *count,
1432ab3d86eSYonghong Song 				    const struct bpf_map_batch_opts *opts);
1442ab3d86eSYonghong Song LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
1452ab3d86eSYonghong Song 					void *out_batch, void *keys,
1462ab3d86eSYonghong Song 					void *values, __u32 *count,
1472ab3d86eSYonghong Song 					const struct bpf_map_batch_opts *opts);
1482ab3d86eSYonghong Song LIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values,
1492ab3d86eSYonghong Song 				    __u32 *count,
1502ab3d86eSYonghong Song 				    const struct bpf_map_batch_opts *opts);
1512ab3d86eSYonghong Song 
152ab9e0848SAndrey Ignatov LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
153ab9e0848SAndrey Ignatov LIBBPF_API int bpf_obj_get(const char *pathname);
154cdbee383SAndrey Ignatov 
155cdbee383SAndrey Ignatov struct bpf_prog_attach_opts {
156cdbee383SAndrey Ignatov 	size_t sz; /* size of this struct for forward/backward compatibility */
157cdbee383SAndrey Ignatov 	unsigned int flags;
158cdbee383SAndrey Ignatov 	int replace_prog_fd;
159cdbee383SAndrey Ignatov };
160cdbee383SAndrey Ignatov #define bpf_prog_attach_opts__last_field replace_prog_fd
161cdbee383SAndrey Ignatov 
162ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
163ab9e0848SAndrey Ignatov 			       enum bpf_attach_type type, unsigned int flags);
164cdbee383SAndrey Ignatov LIBBPF_API int bpf_prog_attach_xattr(int prog_fd, int attachable_fd,
165cdbee383SAndrey Ignatov 				     enum bpf_attach_type type,
166cdbee383SAndrey Ignatov 				     const struct bpf_prog_attach_opts *opts);
167ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
168ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
169ab9e0848SAndrey Ignatov 				enum bpf_attach_type type);
17064a97591SLorenz Bauer 
171cc4f864bSAndrii Nakryiko struct bpf_link_create_opts {
172cc4f864bSAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
173cc4f864bSAndrii Nakryiko };
174cc4f864bSAndrii Nakryiko #define bpf_link_create_opts__last_field sz
175cc4f864bSAndrii Nakryiko 
176cc4f864bSAndrii Nakryiko LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
177cc4f864bSAndrii Nakryiko 			       enum bpf_attach_type attach_type,
178cc4f864bSAndrii Nakryiko 			       const struct bpf_link_create_opts *opts);
179cc4f864bSAndrii Nakryiko 
180cc4f864bSAndrii Nakryiko struct bpf_link_update_opts {
181cc4f864bSAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
182cc4f864bSAndrii Nakryiko 	__u32 flags;	   /* extra flags */
183cc4f864bSAndrii Nakryiko 	__u32 old_prog_fd; /* expected old program FD */
184cc4f864bSAndrii Nakryiko };
185cc4f864bSAndrii Nakryiko #define bpf_link_update_opts__last_field old_prog_fd
186cc4f864bSAndrii Nakryiko 
187cc4f864bSAndrii Nakryiko LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
188cc4f864bSAndrii Nakryiko 			       const struct bpf_link_update_opts *opts);
189cc4f864bSAndrii Nakryiko 
190c09add2fSYonghong Song LIBBPF_API int bpf_iter_create(int link_fd);
191c09add2fSYonghong Song 
19264a97591SLorenz Bauer struct bpf_prog_test_run_attr {
19364a97591SLorenz Bauer 	int prog_fd;
19464a97591SLorenz Bauer 	int repeat;
19564a97591SLorenz Bauer 	const void *data_in;
19664a97591SLorenz Bauer 	__u32 data_size_in;
19764a97591SLorenz Bauer 	void *data_out;      /* optional */
19864a97591SLorenz Bauer 	__u32 data_size_out; /* in: max length of data_out
19964a97591SLorenz Bauer 			      * out: length of data_out */
20064a97591SLorenz Bauer 	__u32 retval;        /* out: return code of the BPF program */
20164a97591SLorenz Bauer 	__u32 duration;      /* out: average per repetition in ns */
2025e903c65SStanislav Fomichev 	const void *ctx_in; /* optional */
2035e903c65SStanislav Fomichev 	__u32 ctx_size_in;
2045e903c65SStanislav Fomichev 	void *ctx_out;      /* optional */
2055e903c65SStanislav Fomichev 	__u32 ctx_size_out; /* in: max length of ctx_out
2065e903c65SStanislav Fomichev 			     * out: length of cxt_out */
20764a97591SLorenz Bauer };
20864a97591SLorenz Bauer 
20964a97591SLorenz Bauer LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);
21064a97591SLorenz Bauer 
21164a97591SLorenz Bauer /*
21264a97591SLorenz Bauer  * bpf_prog_test_run does not check that data_out is large enough. Consider
21364a97591SLorenz Bauer  * using bpf_prog_test_run_xattr instead.
21464a97591SLorenz Bauer  */
215ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
216ab9e0848SAndrey Ignatov 				 __u32 size, void *data_out, __u32 *size_out,
217ab9e0848SAndrey Ignatov 				 __u32 *retval, __u32 *duration);
218ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
219ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
22009d7c2e3SQuentin Monnet LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
2210dbc8668SAndrii Nakryiko LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
222ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
223ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
224ab9e0848SAndrey Ignatov LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
2250dbc8668SAndrii Nakryiko LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
2260dbc8668SAndrii Nakryiko LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
227ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
228ab9e0848SAndrey Ignatov 			      __u32 query_flags, __u32 *attach_flags,
229ab9e0848SAndrey Ignatov 			      __u32 *prog_ids, __u32 *prog_cnt);
230ab9e0848SAndrey Ignatov LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
231ab9e0848SAndrey Ignatov LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf,
232ab9e0848SAndrey Ignatov 			    __u32 log_buf_size, bool do_log);
233ab9e0848SAndrey Ignatov LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
234ab9e0848SAndrey Ignatov 				 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
235ab9e0848SAndrey Ignatov 				 __u64 *probe_offset, __u64 *probe_addr);
236*4e15507fSAndrii Nakryiko 
237*4e15507fSAndrii Nakryiko enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
2380bee1067SSong Liu LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
2398c4905b9SStanislav Fomichev 
2408c4905b9SStanislav Fomichev #ifdef __cplusplus
2418c4905b9SStanislav Fomichev } /* extern "C" */
2428c4905b9SStanislav Fomichev #endif
2438c4905b9SStanislav Fomichev 
244eff81908SAndrey Ignatov #endif /* __LIBBPF_BPF_H */
245