xref: /linux/tools/lib/bpf/bpf.h (revision 737d0646a83cdc65c070a9de61a1ef106cca5ff1)
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"
32be80e9cdSAndrii Nakryiko #include "libbpf_legacy.h"
33544402d4SAndrii Nakryiko 
348c4905b9SStanislav Fomichev #ifdef __cplusplus
358c4905b9SStanislav Fomichev extern "C" {
368c4905b9SStanislav Fomichev #endif
378c4905b9SStanislav Fomichev 
38e542f2c4SAndrii Nakryiko int libbpf_set_memlock_rlim(size_t memlock_bytes);
39e542f2c4SAndrii Nakryiko 
40992c4225SAndrii Nakryiko struct bpf_map_create_opts {
41992c4225SAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
42992c4225SAndrii Nakryiko 
43992c4225SAndrii Nakryiko 	__u32 btf_fd;
44992c4225SAndrii Nakryiko 	__u32 btf_key_type_id;
45992c4225SAndrii Nakryiko 	__u32 btf_value_type_id;
46992c4225SAndrii Nakryiko 	__u32 btf_vmlinux_value_type_id;
47992c4225SAndrii Nakryiko 
4874d98070SAndrii Nakryiko 	__u32 inner_map_fd;
4974d98070SAndrii Nakryiko 	__u32 map_flags;
50992c4225SAndrii Nakryiko 	__u64 map_extra;
51992c4225SAndrii Nakryiko 
5274d98070SAndrii Nakryiko 	__u32 numa_node;
5374d98070SAndrii Nakryiko 	__u32 map_ifindex;
54992c4225SAndrii Nakryiko };
55992c4225SAndrii Nakryiko #define bpf_map_create_opts__last_field map_ifindex
56992c4225SAndrii Nakryiko 
57992c4225SAndrii Nakryiko LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
58992c4225SAndrii Nakryiko 			      const char *map_name,
59992c4225SAndrii Nakryiko 			      __u32 key_size,
60992c4225SAndrii Nakryiko 			      __u32 value_size,
61992c4225SAndrii Nakryiko 			      __u32 max_entries,
62992c4225SAndrii Nakryiko 			      const struct bpf_map_create_opts *opts);
63992c4225SAndrii Nakryiko 
648a138aedSMartin KaFai Lau struct bpf_create_map_attr {
658a138aedSMartin KaFai Lau 	const char *name;
668a138aedSMartin KaFai Lau 	enum bpf_map_type map_type;
678a138aedSMartin KaFai Lau 	__u32 map_flags;
688a138aedSMartin KaFai Lau 	__u32 key_size;
698a138aedSMartin KaFai Lau 	__u32 value_size;
708a138aedSMartin KaFai Lau 	__u32 max_entries;
718a138aedSMartin KaFai Lau 	__u32 numa_node;
728a138aedSMartin KaFai Lau 	__u32 btf_fd;
7361746dbeSMartin KaFai Lau 	__u32 btf_key_type_id;
7461746dbeSMartin KaFai Lau 	__u32 btf_value_type_id;
75f0307a7eSDavid Beckett 	__u32 map_ifindex;
76590a0088SMartin KaFai Lau 	union {
7791134d84SMartin KaFai Lau 		__u32 inner_map_fd;
78590a0088SMartin KaFai Lau 		__u32 btf_vmlinux_value_type_id;
79590a0088SMartin KaFai Lau 	};
808a138aedSMartin KaFai Lau };
818a138aedSMartin KaFai Lau 
82992c4225SAndrii Nakryiko LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead")
83992c4225SAndrii Nakryiko LIBBPF_API int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
84992c4225SAndrii Nakryiko LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead")
85ab9e0848SAndrey Ignatov LIBBPF_API int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
86ab9e0848SAndrey Ignatov 				   int key_size, int value_size,
87ab9e0848SAndrey Ignatov 				   int max_entries, __u32 map_flags, int node);
88992c4225SAndrii Nakryiko LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead")
89ab9e0848SAndrey Ignatov LIBBPF_API int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
90ab9e0848SAndrey Ignatov 				   int key_size, int value_size,
91a5580c7fSJoe Stringer 				   int max_entries, __u32 map_flags);
92992c4225SAndrii Nakryiko LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead")
93ab9e0848SAndrey Ignatov LIBBPF_API int bpf_create_map(enum bpf_map_type map_type, int key_size,
94ab9e0848SAndrey Ignatov 			      int value_size, int max_entries, __u32 map_flags);
95992c4225SAndrii Nakryiko LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead")
96ab9e0848SAndrey Ignatov LIBBPF_API int bpf_create_map_in_map_node(enum bpf_map_type map_type,
97ab9e0848SAndrey Ignatov 					  const char *name, int key_size,
98ab9e0848SAndrey Ignatov 					  int inner_map_fd, int max_entries,
99ad17d0e6SMartin KaFai Lau 					  __u32 map_flags, int node);
100992c4225SAndrii Nakryiko LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead")
101ab9e0848SAndrey Ignatov LIBBPF_API int bpf_create_map_in_map(enum bpf_map_type map_type,
102ab9e0848SAndrey Ignatov 				     const char *name, int key_size,
103ab9e0848SAndrey Ignatov 				     int inner_map_fd, int max_entries,
10488cda1c9SMartin KaFai Lau 				     __u32 map_flags);
105e3ed2fefSWang Nan 
106d10ef2b8SAndrii Nakryiko struct bpf_prog_load_opts {
107d10ef2b8SAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
108d10ef2b8SAndrii Nakryiko 
109d10ef2b8SAndrii Nakryiko 	/* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns
110d10ef2b8SAndrii Nakryiko 	 * -EAGAIN. This field determines how many attempts libbpf has to
111d10ef2b8SAndrii Nakryiko 	 *  make. If not specified, libbpf will use default value of 5.
112d10ef2b8SAndrii Nakryiko 	 */
113d10ef2b8SAndrii Nakryiko 	int attempts;
114d10ef2b8SAndrii Nakryiko 
115d10ef2b8SAndrii Nakryiko 	enum bpf_attach_type expected_attach_type;
116d10ef2b8SAndrii Nakryiko 	__u32 prog_btf_fd;
117d10ef2b8SAndrii Nakryiko 	__u32 prog_flags;
118d10ef2b8SAndrii Nakryiko 	__u32 prog_ifindex;
119d10ef2b8SAndrii Nakryiko 	__u32 kern_version;
120d10ef2b8SAndrii Nakryiko 
121d10ef2b8SAndrii Nakryiko 	__u32 attach_btf_id;
122d10ef2b8SAndrii Nakryiko 	__u32 attach_prog_fd;
123d10ef2b8SAndrii Nakryiko 	__u32 attach_btf_obj_fd;
124d10ef2b8SAndrii Nakryiko 
125d10ef2b8SAndrii Nakryiko 	const int *fd_array;
126d10ef2b8SAndrii Nakryiko 
127d10ef2b8SAndrii Nakryiko 	/* .BTF.ext func info data */
128d10ef2b8SAndrii Nakryiko 	const void *func_info;
129d10ef2b8SAndrii Nakryiko 	__u32 func_info_cnt;
130d10ef2b8SAndrii Nakryiko 	__u32 func_info_rec_size;
131d10ef2b8SAndrii Nakryiko 
132d10ef2b8SAndrii Nakryiko 	/* .BTF.ext line info data */
133d10ef2b8SAndrii Nakryiko 	const void *line_info;
134d10ef2b8SAndrii Nakryiko 	__u32 line_info_cnt;
135d10ef2b8SAndrii Nakryiko 	__u32 line_info_rec_size;
136d10ef2b8SAndrii Nakryiko 
137d10ef2b8SAndrii Nakryiko 	/* verifier log options */
138d10ef2b8SAndrii Nakryiko 	__u32 log_level;
139d10ef2b8SAndrii Nakryiko 	__u32 log_size;
140d10ef2b8SAndrii Nakryiko 	char *log_buf;
141d10ef2b8SAndrii Nakryiko };
142d10ef2b8SAndrii Nakryiko #define bpf_prog_load_opts__last_field log_buf
143d10ef2b8SAndrii Nakryiko 
144d10ef2b8SAndrii Nakryiko LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
145d10ef2b8SAndrii Nakryiko 			     const char *prog_name, const char *license,
146d10ef2b8SAndrii Nakryiko 			     const struct bpf_insn *insns, size_t insn_cnt,
147d10ef2b8SAndrii Nakryiko 			     const struct bpf_prog_load_opts *opts);
148d10ef2b8SAndrii Nakryiko /* this "specialization" should go away in libbpf 1.0 */
149d10ef2b8SAndrii Nakryiko LIBBPF_API int bpf_prog_load_v0_6_0(enum bpf_prog_type prog_type,
150d10ef2b8SAndrii Nakryiko 				    const char *prog_name, const char *license,
151d10ef2b8SAndrii Nakryiko 				    const struct bpf_insn *insns, size_t insn_cnt,
152d10ef2b8SAndrii Nakryiko 				    const struct bpf_prog_load_opts *opts);
153d10ef2b8SAndrii Nakryiko 
154d10ef2b8SAndrii Nakryiko /* This is an elaborate way to not conflict with deprecated bpf_prog_load()
155d10ef2b8SAndrii Nakryiko  * API, defined in libbpf.h. Once we hit libbpf 1.0, all this will be gone.
156d10ef2b8SAndrii Nakryiko  * With this approach, if someone is calling bpf_prog_load() with
157d10ef2b8SAndrii Nakryiko  * 4 arguments, they will use the deprecated API, which keeps backwards
158d10ef2b8SAndrii Nakryiko  * compatibility (both source code and binary). If bpf_prog_load() is called
159d10ef2b8SAndrii Nakryiko  * with 6 arguments, though, it gets redirected to __bpf_prog_load.
160d10ef2b8SAndrii Nakryiko  * So looking forward to libbpf 1.0 when this hack will be gone and
161d10ef2b8SAndrii Nakryiko  * __bpf_prog_load() will be called just bpf_prog_load().
162d10ef2b8SAndrii Nakryiko  */
163d10ef2b8SAndrii Nakryiko #ifndef bpf_prog_load
164d10ef2b8SAndrii Nakryiko #define bpf_prog_load(...) ___libbpf_overload(___bpf_prog_load, __VA_ARGS__)
165d10ef2b8SAndrii Nakryiko #define ___bpf_prog_load4(file, type, pobj, prog_fd) \
166d10ef2b8SAndrii Nakryiko 	bpf_prog_load_deprecated(file, type, pobj, prog_fd)
167d10ef2b8SAndrii Nakryiko #define ___bpf_prog_load6(prog_type, prog_name, license, insns, insn_cnt, opts) \
168d10ef2b8SAndrii Nakryiko 	bpf_prog_load(prog_type, prog_name, license, insns, insn_cnt, opts)
169d10ef2b8SAndrii Nakryiko #endif /* bpf_prog_load */
170d10ef2b8SAndrii Nakryiko 
171d7be143bSAndrey Ignatov struct bpf_load_program_attr {
172d7be143bSAndrey Ignatov 	enum bpf_prog_type prog_type;
173d7be143bSAndrey Ignatov 	enum bpf_attach_type expected_attach_type;
174d7be143bSAndrey Ignatov 	const char *name;
175d7be143bSAndrey Ignatov 	const struct bpf_insn *insns;
176d7be143bSAndrey Ignatov 	size_t insns_cnt;
177d7be143bSAndrey Ignatov 	const char *license;
178e7bf94dbSAlexei Starovoitov 	union {
179d7be143bSAndrey Ignatov 		__u32 kern_version;
180e7bf94dbSAlexei Starovoitov 		__u32 attach_prog_fd;
181e7bf94dbSAlexei Starovoitov 	};
18212a8654bSAlexei Starovoitov 	union {
183f0307a7eSDavid Beckett 		__u32 prog_ifindex;
18412a8654bSAlexei Starovoitov 		__u32 attach_btf_id;
18512a8654bSAlexei Starovoitov 	};
1867e0d0fb5SYonghong Song 	__u32 prog_btf_fd;
1877e0d0fb5SYonghong Song 	__u32 func_info_rec_size;
1887e0d0fb5SYonghong Song 	const void *func_info;
1897e0d0fb5SYonghong Song 	__u32 func_info_cnt;
1903d650141SMartin KaFai Lau 	__u32 line_info_rec_size;
1913d650141SMartin KaFai Lau 	const void *line_info;
1923d650141SMartin KaFai Lau 	__u32 line_info_cnt;
193a4021a35SYonghong Song 	__u32 log_level;
19404656198SJiong Wang 	__u32 prog_flags;
195d7be143bSAndrey Ignatov };
196d7be143bSAndrey Ignatov 
197c034a177SJohn Fastabend /* Flags to direct loading requirements */
198c034a177SJohn Fastabend #define MAPS_RELAX_COMPAT	0x01
199c034a177SJohn Fastabend 
200e0e3ea88SAndrii Nakryiko /* Recommended log buffer size */
2014519efa6SMcCabe, Robert J #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
202e0e3ea88SAndrii Nakryiko 
203d10ef2b8SAndrii Nakryiko LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
204d10ef2b8SAndrii Nakryiko LIBBPF_API int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
205d7be143bSAndrey Ignatov 				      char *log_buf, size_t log_buf_sz);
206d10ef2b8SAndrii Nakryiko LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
207ab9e0848SAndrey Ignatov LIBBPF_API int bpf_load_program(enum bpf_prog_type type,
208ab9e0848SAndrey Ignatov 				const struct bpf_insn *insns, size_t insns_cnt,
209ab9e0848SAndrey Ignatov 				const char *license, __u32 kern_version,
210ab9e0848SAndrey Ignatov 				char *log_buf, size_t log_buf_sz);
211d10ef2b8SAndrii Nakryiko LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
212ab9e0848SAndrey Ignatov LIBBPF_API int bpf_verify_program(enum bpf_prog_type type,
213ab9e0848SAndrey Ignatov 				  const struct bpf_insn *insns,
214e9ee9efcSDavid Miller 				  size_t insns_cnt, __u32 prog_flags,
21591045f5eSDavid S. Miller 				  const char *license, __u32 kern_version,
216ab9e0848SAndrey Ignatov 				  char *log_buf, size_t log_buf_sz,
217ab9e0848SAndrey Ignatov 				  int log_level);
2187bf98369SWang Nan 
2190ed08d67SAndrii Nakryiko struct bpf_btf_load_opts {
2200ed08d67SAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
2210ed08d67SAndrii Nakryiko 
2220ed08d67SAndrii Nakryiko 	/* kernel log options */
2230ed08d67SAndrii Nakryiko 	char *log_buf;
2240ed08d67SAndrii Nakryiko 	__u32 log_level;
2250ed08d67SAndrii Nakryiko 	__u32 log_size;
2260ed08d67SAndrii Nakryiko };
2270ed08d67SAndrii Nakryiko #define bpf_btf_load_opts__last_field log_size
2280ed08d67SAndrii Nakryiko 
2290ed08d67SAndrii Nakryiko LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,
2300ed08d67SAndrii Nakryiko 			    const struct bpf_btf_load_opts *opts);
2310ed08d67SAndrii Nakryiko 
2320ed08d67SAndrii Nakryiko LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_btf_load() instead")
2330ed08d67SAndrii Nakryiko LIBBPF_API int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf,
2340ed08d67SAndrii Nakryiko 			    __u32 log_buf_size, bool do_log);
2350ed08d67SAndrii Nakryiko 
236ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
23783d994d0SJoe Stringer 				   __u64 flags);
2389742da01SWang Nan 
239ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
240df5d22faSAlexei Starovoitov LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value,
241df5d22faSAlexei Starovoitov 					 __u64 flags);
24243b987d2SMauricio Vasquez B LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
24343b987d2SMauricio Vasquez B 					      void *value);
244d59b9f2dSDenis Salopek LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key,
245d59b9f2dSDenis Salopek 						    void *value, __u64 flags);
246ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
247*737d0646SAndrii Nakryiko LIBBPF_API int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags);
248ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
249d859900cSDaniel Borkmann LIBBPF_API int bpf_map_freeze(int fd);
2502ab3d86eSYonghong Song 
2512ab3d86eSYonghong Song struct bpf_map_batch_opts {
2522ab3d86eSYonghong Song 	size_t sz; /* size of this struct for forward/backward compatibility */
2532ab3d86eSYonghong Song 	__u64 elem_flags;
2542ab3d86eSYonghong Song 	__u64 flags;
2552ab3d86eSYonghong Song };
2562ab3d86eSYonghong Song #define bpf_map_batch_opts__last_field flags
2572ab3d86eSYonghong Song 
258e59618f0SGrant Seltzer 
259e59618f0SGrant Seltzer /**
260e59618f0SGrant Seltzer  * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
261e59618f0SGrant Seltzer  * elements in a BPF map.
262e59618f0SGrant Seltzer  *
263e59618f0SGrant Seltzer  * @param fd BPF map file descriptor
264e59618f0SGrant Seltzer  * @param keys pointer to an array of *count* keys
265e59618f0SGrant Seltzer  * @param count input and output parameter; on input **count** represents the
266e59618f0SGrant Seltzer  * number of  elements in the map to delete in batch;
267e59618f0SGrant Seltzer  * on output if a non-EFAULT error is returned, **count** represents the number of deleted
268e59618f0SGrant Seltzer  * elements if the output **count** value is not equal to the input **count** value
269e59618f0SGrant Seltzer  * If EFAULT is returned, **count** should not be trusted to be correct.
270e59618f0SGrant Seltzer  * @param opts options for configuring the way the batch deletion works
271e59618f0SGrant Seltzer  * @return 0, on success; negative error code, otherwise (errno is also set to
272e59618f0SGrant Seltzer  * the error code)
273e59618f0SGrant Seltzer  */
274e59618f0SGrant Seltzer LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
2752ab3d86eSYonghong Song 				    __u32 *count,
2762ab3d86eSYonghong Song 				    const struct bpf_map_batch_opts *opts);
277e59618f0SGrant Seltzer 
278e59618f0SGrant Seltzer /**
279e59618f0SGrant Seltzer  * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
280e59618f0SGrant Seltzer  *
281e59618f0SGrant Seltzer  * The parameter *in_batch* is the address of the first element in the batch to read.
282e59618f0SGrant Seltzer  * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent
283e59618f0SGrant Seltzer  * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate
284e59618f0SGrant Seltzer  * that the batched lookup starts from the beginning of the map.
285e59618f0SGrant Seltzer  *
286e59618f0SGrant Seltzer  * The *keys* and *values* are output parameters which must point to memory large enough to
287e59618f0SGrant Seltzer  * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
288e59618f0SGrant Seltzer  * buffer must be of *key_size* * *count*. The *values* buffer must be of
289e59618f0SGrant Seltzer  * *value_size* * *count*.
290e59618f0SGrant Seltzer  *
291e59618f0SGrant Seltzer  * @param fd BPF map file descriptor
292e59618f0SGrant Seltzer  * @param in_batch address of the first element in batch to read, can pass NULL to
293e59618f0SGrant Seltzer  * indicate that the batched lookup starts from the beginning of the map.
294e59618f0SGrant Seltzer  * @param out_batch output parameter that should be passed to next call as *in_batch*
295e59618f0SGrant Seltzer  * @param keys pointer to an array large enough for *count* keys
296e59618f0SGrant Seltzer  * @param values pointer to an array large enough for *count* values
297e59618f0SGrant Seltzer  * @param count input and output parameter; on input it's the number of elements
298e59618f0SGrant Seltzer  * in the map to read in batch; on output it's the number of elements that were
299e59618f0SGrant Seltzer  * successfully read.
300e59618f0SGrant Seltzer  * If a non-EFAULT error is returned, count will be set as the number of elements
301e59618f0SGrant Seltzer  * that were read before the error occurred.
302e59618f0SGrant Seltzer  * If EFAULT is returned, **count** should not be trusted to be correct.
303e59618f0SGrant Seltzer  * @param opts options for configuring the way the batch lookup works
304e59618f0SGrant Seltzer  * @return 0, on success; negative error code, otherwise (errno is also set to
305e59618f0SGrant Seltzer  * the error code)
306e59618f0SGrant Seltzer  */
3072ab3d86eSYonghong Song LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
3082ab3d86eSYonghong Song 				    void *keys, void *values, __u32 *count,
3092ab3d86eSYonghong Song 				    const struct bpf_map_batch_opts *opts);
310e59618f0SGrant Seltzer 
311e59618f0SGrant Seltzer /**
312e59618f0SGrant Seltzer  * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
313e59618f0SGrant Seltzer  * of BPF map elements where each element is deleted after being retrieved.
314e59618f0SGrant Seltzer  *
315e59618f0SGrant Seltzer  * @param fd BPF map file descriptor
316e59618f0SGrant Seltzer  * @param in_batch address of the first element in batch to read, can pass NULL to
317e59618f0SGrant Seltzer  * get address of the first element in *out_batch*
318e59618f0SGrant Seltzer  * @param out_batch output parameter that should be passed to next call as *in_batch*
319e59618f0SGrant Seltzer  * @param keys pointer to an array of *count* keys
320e59618f0SGrant Seltzer  * @param values pointer to an array large enough for *count* values
321e59618f0SGrant Seltzer  * @param count input and output parameter; on input it's the number of elements
322e59618f0SGrant Seltzer  * in the map to read and delete in batch; on output it represents the number of
323e59618f0SGrant Seltzer  * elements that were successfully read and deleted
324e59618f0SGrant Seltzer  * If a non-**EFAULT** error code is returned and if the output **count** value
325e59618f0SGrant Seltzer  * is not equal to the input **count** value, up to **count** elements may
326e59618f0SGrant Seltzer  * have been deleted.
327e59618f0SGrant Seltzer  * if **EFAULT** is returned up to *count* elements may have been deleted without
328e59618f0SGrant Seltzer  * being returned via the *keys* and *values* output parameters.
329e59618f0SGrant Seltzer  * @param opts options for configuring the way the batch lookup and delete works
330e59618f0SGrant Seltzer  * @return 0, on success; negative error code, otherwise (errno is also set to
331e59618f0SGrant Seltzer  * the error code)
332e59618f0SGrant Seltzer  */
3332ab3d86eSYonghong Song LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
3342ab3d86eSYonghong Song 					void *out_batch, void *keys,
3352ab3d86eSYonghong Song 					void *values, __u32 *count,
3362ab3d86eSYonghong Song 					const struct bpf_map_batch_opts *opts);
337e59618f0SGrant Seltzer 
338e59618f0SGrant Seltzer /**
339e59618f0SGrant Seltzer  * @brief **bpf_map_update_batch()** updates multiple elements in a map
340e59618f0SGrant Seltzer  * by specifying keys and their corresponding values.
341e59618f0SGrant Seltzer  *
342e59618f0SGrant Seltzer  * The *keys* and *values* parameters must point to memory large enough
343e59618f0SGrant Seltzer  * to hold *count* items based on the key and value size of the map.
344e59618f0SGrant Seltzer  *
345e59618f0SGrant Seltzer  * The *opts* parameter can be used to control how *bpf_map_update_batch()*
346e59618f0SGrant Seltzer  * should handle keys that either do or do not already exist in the map.
347e59618f0SGrant Seltzer  * In particular the *flags* parameter of *bpf_map_batch_opts* can be
348e59618f0SGrant Seltzer  * one of the following:
349e59618f0SGrant Seltzer  *
350e59618f0SGrant Seltzer  * Note that *count* is an input and output parameter, where on output it
351e59618f0SGrant Seltzer  * represents how many elements were successfully updated. Also note that if
352e59618f0SGrant Seltzer  * **EFAULT** then *count* should not be trusted to be correct.
353e59618f0SGrant Seltzer  *
354e59618f0SGrant Seltzer  * **BPF_ANY**
355e59618f0SGrant Seltzer  *    Create new elements or update existing.
356e59618f0SGrant Seltzer  *
357e59618f0SGrant Seltzer  * **BPF_NOEXIST**
358e59618f0SGrant Seltzer  *    Create new elements only if they do not exist.
359e59618f0SGrant Seltzer  *
360e59618f0SGrant Seltzer  * **BPF_EXIST**
361e59618f0SGrant Seltzer  *    Update existing elements.
362e59618f0SGrant Seltzer  *
363e59618f0SGrant Seltzer  * **BPF_F_LOCK**
364e59618f0SGrant Seltzer  *    Update spin_lock-ed map elements. This must be
365e59618f0SGrant Seltzer  *    specified if the map value contains a spinlock.
366e59618f0SGrant Seltzer  *
367e59618f0SGrant Seltzer  * @param fd BPF map file descriptor
368e59618f0SGrant Seltzer  * @param keys pointer to an array of *count* keys
369e59618f0SGrant Seltzer  * @param values pointer to an array of *count* values
370e59618f0SGrant Seltzer  * @param count input and output parameter; on input it's the number of elements
371e59618f0SGrant Seltzer  * in the map to update in batch; on output if a non-EFAULT error is returned,
372e59618f0SGrant Seltzer  * **count** represents the number of updated elements if the output **count**
373e59618f0SGrant Seltzer  * value is not equal to the input **count** value.
374e59618f0SGrant Seltzer  * If EFAULT is returned, **count** should not be trusted to be correct.
375e59618f0SGrant Seltzer  * @param opts options for configuring the way the batch update works
376e59618f0SGrant Seltzer  * @return 0, on success; negative error code, otherwise (errno is also set to
377e59618f0SGrant Seltzer  * the error code)
378e59618f0SGrant Seltzer  */
379e59618f0SGrant Seltzer LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
3802ab3d86eSYonghong Song 				    __u32 *count,
3812ab3d86eSYonghong Song 				    const struct bpf_map_batch_opts *opts);
3822ab3d86eSYonghong Song 
383ab9e0848SAndrey Ignatov LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
384ab9e0848SAndrey Ignatov LIBBPF_API int bpf_obj_get(const char *pathname);
385cdbee383SAndrey Ignatov 
386cdbee383SAndrey Ignatov struct bpf_prog_attach_opts {
387cdbee383SAndrey Ignatov 	size_t sz; /* size of this struct for forward/backward compatibility */
388cdbee383SAndrey Ignatov 	unsigned int flags;
389cdbee383SAndrey Ignatov 	int replace_prog_fd;
390cdbee383SAndrey Ignatov };
391cdbee383SAndrey Ignatov #define bpf_prog_attach_opts__last_field replace_prog_fd
392cdbee383SAndrey Ignatov 
393ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
394ab9e0848SAndrey Ignatov 			       enum bpf_attach_type type, unsigned int flags);
395d6c9c24eSChristy Lee LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int attachable_fd,
396d6c9c24eSChristy Lee 				     enum bpf_attach_type type,
397d6c9c24eSChristy Lee 				     const struct bpf_prog_attach_opts *opts);
398d6c9c24eSChristy Lee LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_prog_attach_opts() instead")
399cdbee383SAndrey Ignatov LIBBPF_API int bpf_prog_attach_xattr(int prog_fd, int attachable_fd,
400cdbee383SAndrey Ignatov 				     enum bpf_attach_type type,
401cdbee383SAndrey Ignatov 				     const struct bpf_prog_attach_opts *opts);
402ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
403ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
404ab9e0848SAndrey Ignatov 				enum bpf_attach_type type);
40564a97591SLorenz Bauer 
40674fc097dSYonghong Song union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
407cc4f864bSAndrii Nakryiko struct bpf_link_create_opts {
408cc4f864bSAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
409cd31039aSYonghong Song 	__u32 flags;
41074fc097dSYonghong Song 	union bpf_iter_link_info *iter_info;
41174fc097dSYonghong Song 	__u32 iter_info_len;
412a5359091SToke Høiland-Jørgensen 	__u32 target_btf_id;
4133ec84f4bSAndrii Nakryiko 	union {
4143ec84f4bSAndrii Nakryiko 		struct {
4153ec84f4bSAndrii Nakryiko 			__u64 bpf_cookie;
4163ec84f4bSAndrii Nakryiko 		} perf_event;
4175117c26eSJiri Olsa 		struct {
4185117c26eSJiri Olsa 			__u32 flags;
4195117c26eSJiri Olsa 			__u32 cnt;
4205117c26eSJiri Olsa 			const char **syms;
4215117c26eSJiri Olsa 			const unsigned long *addrs;
4225117c26eSJiri Olsa 			const __u64 *cookies;
4235117c26eSJiri Olsa 		} kprobe_multi;
424129b9c5eSKui-Feng Lee 		struct {
425129b9c5eSKui-Feng Lee 			__u64 cookie;
426129b9c5eSKui-Feng Lee 		} tracing;
427cc4f864bSAndrii Nakryiko 	};
4283ec84f4bSAndrii Nakryiko 	size_t :0;
4293ec84f4bSAndrii Nakryiko };
4305117c26eSJiri Olsa #define bpf_link_create_opts__last_field kprobe_multi.cookies
431cc4f864bSAndrii Nakryiko 
432cc4f864bSAndrii Nakryiko LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
433cc4f864bSAndrii Nakryiko 			       enum bpf_attach_type attach_type,
434cc4f864bSAndrii Nakryiko 			       const struct bpf_link_create_opts *opts);
435cc4f864bSAndrii Nakryiko 
4362e49527eSAndrii Nakryiko LIBBPF_API int bpf_link_detach(int link_fd);
4372e49527eSAndrii Nakryiko 
438cc4f864bSAndrii Nakryiko struct bpf_link_update_opts {
439cc4f864bSAndrii Nakryiko 	size_t sz; /* size of this struct for forward/backward compatibility */
440cc4f864bSAndrii Nakryiko 	__u32 flags;	   /* extra flags */
441cc4f864bSAndrii Nakryiko 	__u32 old_prog_fd; /* expected old program FD */
442cc4f864bSAndrii Nakryiko };
443cc4f864bSAndrii Nakryiko #define bpf_link_update_opts__last_field old_prog_fd
444cc4f864bSAndrii Nakryiko 
445cc4f864bSAndrii Nakryiko LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
446cc4f864bSAndrii Nakryiko 			       const struct bpf_link_update_opts *opts);
447cc4f864bSAndrii Nakryiko 
448c09add2fSYonghong Song LIBBPF_API int bpf_iter_create(int link_fd);
449c09add2fSYonghong Song 
45064a97591SLorenz Bauer struct bpf_prog_test_run_attr {
45164a97591SLorenz Bauer 	int prog_fd;
45264a97591SLorenz Bauer 	int repeat;
45364a97591SLorenz Bauer 	const void *data_in;
45464a97591SLorenz Bauer 	__u32 data_size_in;
45564a97591SLorenz Bauer 	void *data_out;      /* optional */
45664a97591SLorenz Bauer 	__u32 data_size_out; /* in: max length of data_out
45764a97591SLorenz Bauer 			      * out: length of data_out */
45864a97591SLorenz Bauer 	__u32 retval;        /* out: return code of the BPF program */
45964a97591SLorenz Bauer 	__u32 duration;      /* out: average per repetition in ns */
4605e903c65SStanislav Fomichev 	const void *ctx_in; /* optional */
4615e903c65SStanislav Fomichev 	__u32 ctx_size_in;
4625e903c65SStanislav Fomichev 	void *ctx_out;      /* optional */
4635e903c65SStanislav Fomichev 	__u32 ctx_size_out; /* in: max length of ctx_out
4645e903c65SStanislav Fomichev 			     * out: length of cxt_out */
46564a97591SLorenz Bauer };
46664a97591SLorenz Bauer 
4673e1ab843SDelyan Kratunov LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_test_run_opts() instead")
46864a97591SLorenz Bauer LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);
46964a97591SLorenz Bauer 
47064a97591SLorenz Bauer /*
47164a97591SLorenz Bauer  * bpf_prog_test_run does not check that data_out is large enough. Consider
4723e1ab843SDelyan Kratunov  * using bpf_prog_test_run_opts instead.
47364a97591SLorenz Bauer  */
4743e1ab843SDelyan Kratunov LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_test_run_opts() instead")
475ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
476ab9e0848SAndrey Ignatov 				 __u32 size, void *data_out, __u32 *size_out,
477ab9e0848SAndrey Ignatov 				 __u32 *retval, __u32 *duration);
478ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
479ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
48009d7c2e3SQuentin Monnet LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
4810dbc8668SAndrii Nakryiko LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
482ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
483ab9e0848SAndrey Ignatov LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
484ab9e0848SAndrey Ignatov LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
4850dbc8668SAndrii Nakryiko LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
4860dbc8668SAndrii Nakryiko LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
487ab9e0848SAndrey Ignatov LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
488ab9e0848SAndrey Ignatov 			      __u32 query_flags, __u32 *attach_flags,
489ab9e0848SAndrey Ignatov 			      __u32 *prog_ids, __u32 *prog_cnt);
490ab9e0848SAndrey Ignatov LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
491ab9e0848SAndrey Ignatov LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
492ab9e0848SAndrey Ignatov 				 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
493ab9e0848SAndrey Ignatov 				 __u64 *probe_offset, __u64 *probe_addr);
4944e15507fSAndrii Nakryiko 
4954e15507fSAndrii Nakryiko enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
4960bee1067SSong Liu LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
4978c4905b9SStanislav Fomichev 
4985d23328dSYiFei Zhu struct bpf_prog_bind_opts {
4995d23328dSYiFei Zhu 	size_t sz; /* size of this struct for forward/backward compatibility */
5005d23328dSYiFei Zhu 	__u32 flags;
5015d23328dSYiFei Zhu };
5025d23328dSYiFei Zhu #define bpf_prog_bind_opts__last_field flags
5035d23328dSYiFei Zhu 
5045d23328dSYiFei Zhu LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd,
5055d23328dSYiFei Zhu 				 const struct bpf_prog_bind_opts *opts);
50688f7fe72SSong Liu 
50788f7fe72SSong Liu struct bpf_test_run_opts {
50888f7fe72SSong Liu 	size_t sz; /* size of this struct for forward/backward compatibility */
50988f7fe72SSong Liu 	const void *data_in; /* optional */
51088f7fe72SSong Liu 	void *data_out;      /* optional */
51188f7fe72SSong Liu 	__u32 data_size_in;
51288f7fe72SSong Liu 	__u32 data_size_out; /* in: max length of data_out
51388f7fe72SSong Liu 			      * out: length of data_out
51488f7fe72SSong Liu 			      */
51588f7fe72SSong Liu 	const void *ctx_in; /* optional */
51688f7fe72SSong Liu 	void *ctx_out;      /* optional */
51788f7fe72SSong Liu 	__u32 ctx_size_in;
51888f7fe72SSong Liu 	__u32 ctx_size_out; /* in: max length of ctx_out
51988f7fe72SSong Liu 			     * out: length of cxt_out
52088f7fe72SSong Liu 			     */
52188f7fe72SSong Liu 	__u32 retval;        /* out: return code of the BPF program */
52288f7fe72SSong Liu 	int repeat;
52388f7fe72SSong Liu 	__u32 duration;      /* out: average per repetition in ns */
52488f7fe72SSong Liu 	__u32 flags;
52588f7fe72SSong Liu 	__u32 cpu;
52624592ad1SToke Høiland-Jørgensen 	__u32 batch_size;
52788f7fe72SSong Liu };
52824592ad1SToke Høiland-Jørgensen #define bpf_test_run_opts__last_field batch_size
52988f7fe72SSong Liu 
53088f7fe72SSong Liu LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
53188f7fe72SSong Liu 				      struct bpf_test_run_opts *opts);
53288f7fe72SSong Liu 
5338c4905b9SStanislav Fomichev #ifdef __cplusplus
5348c4905b9SStanislav Fomichev } /* extern "C" */
5358c4905b9SStanislav Fomichev #endif
5368c4905b9SStanislav Fomichev 
537eff81908SAndrey Ignatov #endif /* __LIBBPF_BPF_H */
538