18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __BPF_LOAD_H
38c2ecf20Sopenharmony_ci#define __BPF_LOAD_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <bpf/bpf.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#define MAX_MAPS 32
88c2ecf20Sopenharmony_ci#define MAX_PROGS 32
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistruct bpf_load_map_def {
118c2ecf20Sopenharmony_ci	unsigned int type;
128c2ecf20Sopenharmony_ci	unsigned int key_size;
138c2ecf20Sopenharmony_ci	unsigned int value_size;
148c2ecf20Sopenharmony_ci	unsigned int max_entries;
158c2ecf20Sopenharmony_ci	unsigned int map_flags;
168c2ecf20Sopenharmony_ci	unsigned int inner_map_idx;
178c2ecf20Sopenharmony_ci	unsigned int numa_node;
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistruct bpf_map_data {
218c2ecf20Sopenharmony_ci	int fd;
228c2ecf20Sopenharmony_ci	char *name;
238c2ecf20Sopenharmony_ci	size_t elf_offset;
248c2ecf20Sopenharmony_ci	struct bpf_load_map_def def;
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_citypedef void (*fixup_map_cb)(struct bpf_map_data *map, int idx);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciextern int prog_fd[MAX_PROGS];
308c2ecf20Sopenharmony_ciextern int event_fd[MAX_PROGS];
318c2ecf20Sopenharmony_ciextern char bpf_log_buf[BPF_LOG_BUF_SIZE];
328c2ecf20Sopenharmony_ciextern int prog_cnt;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* There is a one-to-one mapping between map_fd[] and map_data[].
358c2ecf20Sopenharmony_ci * The map_data[] just contains more rich info on the given map.
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ciextern int map_fd[MAX_MAPS];
388c2ecf20Sopenharmony_ciextern struct bpf_map_data map_data[MAX_MAPS];
398c2ecf20Sopenharmony_ciextern int map_data_count;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* parses elf file compiled by llvm .c->.o
428c2ecf20Sopenharmony_ci * . parses 'maps' section and creates maps via BPF syscall
438c2ecf20Sopenharmony_ci * . parses 'license' section and passes it to syscall
448c2ecf20Sopenharmony_ci * . parses elf relocations for BPF maps and adjusts BPF_LD_IMM64 insns by
458c2ecf20Sopenharmony_ci *   storing map_fd into insn->imm and marking such insns as BPF_PSEUDO_MAP_FD
468c2ecf20Sopenharmony_ci * . loads eBPF programs via BPF syscall
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * One ELF file can contain multiple BPF programs which will be loaded
498c2ecf20Sopenharmony_ci * and their FDs stored stored in prog_fd array
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * returns zero on success
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ciint load_bpf_file(char *path);
548c2ecf20Sopenharmony_ciint load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciint bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
578c2ecf20Sopenharmony_ci#endif
58