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