1f08c3bdfSopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2019-2020 Linux Test Project
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci#ifndef LTP_BPF_COMMON_H
7f08c3bdfSopenharmony_ci#define LTP_BPF_COMMON_H
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci#include <sys/types.h>
10f08c3bdfSopenharmony_ci#include <inttypes.h>
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#include "lapi/bpf.h"
13f08c3bdfSopenharmony_ci#include "lapi/socket.h"
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#define BPF_MEMLOCK_ADD (2*1024*1024)
16f08c3bdfSopenharmony_ci#define BUFSIZE 8192
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci/* map[array_indx] = reg_to_save
19f08c3bdfSopenharmony_ci *
20f08c3bdfSopenharmony_ci * Inserts the following instructions
21f08c3bdfSopenharmony_ci *
22f08c3bdfSopenharmony_ci * r1 = map_fd
23f08c3bdfSopenharmony_ci * r2 = fp
24f08c3bdfSopenharmony_ci * r2 = r2 - 4
25f08c3bdfSopenharmony_ci * r2 = array_indx
26f08c3bdfSopenharmony_ci * call map_lookup_elem(r1, r2)
27f08c3bdfSopenharmony_ci * if r0 != 0 goto pc+1
28f08c3bdfSopenharmony_ci * exit
29f08c3bdfSopenharmony_ci * *r0 = reg_to_save
30f08c3bdfSopenharmony_ci *
31f08c3bdfSopenharmony_ci */
32f08c3bdfSopenharmony_ci#define BPF_MAP_ARRAY_STX(map_fd, array_indx, reg_to_save)\
33f08c3bdfSopenharmony_ci	BPF_LD_MAP_FD(BPF_REG_1, map_fd),		\
34f08c3bdfSopenharmony_ci	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),		\
35f08c3bdfSopenharmony_ci	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),		\
36f08c3bdfSopenharmony_ci	BPF_ST_MEM(BPF_W, BPF_REG_2, 0, array_indx),	\
37f08c3bdfSopenharmony_ci	BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),	\
38f08c3bdfSopenharmony_ci	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),		\
39f08c3bdfSopenharmony_ci	BPF_EXIT_INSN(),				\
40f08c3bdfSopenharmony_ci	BPF_STX_MEM(BPF_DW, BPF_REG_0, reg_to_save, 0)
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_civoid rlimit_bump_memlock(void);
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ciint bpf_map_create(union bpf_attr *const attr)
45f08c3bdfSopenharmony_ci	__attribute__((nonnull, warn_unused_result));
46f08c3bdfSopenharmony_ciint bpf_map_array_create(const uint32_t max_entries)
47f08c3bdfSopenharmony_ci	__attribute__((warn_unused_result));
48f08c3bdfSopenharmony_civoid bpf_map_array_get(const int map_fd,
49f08c3bdfSopenharmony_ci		       const uint32_t *const array_indx,
50f08c3bdfSopenharmony_ci		       uint64_t *const array_val)
51f08c3bdfSopenharmony_ci	__attribute__((nonnull));
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_civoid bpf_init_prog_attr(union bpf_attr *const attr,
54f08c3bdfSopenharmony_ci			const struct bpf_insn *const prog,
55f08c3bdfSopenharmony_ci			const size_t prog_size,
56f08c3bdfSopenharmony_ci			char *const log_buf, const size_t log_size)
57f08c3bdfSopenharmony_ci	__attribute__((nonnull));
58f08c3bdfSopenharmony_ciint bpf_load_prog(union bpf_attr *const attr, const char *const log)
59f08c3bdfSopenharmony_ci	__attribute__((nonnull, warn_unused_result));
60f08c3bdfSopenharmony_civoid bpf_run_prog(const int prog_fd,
61f08c3bdfSopenharmony_ci		  const char *const msg, const size_t msg_len)
62f08c3bdfSopenharmony_ci	__attribute__((nonnull));
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci#endif
65