18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/*
48c2ecf20Sopenharmony_ci * common eBPF ELF operations.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
78c2ecf20Sopenharmony_ci * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
88c2ecf20Sopenharmony_ci * Copyright (C) 2015 Huawei Inc.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
118c2ecf20Sopenharmony_ci * modify it under the terms of the GNU Lesser General Public
128c2ecf20Sopenharmony_ci * License as published by the Free Software Foundation;
138c2ecf20Sopenharmony_ci * version 2.1 of the License (not later!)
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful,
168c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
178c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
188c2ecf20Sopenharmony_ci * GNU Lesser General Public License for more details.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * You should have received a copy of the GNU Lesser General Public
218c2ecf20Sopenharmony_ci * License along with this program; if not,  see <http://www.gnu.org/licenses>
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#ifndef __LIBBPF_BPF_H
248c2ecf20Sopenharmony_ci#define __LIBBPF_BPF_H
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <linux/bpf.h>
278c2ecf20Sopenharmony_ci#include <stdbool.h>
288c2ecf20Sopenharmony_ci#include <stddef.h>
298c2ecf20Sopenharmony_ci#include <stdint.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include "libbpf_common.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#ifdef __cplusplus
348c2ecf20Sopenharmony_ciextern "C" {
358c2ecf20Sopenharmony_ci#endif
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistruct bpf_create_map_attr {
388c2ecf20Sopenharmony_ci	const char *name;
398c2ecf20Sopenharmony_ci	enum bpf_map_type map_type;
408c2ecf20Sopenharmony_ci	__u32 map_flags;
418c2ecf20Sopenharmony_ci	__u32 key_size;
428c2ecf20Sopenharmony_ci	__u32 value_size;
438c2ecf20Sopenharmony_ci	__u32 max_entries;
448c2ecf20Sopenharmony_ci	__u32 numa_node;
458c2ecf20Sopenharmony_ci	__u32 btf_fd;
468c2ecf20Sopenharmony_ci	__u32 btf_key_type_id;
478c2ecf20Sopenharmony_ci	__u32 btf_value_type_id;
488c2ecf20Sopenharmony_ci	__u32 map_ifindex;
498c2ecf20Sopenharmony_ci	union {
508c2ecf20Sopenharmony_ci		__u32 inner_map_fd;
518c2ecf20Sopenharmony_ci		__u32 btf_vmlinux_value_type_id;
528c2ecf20Sopenharmony_ci	};
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ciLIBBPF_API int
568c2ecf20Sopenharmony_cibpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
578c2ecf20Sopenharmony_ciLIBBPF_API int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
588c2ecf20Sopenharmony_ci				   int key_size, int value_size,
598c2ecf20Sopenharmony_ci				   int max_entries, __u32 map_flags, int node);
608c2ecf20Sopenharmony_ciLIBBPF_API int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
618c2ecf20Sopenharmony_ci				   int key_size, int value_size,
628c2ecf20Sopenharmony_ci				   int max_entries, __u32 map_flags);
638c2ecf20Sopenharmony_ciLIBBPF_API int bpf_create_map(enum bpf_map_type map_type, int key_size,
648c2ecf20Sopenharmony_ci			      int value_size, int max_entries, __u32 map_flags);
658c2ecf20Sopenharmony_ciLIBBPF_API int bpf_create_map_in_map_node(enum bpf_map_type map_type,
668c2ecf20Sopenharmony_ci					  const char *name, int key_size,
678c2ecf20Sopenharmony_ci					  int inner_map_fd, int max_entries,
688c2ecf20Sopenharmony_ci					  __u32 map_flags, int node);
698c2ecf20Sopenharmony_ciLIBBPF_API int bpf_create_map_in_map(enum bpf_map_type map_type,
708c2ecf20Sopenharmony_ci				     const char *name, int key_size,
718c2ecf20Sopenharmony_ci				     int inner_map_fd, int max_entries,
728c2ecf20Sopenharmony_ci				     __u32 map_flags);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistruct bpf_load_program_attr {
758c2ecf20Sopenharmony_ci	enum bpf_prog_type prog_type;
768c2ecf20Sopenharmony_ci	enum bpf_attach_type expected_attach_type;
778c2ecf20Sopenharmony_ci	const char *name;
788c2ecf20Sopenharmony_ci	const struct bpf_insn *insns;
798c2ecf20Sopenharmony_ci	size_t insns_cnt;
808c2ecf20Sopenharmony_ci	const char *license;
818c2ecf20Sopenharmony_ci	union {
828c2ecf20Sopenharmony_ci		__u32 kern_version;
838c2ecf20Sopenharmony_ci		__u32 attach_prog_fd;
848c2ecf20Sopenharmony_ci	};
858c2ecf20Sopenharmony_ci	union {
868c2ecf20Sopenharmony_ci		__u32 prog_ifindex;
878c2ecf20Sopenharmony_ci		__u32 attach_btf_id;
888c2ecf20Sopenharmony_ci	};
898c2ecf20Sopenharmony_ci	__u32 prog_btf_fd;
908c2ecf20Sopenharmony_ci	__u32 func_info_rec_size;
918c2ecf20Sopenharmony_ci	const void *func_info;
928c2ecf20Sopenharmony_ci	__u32 func_info_cnt;
938c2ecf20Sopenharmony_ci	__u32 line_info_rec_size;
948c2ecf20Sopenharmony_ci	const void *line_info;
958c2ecf20Sopenharmony_ci	__u32 line_info_cnt;
968c2ecf20Sopenharmony_ci	__u32 log_level;
978c2ecf20Sopenharmony_ci	__u32 prog_flags;
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* Flags to direct loading requirements */
1018c2ecf20Sopenharmony_ci#define MAPS_RELAX_COMPAT	0x01
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* Recommend log buffer size */
1048c2ecf20Sopenharmony_ci#define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
1058c2ecf20Sopenharmony_ciLIBBPF_API int
1068c2ecf20Sopenharmony_cibpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
1078c2ecf20Sopenharmony_ci		       char *log_buf, size_t log_buf_sz);
1088c2ecf20Sopenharmony_ciLIBBPF_API int bpf_load_program(enum bpf_prog_type type,
1098c2ecf20Sopenharmony_ci				const struct bpf_insn *insns, size_t insns_cnt,
1108c2ecf20Sopenharmony_ci				const char *license, __u32 kern_version,
1118c2ecf20Sopenharmony_ci				char *log_buf, size_t log_buf_sz);
1128c2ecf20Sopenharmony_ciLIBBPF_API int bpf_verify_program(enum bpf_prog_type type,
1138c2ecf20Sopenharmony_ci				  const struct bpf_insn *insns,
1148c2ecf20Sopenharmony_ci				  size_t insns_cnt, __u32 prog_flags,
1158c2ecf20Sopenharmony_ci				  const char *license, __u32 kern_version,
1168c2ecf20Sopenharmony_ci				  char *log_buf, size_t log_buf_sz,
1178c2ecf20Sopenharmony_ci				  int log_level);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
1208c2ecf20Sopenharmony_ci				   __u64 flags);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
1238c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value,
1248c2ecf20Sopenharmony_ci					 __u64 flags);
1258c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
1268c2ecf20Sopenharmony_ci					      void *value);
1278c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
1288c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
1298c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_freeze(int fd);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistruct bpf_map_batch_opts {
1328c2ecf20Sopenharmony_ci	size_t sz; /* size of this struct for forward/backward compatibility */
1338c2ecf20Sopenharmony_ci	__u64 elem_flags;
1348c2ecf20Sopenharmony_ci	__u64 flags;
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ci#define bpf_map_batch_opts__last_field flags
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_delete_batch(int fd, void *keys,
1398c2ecf20Sopenharmony_ci				    __u32 *count,
1408c2ecf20Sopenharmony_ci				    const struct bpf_map_batch_opts *opts);
1418c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
1428c2ecf20Sopenharmony_ci				    void *keys, void *values, __u32 *count,
1438c2ecf20Sopenharmony_ci				    const struct bpf_map_batch_opts *opts);
1448c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
1458c2ecf20Sopenharmony_ci					void *out_batch, void *keys,
1468c2ecf20Sopenharmony_ci					void *values, __u32 *count,
1478c2ecf20Sopenharmony_ci					const struct bpf_map_batch_opts *opts);
1488c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values,
1498c2ecf20Sopenharmony_ci				    __u32 *count,
1508c2ecf20Sopenharmony_ci				    const struct bpf_map_batch_opts *opts);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ciLIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
1538c2ecf20Sopenharmony_ciLIBBPF_API int bpf_obj_get(const char *pathname);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistruct bpf_prog_attach_opts {
1568c2ecf20Sopenharmony_ci	size_t sz; /* size of this struct for forward/backward compatibility */
1578c2ecf20Sopenharmony_ci	unsigned int flags;
1588c2ecf20Sopenharmony_ci	int replace_prog_fd;
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci#define bpf_prog_attach_opts__last_field replace_prog_fd
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
1638c2ecf20Sopenharmony_ci			       enum bpf_attach_type type, unsigned int flags);
1648c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_attach_xattr(int prog_fd, int attachable_fd,
1658c2ecf20Sopenharmony_ci				     enum bpf_attach_type type,
1668c2ecf20Sopenharmony_ci				     const struct bpf_prog_attach_opts *opts);
1678c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
1688c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
1698c2ecf20Sopenharmony_ci				enum bpf_attach_type type);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ciunion bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
1728c2ecf20Sopenharmony_cistruct bpf_link_create_opts {
1738c2ecf20Sopenharmony_ci	size_t sz; /* size of this struct for forward/backward compatibility */
1748c2ecf20Sopenharmony_ci	__u32 flags;
1758c2ecf20Sopenharmony_ci	union bpf_iter_link_info *iter_info;
1768c2ecf20Sopenharmony_ci	__u32 iter_info_len;
1778c2ecf20Sopenharmony_ci	__u32 target_btf_id;
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci#define bpf_link_create_opts__last_field target_btf_id
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ciLIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
1828c2ecf20Sopenharmony_ci			       enum bpf_attach_type attach_type,
1838c2ecf20Sopenharmony_ci			       const struct bpf_link_create_opts *opts);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ciLIBBPF_API int bpf_link_detach(int link_fd);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistruct bpf_link_update_opts {
1888c2ecf20Sopenharmony_ci	size_t sz; /* size of this struct for forward/backward compatibility */
1898c2ecf20Sopenharmony_ci	__u32 flags;	   /* extra flags */
1908c2ecf20Sopenharmony_ci	__u32 old_prog_fd; /* expected old program FD */
1918c2ecf20Sopenharmony_ci};
1928c2ecf20Sopenharmony_ci#define bpf_link_update_opts__last_field old_prog_fd
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ciLIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
1958c2ecf20Sopenharmony_ci			       const struct bpf_link_update_opts *opts);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ciLIBBPF_API int bpf_iter_create(int link_fd);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistruct bpf_prog_test_run_attr {
2008c2ecf20Sopenharmony_ci	int prog_fd;
2018c2ecf20Sopenharmony_ci	int repeat;
2028c2ecf20Sopenharmony_ci	const void *data_in;
2038c2ecf20Sopenharmony_ci	__u32 data_size_in;
2048c2ecf20Sopenharmony_ci	void *data_out;      /* optional */
2058c2ecf20Sopenharmony_ci	__u32 data_size_out; /* in: max length of data_out
2068c2ecf20Sopenharmony_ci			      * out: length of data_out */
2078c2ecf20Sopenharmony_ci	__u32 retval;        /* out: return code of the BPF program */
2088c2ecf20Sopenharmony_ci	__u32 duration;      /* out: average per repetition in ns */
2098c2ecf20Sopenharmony_ci	const void *ctx_in; /* optional */
2108c2ecf20Sopenharmony_ci	__u32 ctx_size_in;
2118c2ecf20Sopenharmony_ci	void *ctx_out;      /* optional */
2128c2ecf20Sopenharmony_ci	__u32 ctx_size_out; /* in: max length of ctx_out
2138c2ecf20Sopenharmony_ci			     * out: length of cxt_out */
2148c2ecf20Sopenharmony_ci};
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/*
2198c2ecf20Sopenharmony_ci * bpf_prog_test_run does not check that data_out is large enough. Consider
2208c2ecf20Sopenharmony_ci * using bpf_prog_test_run_xattr instead.
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
2238c2ecf20Sopenharmony_ci				 __u32 size, void *data_out, __u32 *size_out,
2248c2ecf20Sopenharmony_ci				 __u32 *retval, __u32 *duration);
2258c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
2268c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
2278c2ecf20Sopenharmony_ciLIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
2288c2ecf20Sopenharmony_ciLIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
2298c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
2308c2ecf20Sopenharmony_ciLIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
2318c2ecf20Sopenharmony_ciLIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
2328c2ecf20Sopenharmony_ciLIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
2338c2ecf20Sopenharmony_ciLIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
2348c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
2358c2ecf20Sopenharmony_ci			      __u32 query_flags, __u32 *attach_flags,
2368c2ecf20Sopenharmony_ci			      __u32 *prog_ids, __u32 *prog_cnt);
2378c2ecf20Sopenharmony_ciLIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
2388c2ecf20Sopenharmony_ciLIBBPF_API int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf,
2398c2ecf20Sopenharmony_ci			    __u32 log_buf_size, bool do_log);
2408c2ecf20Sopenharmony_ciLIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
2418c2ecf20Sopenharmony_ci				 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
2428c2ecf20Sopenharmony_ci				 __u64 *probe_offset, __u64 *probe_addr);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci#ifdef __cplusplus
2458c2ecf20Sopenharmony_ci/* forward-declaring enums in C++ isn't compatible with pure C enums, so
2468c2ecf20Sopenharmony_ci * instead define bpf_enable_stats() as accepting int as an input
2478c2ecf20Sopenharmony_ci */
2488c2ecf20Sopenharmony_ciLIBBPF_API int bpf_enable_stats(int type);
2498c2ecf20Sopenharmony_ci#else
2508c2ecf20Sopenharmony_cienum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
2518c2ecf20Sopenharmony_ciLIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
2528c2ecf20Sopenharmony_ci#endif
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistruct bpf_prog_bind_opts {
2558c2ecf20Sopenharmony_ci	size_t sz; /* size of this struct for forward/backward compatibility */
2568c2ecf20Sopenharmony_ci	__u32 flags;
2578c2ecf20Sopenharmony_ci};
2588c2ecf20Sopenharmony_ci#define bpf_prog_bind_opts__last_field flags
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd,
2618c2ecf20Sopenharmony_ci				 const struct bpf_prog_bind_opts *opts);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistruct bpf_test_run_opts {
2648c2ecf20Sopenharmony_ci	size_t sz; /* size of this struct for forward/backward compatibility */
2658c2ecf20Sopenharmony_ci	const void *data_in; /* optional */
2668c2ecf20Sopenharmony_ci	void *data_out;      /* optional */
2678c2ecf20Sopenharmony_ci	__u32 data_size_in;
2688c2ecf20Sopenharmony_ci	__u32 data_size_out; /* in: max length of data_out
2698c2ecf20Sopenharmony_ci			      * out: length of data_out
2708c2ecf20Sopenharmony_ci			      */
2718c2ecf20Sopenharmony_ci	const void *ctx_in; /* optional */
2728c2ecf20Sopenharmony_ci	void *ctx_out;      /* optional */
2738c2ecf20Sopenharmony_ci	__u32 ctx_size_in;
2748c2ecf20Sopenharmony_ci	__u32 ctx_size_out; /* in: max length of ctx_out
2758c2ecf20Sopenharmony_ci			     * out: length of cxt_out
2768c2ecf20Sopenharmony_ci			     */
2778c2ecf20Sopenharmony_ci	__u32 retval;        /* out: return code of the BPF program */
2788c2ecf20Sopenharmony_ci	int repeat;
2798c2ecf20Sopenharmony_ci	__u32 duration;      /* out: average per repetition in ns */
2808c2ecf20Sopenharmony_ci	__u32 flags;
2818c2ecf20Sopenharmony_ci	__u32 cpu;
2828c2ecf20Sopenharmony_ci};
2838c2ecf20Sopenharmony_ci#define bpf_test_run_opts__last_field cpu
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ciLIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
2868c2ecf20Sopenharmony_ci				      struct bpf_test_run_opts *opts);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci#ifdef __cplusplus
2898c2ecf20Sopenharmony_ci} /* extern "C" */
2908c2ecf20Sopenharmony_ci#endif
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci#endif /* __LIBBPF_BPF_H */
293