blob: 3a44d3a272af40119b379fe1930244909499fd8d [file] [log] [blame]
Alexei Starovoitov249b8122014-12-01 15:06:37 -08001#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;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070018static 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;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070024static void (*bpf_tail_call)(void *ctx, void *map, int index) =
25 (void *) BPF_FUNC_tail_call;
Alexei Starovoitov530b2c82015-05-19 16:59:06 -070026static unsigned long long (*bpf_get_smp_processor_id)(void) =
27 (void *) BPF_FUNC_get_smp_processor_id;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070028static 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;
Kaixu Xia47efb302015-08-06 07:02:36 +000034static int (*bpf_perf_event_read)(void *map, int index) =
35 (void *) BPF_FUNC_perf_event_read;
Alexei Starovoitov249b8122014-12-01 15:06:37 -080036
37/* llvm builtin functions that eBPF C program may use to
38 * emit BPF_LD_ABS and BPF_LD_IND instructions
39 */
40struct sk_buff;
41unsigned long long load_byte(void *skb,
42 unsigned long long off) asm("llvm.bpf.load.byte");
43unsigned long long load_half(void *skb,
44 unsigned long long off) asm("llvm.bpf.load.half");
45unsigned long long load_word(void *skb,
46 unsigned long long off) asm("llvm.bpf.load.word");
47
48/* a helper structure used by eBPF C program
49 * to describe map attributes to elf_bpf loader
50 */
51struct bpf_map_def {
52 unsigned int type;
53 unsigned int key_size;
54 unsigned int value_size;
55 unsigned int max_entries;
56};
57
Alexei Starovoitov91bc48222015-04-01 17:12:13 -070058static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
59 (void *) BPF_FUNC_skb_store_bytes;
60static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
61 (void *) BPF_FUNC_l3_csum_replace;
62static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
63 (void *) BPF_FUNC_l4_csum_replace;
64
Michael Holzheud9125572015-07-06 16:20:07 +020065#if defined(__x86_64__)
66
67#define PT_REGS_PARM1(x) ((x)->di)
68#define PT_REGS_PARM2(x) ((x)->si)
69#define PT_REGS_PARM3(x) ((x)->dx)
70#define PT_REGS_PARM4(x) ((x)->cx)
71#define PT_REGS_PARM5(x) ((x)->r8)
72#define PT_REGS_RET(x) ((x)->sp)
73#define PT_REGS_FP(x) ((x)->bp)
74#define PT_REGS_RC(x) ((x)->ax)
75#define PT_REGS_SP(x) ((x)->sp)
76
77#elif defined(__s390x__)
78
79#define PT_REGS_PARM1(x) ((x)->gprs[2])
80#define PT_REGS_PARM2(x) ((x)->gprs[3])
81#define PT_REGS_PARM3(x) ((x)->gprs[4])
82#define PT_REGS_PARM4(x) ((x)->gprs[5])
83#define PT_REGS_PARM5(x) ((x)->gprs[6])
84#define PT_REGS_RET(x) ((x)->gprs[14])
85#define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
86#define PT_REGS_RC(x) ((x)->gprs[2])
87#define PT_REGS_SP(x) ((x)->gprs[15])
88
89#endif
Alexei Starovoitov249b8122014-12-01 15:06:37 -080090#endif