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; |
Teng Qin | 41e9a80 | 2017-06-02 21:03:53 -0700 | [diff] [blame] | 34 | static unsigned long long (*bpf_perf_event_read)(void *map, |
| 35 | unsigned long long flags) = |
Kaixu Xia | 47efb30 | 2015-08-06 07:02:36 +0000 | [diff] [blame] | 36 | (void *) BPF_FUNC_perf_event_read; |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 37 | static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) = |
| 38 | (void *) BPF_FUNC_clone_redirect; |
| 39 | static int (*bpf_redirect)(int ifindex, int flags) = |
| 40 | (void *) BPF_FUNC_redirect; |
Adam Barth | 05b8ad2 | 2016-08-10 09:45:39 -0700 | [diff] [blame] | 41 | static int (*bpf_perf_event_output)(void *ctx, void *map, |
| 42 | unsigned long long flags, void *data, |
| 43 | int size) = |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 44 | (void *) BPF_FUNC_perf_event_output; |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 45 | static int (*bpf_get_stackid)(void *ctx, void *map, int flags) = |
| 46 | (void *) BPF_FUNC_get_stackid; |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 47 | static int (*bpf_probe_write_user)(void *dst, void *src, int size) = |
| 48 | (void *) BPF_FUNC_probe_write_user; |
Sargun Dhillon | 9e6e60e | 2016-08-12 08:57:04 -0700 | [diff] [blame] | 49 | static int (*bpf_current_task_under_cgroup)(void *map, int index) = |
| 50 | (void *) BPF_FUNC_current_task_under_cgroup; |
William Tu | 6afb1e2 | 2016-08-19 11:55:44 -0700 | [diff] [blame] | 51 | static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) = |
| 52 | (void *) BPF_FUNC_skb_get_tunnel_key; |
| 53 | static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) = |
| 54 | (void *) BPF_FUNC_skb_set_tunnel_key; |
| 55 | static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) = |
| 56 | (void *) BPF_FUNC_skb_get_tunnel_opt; |
| 57 | static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) = |
| 58 | (void *) BPF_FUNC_skb_set_tunnel_opt; |
Alexei Starovoitov | 1c47910e | 2016-09-01 18:37:25 -0700 | [diff] [blame] | 59 | static unsigned long long (*bpf_get_prandom_u32)(void) = |
| 60 | (void *) BPF_FUNC_get_prandom_u32; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 61 | static int (*bpf_xdp_adjust_head)(void *ctx, int offset) = |
| 62 | (void *) BPF_FUNC_xdp_adjust_head; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 63 | |
| 64 | /* llvm builtin functions that eBPF C program may use to |
| 65 | * emit BPF_LD_ABS and BPF_LD_IND instructions |
| 66 | */ |
| 67 | struct sk_buff; |
| 68 | unsigned long long load_byte(void *skb, |
| 69 | unsigned long long off) asm("llvm.bpf.load.byte"); |
| 70 | unsigned long long load_half(void *skb, |
| 71 | unsigned long long off) asm("llvm.bpf.load.half"); |
| 72 | unsigned long long load_word(void *skb, |
| 73 | unsigned long long off) asm("llvm.bpf.load.word"); |
| 74 | |
| 75 | /* a helper structure used by eBPF C program |
| 76 | * to describe map attributes to elf_bpf loader |
| 77 | */ |
| 78 | struct bpf_map_def { |
| 79 | unsigned int type; |
| 80 | unsigned int key_size; |
| 81 | unsigned int value_size; |
| 82 | unsigned int max_entries; |
Alexei Starovoitov | 89b9760 | 2016-03-07 21:57:20 -0800 | [diff] [blame] | 83 | unsigned int map_flags; |
Martin KaFai Lau | fb30d4b | 2017-03-22 10:00:35 -0700 | [diff] [blame] | 84 | unsigned int inner_map_idx; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Thomas Graf | f74599f | 2016-11-30 17:10:11 +0100 | [diff] [blame] | 87 | static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) = |
| 88 | (void *) BPF_FUNC_skb_load_bytes; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 89 | static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) = |
| 90 | (void *) BPF_FUNC_skb_store_bytes; |
| 91 | static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) = |
| 92 | (void *) BPF_FUNC_l3_csum_replace; |
| 93 | static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) = |
| 94 | (void *) BPF_FUNC_l4_csum_replace; |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 95 | static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) = |
| 96 | (void *) BPF_FUNC_skb_under_cgroup; |
Thomas Graf | f74599f | 2016-11-30 17:10:11 +0100 | [diff] [blame] | 97 | static int (*bpf_skb_change_head)(void *, int len, int flags) = |
| 98 | (void *) BPF_FUNC_skb_change_head; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 99 | |
Michael Holzheu | d912557 | 2015-07-06 16:20:07 +0200 | [diff] [blame] | 100 | #if defined(__x86_64__) |
| 101 | |
| 102 | #define PT_REGS_PARM1(x) ((x)->di) |
| 103 | #define PT_REGS_PARM2(x) ((x)->si) |
| 104 | #define PT_REGS_PARM3(x) ((x)->dx) |
| 105 | #define PT_REGS_PARM4(x) ((x)->cx) |
| 106 | #define PT_REGS_PARM5(x) ((x)->r8) |
| 107 | #define PT_REGS_RET(x) ((x)->sp) |
| 108 | #define PT_REGS_FP(x) ((x)->bp) |
| 109 | #define PT_REGS_RC(x) ((x)->ax) |
| 110 | #define PT_REGS_SP(x) ((x)->sp) |
Naveen N. Rao | 138d615 | 2016-04-04 22:31:34 +0530 | [diff] [blame] | 111 | #define PT_REGS_IP(x) ((x)->ip) |
Michael Holzheu | d912557 | 2015-07-06 16:20:07 +0200 | [diff] [blame] | 112 | |
| 113 | #elif defined(__s390x__) |
| 114 | |
| 115 | #define PT_REGS_PARM1(x) ((x)->gprs[2]) |
| 116 | #define PT_REGS_PARM2(x) ((x)->gprs[3]) |
| 117 | #define PT_REGS_PARM3(x) ((x)->gprs[4]) |
| 118 | #define PT_REGS_PARM4(x) ((x)->gprs[5]) |
| 119 | #define PT_REGS_PARM5(x) ((x)->gprs[6]) |
| 120 | #define PT_REGS_RET(x) ((x)->gprs[14]) |
| 121 | #define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */ |
| 122 | #define PT_REGS_RC(x) ((x)->gprs[2]) |
| 123 | #define PT_REGS_SP(x) ((x)->gprs[15]) |
Michael Holzheu | 2dbb4c0 | 2016-11-28 13:48:30 +0100 | [diff] [blame] | 124 | #define PT_REGS_IP(x) ((x)->psw.addr) |
Michael Holzheu | d912557 | 2015-07-06 16:20:07 +0200 | [diff] [blame] | 125 | |
Yang Shi | 85ff8a43 | 2015-10-26 17:02:19 -0700 | [diff] [blame] | 126 | #elif defined(__aarch64__) |
| 127 | |
| 128 | #define PT_REGS_PARM1(x) ((x)->regs[0]) |
| 129 | #define PT_REGS_PARM2(x) ((x)->regs[1]) |
| 130 | #define PT_REGS_PARM3(x) ((x)->regs[2]) |
| 131 | #define PT_REGS_PARM4(x) ((x)->regs[3]) |
| 132 | #define PT_REGS_PARM5(x) ((x)->regs[4]) |
| 133 | #define PT_REGS_RET(x) ((x)->regs[30]) |
| 134 | #define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */ |
| 135 | #define PT_REGS_RC(x) ((x)->regs[0]) |
| 136 | #define PT_REGS_SP(x) ((x)->sp) |
Naveen N. Rao | 138d615 | 2016-04-04 22:31:34 +0530 | [diff] [blame] | 137 | #define PT_REGS_IP(x) ((x)->pc) |
| 138 | |
| 139 | #elif defined(__powerpc__) |
| 140 | |
| 141 | #define PT_REGS_PARM1(x) ((x)->gpr[3]) |
| 142 | #define PT_REGS_PARM2(x) ((x)->gpr[4]) |
| 143 | #define PT_REGS_PARM3(x) ((x)->gpr[5]) |
| 144 | #define PT_REGS_PARM4(x) ((x)->gpr[6]) |
| 145 | #define PT_REGS_PARM5(x) ((x)->gpr[7]) |
| 146 | #define PT_REGS_RC(x) ((x)->gpr[3]) |
| 147 | #define PT_REGS_SP(x) ((x)->sp) |
| 148 | #define PT_REGS_IP(x) ((x)->nip) |
Yang Shi | 85ff8a43 | 2015-10-26 17:02:19 -0700 | [diff] [blame] | 149 | |
David S. Miller | b0c4780 | 2017-04-22 12:31:05 -0700 | [diff] [blame] | 150 | #elif defined(__sparc__) |
| 151 | |
| 152 | #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0]) |
| 153 | #define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1]) |
| 154 | #define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2]) |
| 155 | #define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3]) |
| 156 | #define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4]) |
| 157 | #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7]) |
| 158 | #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0]) |
| 159 | #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP]) |
| 160 | #if defined(__arch64__) |
| 161 | #define PT_REGS_IP(x) ((x)->tpc) |
| 162 | #else |
| 163 | #define PT_REGS_IP(x) ((x)->pc) |
| 164 | #endif |
| 165 | |
Michael Holzheu | d912557 | 2015-07-06 16:20:07 +0200 | [diff] [blame] | 166 | #endif |
Naveen N. Rao | 138d615 | 2016-04-04 22:31:34 +0530 | [diff] [blame] | 167 | |
| 168 | #ifdef __powerpc__ |
| 169 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) |
| 170 | #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP |
David S. Miller | b0c4780 | 2017-04-22 12:31:05 -0700 | [diff] [blame] | 171 | #elif defined(__sparc__) |
| 172 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) |
| 173 | #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP |
Naveen N. Rao | 138d615 | 2016-04-04 22:31:34 +0530 | [diff] [blame] | 174 | #else |
| 175 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \ |
| 176 | bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) |
| 177 | #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \ |
| 178 | bpf_probe_read(&(ip), sizeof(ip), \ |
| 179 | (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) |
| 180 | #endif |
| 181 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 182 | #endif |