Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #ifndef _PERF_BPF_H
0003 #define _PERF_BPF_H
0004 
0005 #include <uapi/linux/bpf.h>
0006 
0007 /*
0008  * A helper structure used by eBPF C program to describe map attributes to
0009  * elf_bpf loader, taken from tools/testing/selftests/bpf/bpf_helpers.h:
0010  */
0011 struct bpf_map {
0012         unsigned int type;
0013         unsigned int key_size;
0014         unsigned int value_size;
0015         unsigned int max_entries;
0016         unsigned int map_flags;
0017         unsigned int inner_map_idx;
0018         unsigned int numa_node;
0019 };
0020 
0021 #define bpf_map(name, _type, type_key, type_val, _max_entries)  \
0022 struct bpf_map SEC("maps") name = {             \
0023     .type        = BPF_MAP_TYPE_##_type,            \
0024     .key_size    = sizeof(type_key),            \
0025     .value_size  = sizeof(type_val),            \
0026     .max_entries = _max_entries,                \
0027 };                              \
0028 struct ____btf_map_##name {                 \
0029     type_key key;                       \
0030     type_val value;                                     \
0031 };                              \
0032 struct ____btf_map_##name __attribute__((section(".maps." #name), used)) \
0033     ____btf_map_##name = { }
0034 
0035 /*
0036  * FIXME: this should receive .max_entries as a parameter, as careful
0037  *    tuning of these limits is needed to avoid hitting limits that
0038  *    prevents other BPF constructs, such as tracepoint handlers,
0039  *    to get installed, with cryptic messages from libbpf, etc.
0040  *    For the current need, 'perf trace --filter-pids', 64 should
0041  *    be good enough, but this surely needs to be revisited.
0042  */
0043 #define pid_map(name, value_type) bpf_map(name, HASH, pid_t, value_type, 64)
0044 
0045 static int (*bpf_map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags) = (void *)BPF_FUNC_map_update_elem;
0046 static void *(*bpf_map_lookup_elem)(struct bpf_map *map, void *key) = (void *)BPF_FUNC_map_lookup_elem;
0047 
0048 static void (*bpf_tail_call)(void *ctx, void *map, int index) = (void *)BPF_FUNC_tail_call;
0049 
0050 #define SEC(NAME) __attribute__((section(NAME),  used))
0051 
0052 #define probe(function, vars) \
0053     SEC(#function "=" #function " " #vars) function
0054 
0055 #define syscall_enter(name) \
0056     SEC("syscalls:sys_enter_" #name) syscall_enter_ ## name
0057 
0058 #define syscall_exit(name) \
0059     SEC("syscalls:sys_exit_" #name) syscall_exit_ ## name
0060 
0061 #define license(name) \
0062 char _license[] SEC("license") = #name; \
0063 int _version SEC("version") = LINUX_VERSION_CODE;
0064 
0065 static int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read;
0066 static int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str;
0067 
0068 static int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output;
0069 
0070 #endif /* _PERF_BPF_H */