Merge remote-tracking branch 'vfio/next'
[deliverable/linux.git] / samples / bpf / bpf_helpers.h
CommitLineData
249b812d
AS
1#ifndef __BPF_HELPERS_H
2#define __BPF_HELPERS_H
3
4/* helper macro to place programs, maps, license in
5 * different sections in elf_bpf file. Section names
6 * are interpreted by elf_bpf loader
7 */
8#define SEC(NAME) __attribute__((section(NAME), used))
9
10/* helper functions called from eBPF programs written in C */
11static void *(*bpf_map_lookup_elem)(void *map, void *key) =
12 (void *) BPF_FUNC_map_lookup_elem;
13static int (*bpf_map_update_elem)(void *map, void *key, void *value,
14 unsigned long long flags) =
15 (void *) BPF_FUNC_map_update_elem;
16static int (*bpf_map_delete_elem)(void *map, void *key) =
17 (void *) BPF_FUNC_map_delete_elem;
b896c4f9
AS
18static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) =
19 (void *) BPF_FUNC_probe_read;
20static unsigned long long (*bpf_ktime_get_ns)(void) =
21 (void *) BPF_FUNC_ktime_get_ns;
22static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
23 (void *) BPF_FUNC_trace_printk;
5bacd780
AS
24static void (*bpf_tail_call)(void *ctx, void *map, int index) =
25 (void *) BPF_FUNC_tail_call;
530b2c86
AS
26static unsigned long long (*bpf_get_smp_processor_id)(void) =
27 (void *) BPF_FUNC_get_smp_processor_id;
ffeedafb
AS
28static unsigned long long (*bpf_get_current_pid_tgid)(void) =
29 (void *) BPF_FUNC_get_current_pid_tgid;
30static unsigned long long (*bpf_get_current_uid_gid)(void) =
31 (void *) BPF_FUNC_get_current_uid_gid;
32static int (*bpf_get_current_comm)(void *buf, int buf_size) =
33 (void *) BPF_FUNC_get_current_comm;
47efb302
KX
34static int (*bpf_perf_event_read)(void *map, int index) =
35 (void *) BPF_FUNC_perf_event_read;
27b29f63
AS
36static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) =
37 (void *) BPF_FUNC_clone_redirect;
38static int (*bpf_redirect)(int ifindex, int flags) =
39 (void *) BPF_FUNC_redirect;
05b8ad25
AB
40static int (*bpf_perf_event_output)(void *ctx, void *map,
41 unsigned long long flags, void *data,
42 int size) =
39111695 43 (void *) BPF_FUNC_perf_event_output;
a6ffe7b9
AS
44static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
45 (void *) BPF_FUNC_get_stackid;
96ae5227
SD
46static int (*bpf_probe_write_user)(void *dst, void *src, int size) =
47 (void *) BPF_FUNC_probe_write_user;
9e6e60ec
SD
48static int (*bpf_current_task_under_cgroup)(void *map, int index) =
49 (void *) BPF_FUNC_current_task_under_cgroup;
6afb1e28
WT
50static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) =
51 (void *) BPF_FUNC_skb_get_tunnel_key;
52static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) =
53 (void *) BPF_FUNC_skb_set_tunnel_key;
54static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) =
55 (void *) BPF_FUNC_skb_get_tunnel_opt;
56static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) =
57 (void *) BPF_FUNC_skb_set_tunnel_opt;
1c47910e
AS
58static unsigned long long (*bpf_get_prandom_u32)(void) =
59 (void *) BPF_FUNC_get_prandom_u32;
249b812d
AS
60
61/* llvm builtin functions that eBPF C program may use to
62 * emit BPF_LD_ABS and BPF_LD_IND instructions
63 */
64struct sk_buff;
65unsigned long long load_byte(void *skb,
66 unsigned long long off) asm("llvm.bpf.load.byte");
67unsigned long long load_half(void *skb,
68 unsigned long long off) asm("llvm.bpf.load.half");
69unsigned long long load_word(void *skb,
70 unsigned long long off) asm("llvm.bpf.load.word");
71
72/* a helper structure used by eBPF C program
73 * to describe map attributes to elf_bpf loader
74 */
75struct bpf_map_def {
76 unsigned int type;
77 unsigned int key_size;
78 unsigned int value_size;
79 unsigned int max_entries;
89b97607 80 unsigned int map_flags;
249b812d
AS
81};
82
91bc4822
AS
83static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
84 (void *) BPF_FUNC_skb_store_bytes;
85static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
86 (void *) BPF_FUNC_l3_csum_replace;
87static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
88 (void *) BPF_FUNC_l4_csum_replace;
747ea55e
DB
89static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
90 (void *) BPF_FUNC_skb_under_cgroup;
91bc4822 91
d912557b
MH
92#if defined(__x86_64__)
93
94#define PT_REGS_PARM1(x) ((x)->di)
95#define PT_REGS_PARM2(x) ((x)->si)
96#define PT_REGS_PARM3(x) ((x)->dx)
97#define PT_REGS_PARM4(x) ((x)->cx)
98#define PT_REGS_PARM5(x) ((x)->r8)
99#define PT_REGS_RET(x) ((x)->sp)
100#define PT_REGS_FP(x) ((x)->bp)
101#define PT_REGS_RC(x) ((x)->ax)
102#define PT_REGS_SP(x) ((x)->sp)
138d6153 103#define PT_REGS_IP(x) ((x)->ip)
d912557b
MH
104
105#elif defined(__s390x__)
106
107#define PT_REGS_PARM1(x) ((x)->gprs[2])
108#define PT_REGS_PARM2(x) ((x)->gprs[3])
109#define PT_REGS_PARM3(x) ((x)->gprs[4])
110#define PT_REGS_PARM4(x) ((x)->gprs[5])
111#define PT_REGS_PARM5(x) ((x)->gprs[6])
112#define PT_REGS_RET(x) ((x)->gprs[14])
113#define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
114#define PT_REGS_RC(x) ((x)->gprs[2])
115#define PT_REGS_SP(x) ((x)->gprs[15])
138d6153 116#define PT_REGS_IP(x) ((x)->ip)
d912557b 117
85ff8a43
YS
118#elif defined(__aarch64__)
119
120#define PT_REGS_PARM1(x) ((x)->regs[0])
121#define PT_REGS_PARM2(x) ((x)->regs[1])
122#define PT_REGS_PARM3(x) ((x)->regs[2])
123#define PT_REGS_PARM4(x) ((x)->regs[3])
124#define PT_REGS_PARM5(x) ((x)->regs[4])
125#define PT_REGS_RET(x) ((x)->regs[30])
126#define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
127#define PT_REGS_RC(x) ((x)->regs[0])
128#define PT_REGS_SP(x) ((x)->sp)
138d6153
NR
129#define PT_REGS_IP(x) ((x)->pc)
130
131#elif defined(__powerpc__)
132
133#define PT_REGS_PARM1(x) ((x)->gpr[3])
134#define PT_REGS_PARM2(x) ((x)->gpr[4])
135#define PT_REGS_PARM3(x) ((x)->gpr[5])
136#define PT_REGS_PARM4(x) ((x)->gpr[6])
137#define PT_REGS_PARM5(x) ((x)->gpr[7])
138#define PT_REGS_RC(x) ((x)->gpr[3])
139#define PT_REGS_SP(x) ((x)->sp)
140#define PT_REGS_IP(x) ((x)->nip)
85ff8a43 141
d912557b 142#endif
138d6153
NR
143
144#ifdef __powerpc__
145#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
146#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
147#else
148#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \
149 bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
150#define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \
151 bpf_probe_read(&(ip), sizeof(ip), \
152 (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
153#endif
154
249b812d 155#endif
This page took 0.097958 seconds and 5 git commands to generate.