blob: b422aeef53396e2494287f897194bfdc712013f1 [file] [log] [blame]
Arnaldo Carvalho de Melodd8e4ea2018-05-04 12:23:29 -03001// SPDX-License-Identifier: GPL-2.0
2#ifndef _PERF_BPF_H
3#define _PERF_BPF_H
Arnaldo Carvalho de Melo822c2622018-08-01 10:50:51 -03004
5#include <uapi/linux/bpf.h>
6
Arnaldo Carvalho de Melo7402e542018-08-06 09:02:26 -03007/*
8 * A helper structure used by eBPF C program to describe map attributes to
9 * elf_bpf loader, taken from tools/testing/selftests/bpf/bpf_helpers.h:
10 */
11struct bpf_map {
12 unsigned int type;
13 unsigned int key_size;
14 unsigned int value_size;
15 unsigned int max_entries;
16 unsigned int map_flags;
17 unsigned int inner_map_idx;
18 unsigned int numa_node;
19};
20
Arnaldo Carvalho de Melob45d5512019-01-24 15:01:46 +010021#define bpf_map(name, _type, type_key, type_val, _max_entries) \
22struct bpf_map SEC("maps") name = { \
23 .type = BPF_MAP_TYPE_##_type, \
24 .key_size = sizeof(type_key), \
25 .value_size = sizeof(type_val), \
26 .max_entries = _max_entries, \
Arnaldo Carvalho de Melo31636132019-03-01 16:09:31 -030027}; \
28struct ____btf_map_##name { \
29 type_key key; \
30 type_val value; \
31}; \
32struct ____btf_map_##name __attribute__((section(".maps." #name), used)) \
33 ____btf_map_##name = { }
Arnaldo Carvalho de Melob45d5512019-01-24 15:01:46 +010034
Arnaldo Carvalho de Melo0f7c2de2018-11-07 14:49:27 -030035/*
36 * FIXME: this should receive .max_entries as a parameter, as careful
37 * tuning of these limits is needed to avoid hitting limits that
38 * prevents other BPF constructs, such as tracepoint handlers,
39 * to get installed, with cryptic messages from libbpf, etc.
40 * For the current need, 'perf trace --filter-pids', 64 should
41 * be good enough, but this surely needs to be revisited.
42 */
Arnaldo Carvalho de Meloc657d762019-01-24 15:11:28 +010043#define pid_map(name, value_type) bpf_map(name, HASH, pid_t, value_type, 64)
Arnaldo Carvalho de Melo382b55d2018-11-06 15:46:27 -030044
45static int (*bpf_map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags) = (void *)BPF_FUNC_map_update_elem;
46static void *(*bpf_map_lookup_elem)(struct bpf_map *map, void *key) = (void *)BPF_FUNC_map_lookup_elem;
47
Arnaldo Carvalho de Melo941a7652019-07-15 15:25:41 -030048static void (*bpf_tail_call)(void *ctx, void *map, int index) = (void *)BPF_FUNC_tail_call;
49
Arnaldo Carvalho de Melodd8e4ea2018-05-04 12:23:29 -030050#define SEC(NAME) __attribute__((section(NAME), used))
Arnaldo Carvalho de Melo1f477302018-05-04 15:18:31 -030051
Arnaldo Carvalho de Melod8fc7642018-05-04 15:59:16 -030052#define probe(function, vars) \
53 SEC(#function "=" #function " " #vars) function
54
Arnaldo Carvalho de Melodda9ac92018-08-03 16:28:35 -030055#define syscall_enter(name) \
56 SEC("syscalls:sys_enter_" #name) syscall_enter_ ## name
57
Arnaldo Carvalho de Melo664b6a92018-08-30 08:48:44 -030058#define syscall_exit(name) \
59 SEC("syscalls:sys_exit_" #name) syscall_exit_ ## name
60
Arnaldo Carvalho de Melo1f477302018-05-04 15:18:31 -030061#define license(name) \
62char _license[] SEC("license") = #name; \
63int _version SEC("version") = LINUX_VERSION_CODE;
64
Arnaldo Carvalho de Melo8fa25f32018-08-07 15:10:19 -030065static int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read;
66static int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str;
67
Arnaldo Carvalho de Melo61d00712018-12-12 15:31:28 -030068static int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output;
69
Arnaldo Carvalho de Melodd8e4ea2018-05-04 12:23:29 -030070#endif /* _PERF_BPF_H */