^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef _PERF_BPF_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _PERF_BPF_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <uapi/linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * A helper structure used by eBPF C program to describe map attributes to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * elf_bpf loader, taken from tools/testing/selftests/bpf/bpf_helpers.h:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) struct bpf_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) unsigned int key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) unsigned int value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) unsigned int max_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) unsigned int map_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) unsigned int inner_map_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) unsigned int numa_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define bpf_map(name, _type, type_key, type_val, _max_entries) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct bpf_map SEC("maps") name = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .type = BPF_MAP_TYPE_##_type, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) .key_size = sizeof(type_key), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) .value_size = sizeof(type_val), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .max_entries = _max_entries, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct ____btf_map_##name { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) type_key key; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) type_val value; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct ____btf_map_##name __attribute__((section(".maps." #name), used)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ____btf_map_##name = { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * FIXME: this should receive .max_entries as a parameter, as careful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * tuning of these limits is needed to avoid hitting limits that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * prevents other BPF constructs, such as tracepoint handlers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * to get installed, with cryptic messages from libbpf, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * For the current need, 'perf trace --filter-pids', 64 should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * be good enough, but this surely needs to be revisited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define pid_map(name, value_type) bpf_map(name, HASH, pid_t, value_type, 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int (*bpf_map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags) = (void *)BPF_FUNC_map_update_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static void *(*bpf_map_lookup_elem)(struct bpf_map *map, void *key) = (void *)BPF_FUNC_map_lookup_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static void (*bpf_tail_call)(void *ctx, void *map, int index) = (void *)BPF_FUNC_tail_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define SEC(NAME) __attribute__((section(NAME), used))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define probe(function, vars) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) SEC(#function "=" #function " " #vars) function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define syscall_enter(name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) SEC("syscalls:sys_enter_" #name) syscall_enter_ ## name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define syscall_exit(name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) SEC("syscalls:sys_exit_" #name) syscall_exit_ ## name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define license(name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) char _license[] SEC("license") = #name; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int _version SEC("version") = LINUX_VERSION_CODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #endif /* _PERF_BPF_H */