Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 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 */ |
| 11 | static void *(*bpf_map_lookup_elem)(void *map, void *key) = |
| 12 | (void *) BPF_FUNC_map_lookup_elem; |
| 13 | static int (*bpf_map_update_elem)(void *map, void *key, void *value, |
| 14 | unsigned long long flags) = |
| 15 | (void *) BPF_FUNC_map_update_elem; |
| 16 | static int (*bpf_map_delete_elem)(void *map, void *key) = |
| 17 | (void *) BPF_FUNC_map_delete_elem; |
Alexei Starovoitov | b896c4f | 2015-03-25 12:49:23 -0700 | [diff] [blame] | 18 | static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) = |
| 19 | (void *) BPF_FUNC_probe_read; |
| 20 | static unsigned long long (*bpf_ktime_get_ns)(void) = |
| 21 | (void *) BPF_FUNC_ktime_get_ns; |
| 22 | static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = |
| 23 | (void *) BPF_FUNC_trace_printk; |
Alexei Starovoitov | 5bacd78 | 2015-05-19 16:59:05 -0700 | [diff] [blame] | 24 | static void (*bpf_tail_call)(void *ctx, void *map, int index) = |
| 25 | (void *) BPF_FUNC_tail_call; |
Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 26 | static unsigned long long (*bpf_get_smp_processor_id)(void) = |
| 27 | (void *) BPF_FUNC_get_smp_processor_id; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 28 | static unsigned long long (*bpf_get_current_pid_tgid)(void) = |
| 29 | (void *) BPF_FUNC_get_current_pid_tgid; |
| 30 | static unsigned long long (*bpf_get_current_uid_gid)(void) = |
| 31 | (void *) BPF_FUNC_get_current_uid_gid; |
| 32 | static int (*bpf_get_current_comm)(void *buf, int buf_size) = |
| 33 | (void *) BPF_FUNC_get_current_comm; |
Kaixu Xia | 47efb30 | 2015-08-06 07:02:36 +0000 | [diff] [blame] | 34 | static int (*bpf_perf_event_read)(void *map, int index) = |
| 35 | (void *) BPF_FUNC_perf_event_read; |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 36 | static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) = |
| 37 | (void *) BPF_FUNC_clone_redirect; |
| 38 | static int (*bpf_redirect)(int ifindex, int flags) = |
| 39 | (void *) BPF_FUNC_redirect; |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 40 | static int (*bpf_perf_event_output)(void *ctx, void *map, int index, void *data, int size) = |
| 41 | (void *) BPF_FUNC_perf_event_output; |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 42 | static int (*bpf_get_stackid)(void *ctx, void *map, int flags) = |
| 43 | (void *) BPF_FUNC_get_stackid; |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame^] | 44 | static int (*bpf_probe_write_user)(void *dst, void *src, int size) = |
| 45 | (void *) BPF_FUNC_probe_write_user; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 46 | |
| 47 | /* llvm builtin functions that eBPF C program may use to |
| 48 | * emit BPF_LD_ABS and BPF_LD_IND instructions |
| 49 | */ |
| 50 | struct sk_buff; |
| 51 | unsigned long long load_byte(void *skb, |
| 52 | unsigned long long off) asm("llvm.bpf.load.byte"); |
| 53 | unsigned long long load_half(void *skb, |
| 54 | unsigned long long off) asm("llvm.bpf.load.half"); |
| 55 | unsigned long long load_word(void *skb, |
| 56 | unsigned long long off) asm("llvm.bpf.load.word"); |
| 57 | |
| 58 | /* a helper structure used by eBPF C program |
| 59 | * to describe map attributes to elf_bpf loader |
| 60 | */ |
| 61 | struct bpf_map_def { |
| 62 | unsigned int type; |
| 63 | unsigned int key_size; |
| 64 | unsigned int value_size; |
| 65 | unsigned int max_entries; |
Alexei Starovoitov | 89b9760 | 2016-03-07 21:57:20 -0800 | [diff] [blame] | 66 | unsigned int map_flags; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 69 | static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) = |
| 70 | (void *) BPF_FUNC_skb_store_bytes; |
| 71 | static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) = |
| 72 | (void *) BPF_FUNC_l3_csum_replace; |
| 73 | static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) = |
| 74 | (void *) BPF_FUNC_l4_csum_replace; |
Martin KaFai Lau | a3f7461 | 2016-06-30 10:28:45 -0700 | [diff] [blame] | 75 | static int (*bpf_skb_in_cgroup)(void *ctx, void *map, int index) = |
| 76 | (void *) BPF_FUNC_skb_in_cgroup; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 77 | |
Michael Holzheu | d912557 | 2015-07-06 16:20:07 +0200 | [diff] [blame] | 78 | #if defined(__x86_64__) |
| 79 | |
| 80 | #define PT_REGS_PARM1(x) ((x)->di) |
| 81 | #define PT_REGS_PARM2(x) ((x)->si) |
| 82 | #define PT_REGS_PARM3(x) ((x)->dx) |
| 83 | #define PT_REGS_PARM4(x) ((x)->cx) |
| 84 | #define PT_REGS_PARM5(x) ((x)->r8) |
| 85 | #define PT_REGS_RET(x) ((x)->sp) |
| 86 | #define PT_REGS_FP(x) ((x)->bp) |
| 87 | #define PT_REGS_RC(x) ((x)->ax) |
| 88 | #define PT_REGS_SP(x) ((x)->sp) |
Naveen N. Rao | 138d615 | 2016-04-04 22:31:34 +0530 | [diff] [blame] | 89 | #define PT_REGS_IP(x) ((x)->ip) |
Michael Holzheu | d912557 | 2015-07-06 16:20:07 +0200 | [diff] [blame] | 90 | |
| 91 | #elif defined(__s390x__) |
| 92 | |
| 93 | #define PT_REGS_PARM1(x) ((x)->gprs[2]) |
| 94 | #define PT_REGS_PARM2(x) ((x)->gprs[3]) |
| 95 | #define PT_REGS_PARM3(x) ((x)->gprs[4]) |
| 96 | #define PT_REGS_PARM4(x) ((x)->gprs[5]) |
| 97 | #define PT_REGS_PARM5(x) ((x)->gprs[6]) |
| 98 | #define PT_REGS_RET(x) ((x)->gprs[14]) |
| 99 | #define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */ |
| 100 | #define PT_REGS_RC(x) ((x)->gprs[2]) |
| 101 | #define PT_REGS_SP(x) ((x)->gprs[15]) |
Naveen N. Rao | 138d615 | 2016-04-04 22:31:34 +0530 | [diff] [blame] | 102 | #define PT_REGS_IP(x) ((x)->ip) |
Michael Holzheu | d912557 | 2015-07-06 16:20:07 +0200 | [diff] [blame] | 103 | |
Yang Shi | 85ff8a43 | 2015-10-26 17:02:19 -0700 | [diff] [blame] | 104 | #elif defined(__aarch64__) |
| 105 | |
| 106 | #define PT_REGS_PARM1(x) ((x)->regs[0]) |
| 107 | #define PT_REGS_PARM2(x) ((x)->regs[1]) |
| 108 | #define PT_REGS_PARM3(x) ((x)->regs[2]) |
| 109 | #define PT_REGS_PARM4(x) ((x)->regs[3]) |
| 110 | #define PT_REGS_PARM5(x) ((x)->regs[4]) |
| 111 | #define PT_REGS_RET(x) ((x)->regs[30]) |
| 112 | #define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */ |
| 113 | #define PT_REGS_RC(x) ((x)->regs[0]) |
| 114 | #define PT_REGS_SP(x) ((x)->sp) |
Naveen N. Rao | 138d615 | 2016-04-04 22:31:34 +0530 | [diff] [blame] | 115 | #define PT_REGS_IP(x) ((x)->pc) |
| 116 | |
| 117 | #elif defined(__powerpc__) |
| 118 | |
| 119 | #define PT_REGS_PARM1(x) ((x)->gpr[3]) |
| 120 | #define PT_REGS_PARM2(x) ((x)->gpr[4]) |
| 121 | #define PT_REGS_PARM3(x) ((x)->gpr[5]) |
| 122 | #define PT_REGS_PARM4(x) ((x)->gpr[6]) |
| 123 | #define PT_REGS_PARM5(x) ((x)->gpr[7]) |
| 124 | #define PT_REGS_RC(x) ((x)->gpr[3]) |
| 125 | #define PT_REGS_SP(x) ((x)->sp) |
| 126 | #define PT_REGS_IP(x) ((x)->nip) |
Yang Shi | 85ff8a43 | 2015-10-26 17:02:19 -0700 | [diff] [blame] | 127 | |
Michael Holzheu | d912557 | 2015-07-06 16:20:07 +0200 | [diff] [blame] | 128 | #endif |
Naveen N. Rao | 138d615 | 2016-04-04 22:31:34 +0530 | [diff] [blame] | 129 | |
| 130 | #ifdef __powerpc__ |
| 131 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) |
| 132 | #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP |
| 133 | #else |
| 134 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \ |
| 135 | bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) |
| 136 | #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \ |
| 137 | bpf_probe_read(&(ip), sizeof(ip), \ |
| 138 | (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) |
| 139 | #endif |
| 140 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 141 | #endif |