1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __BPF_HELPERS_H 3 #define __BPF_HELPERS_H 4 5 /* helper macro to place programs, maps, license in 6 * different sections in elf_bpf file. Section names 7 * are interpreted by elf_bpf loader 8 */ 9 #define SEC(NAME) __attribute__((section(NAME), used)) 10 11 /* helper functions called from eBPF programs written in C */ 12 static void *(*bpf_map_lookup_elem)(void *map, void *key) = 13 (void *) BPF_FUNC_map_lookup_elem; 14 static int (*bpf_map_update_elem)(void *map, void *key, void *value, 15 unsigned long long flags) = 16 (void *) BPF_FUNC_map_update_elem; 17 static int (*bpf_map_delete_elem)(void *map, void *key) = 18 (void *) BPF_FUNC_map_delete_elem; 19 static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) = 20 (void *) BPF_FUNC_probe_read; 21 static unsigned long long (*bpf_ktime_get_ns)(void) = 22 (void *) BPF_FUNC_ktime_get_ns; 23 static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = 24 (void *) BPF_FUNC_trace_printk; 25 static void (*bpf_tail_call)(void *ctx, void *map, int index) = 26 (void *) BPF_FUNC_tail_call; 27 static unsigned long long (*bpf_get_smp_processor_id)(void) = 28 (void *) BPF_FUNC_get_smp_processor_id; 29 static unsigned long long (*bpf_get_current_pid_tgid)(void) = 30 (void *) BPF_FUNC_get_current_pid_tgid; 31 static unsigned long long (*bpf_get_current_uid_gid)(void) = 32 (void *) BPF_FUNC_get_current_uid_gid; 33 static int (*bpf_get_current_comm)(void *buf, int buf_size) = 34 (void *) BPF_FUNC_get_current_comm; 35 static unsigned long long (*bpf_perf_event_read)(void *map, 36 unsigned long long flags) = 37 (void *) BPF_FUNC_perf_event_read; 38 static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) = 39 (void *) BPF_FUNC_clone_redirect; 40 static int (*bpf_redirect)(int ifindex, int flags) = 41 (void *) BPF_FUNC_redirect; 42 static int (*bpf_redirect_map)(void *map, int key, int flags) = 43 (void *) BPF_FUNC_redirect_map; 44 static int (*bpf_perf_event_output)(void *ctx, void *map, 45 unsigned long long flags, void *data, 46 int size) = 47 (void *) BPF_FUNC_perf_event_output; 48 static int (*bpf_get_stackid)(void *ctx, void *map, int flags) = 49 (void *) BPF_FUNC_get_stackid; 50 static int (*bpf_probe_write_user)(void *dst, void *src, int size) = 51 (void *) BPF_FUNC_probe_write_user; 52 static int (*bpf_current_task_under_cgroup)(void *map, int index) = 53 (void *) BPF_FUNC_current_task_under_cgroup; 54 static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) = 55 (void *) BPF_FUNC_skb_get_tunnel_key; 56 static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) = 57 (void *) BPF_FUNC_skb_set_tunnel_key; 58 static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) = 59 (void *) BPF_FUNC_skb_get_tunnel_opt; 60 static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) = 61 (void *) BPF_FUNC_skb_set_tunnel_opt; 62 static unsigned long long (*bpf_get_prandom_u32)(void) = 63 (void *) BPF_FUNC_get_prandom_u32; 64 static int (*bpf_xdp_adjust_head)(void *ctx, int offset) = 65 (void *) BPF_FUNC_xdp_adjust_head; 66 static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval, 67 int optlen) = 68 (void *) BPF_FUNC_setsockopt; 69 static int (*bpf_sk_redirect_map)(void *ctx, void *map, int key, int flags) = 70 (void *) BPF_FUNC_sk_redirect_map; 71 static int (*bpf_sock_map_update)(void *map, void *key, void *value, 72 unsigned long long flags) = 73 (void *) BPF_FUNC_sock_map_update; 74 75 76 /* llvm builtin functions that eBPF C program may use to 77 * emit BPF_LD_ABS and BPF_LD_IND instructions 78 */ 79 struct sk_buff; 80 unsigned long long load_byte(void *skb, 81 unsigned long long off) asm("llvm.bpf.load.byte"); 82 unsigned long long load_half(void *skb, 83 unsigned long long off) asm("llvm.bpf.load.half"); 84 unsigned long long load_word(void *skb, 85 unsigned long long off) asm("llvm.bpf.load.word"); 86 87 /* a helper structure used by eBPF C program 88 * to describe map attributes to elf_bpf loader 89 */ 90 struct bpf_map_def { 91 unsigned int type; 92 unsigned int key_size; 93 unsigned int value_size; 94 unsigned int max_entries; 95 unsigned int map_flags; 96 unsigned int inner_map_idx; 97 unsigned int numa_node; 98 }; 99 100 static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) = 101 (void *) BPF_FUNC_skb_load_bytes; 102 static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) = 103 (void *) BPF_FUNC_skb_store_bytes; 104 static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) = 105 (void *) BPF_FUNC_l3_csum_replace; 106 static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) = 107 (void *) BPF_FUNC_l4_csum_replace; 108 static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) = 109 (void *) BPF_FUNC_skb_under_cgroup; 110 static int (*bpf_skb_change_head)(void *, int len, int flags) = 111 (void *) BPF_FUNC_skb_change_head; 112 113 #if defined(__x86_64__) 114 115 #define PT_REGS_PARM1(x) ((x)->di) 116 #define PT_REGS_PARM2(x) ((x)->si) 117 #define PT_REGS_PARM3(x) ((x)->dx) 118 #define PT_REGS_PARM4(x) ((x)->cx) 119 #define PT_REGS_PARM5(x) ((x)->r8) 120 #define PT_REGS_RET(x) ((x)->sp) 121 #define PT_REGS_FP(x) ((x)->bp) 122 #define PT_REGS_RC(x) ((x)->ax) 123 #define PT_REGS_SP(x) ((x)->sp) 124 #define PT_REGS_IP(x) ((x)->ip) 125 126 #elif defined(__s390x__) 127 128 #define PT_REGS_PARM1(x) ((x)->gprs[2]) 129 #define PT_REGS_PARM2(x) ((x)->gprs[3]) 130 #define PT_REGS_PARM3(x) ((x)->gprs[4]) 131 #define PT_REGS_PARM4(x) ((x)->gprs[5]) 132 #define PT_REGS_PARM5(x) ((x)->gprs[6]) 133 #define PT_REGS_RET(x) ((x)->gprs[14]) 134 #define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */ 135 #define PT_REGS_RC(x) ((x)->gprs[2]) 136 #define PT_REGS_SP(x) ((x)->gprs[15]) 137 #define PT_REGS_IP(x) ((x)->psw.addr) 138 139 #elif defined(__aarch64__) 140 141 #define PT_REGS_PARM1(x) ((x)->regs[0]) 142 #define PT_REGS_PARM2(x) ((x)->regs[1]) 143 #define PT_REGS_PARM3(x) ((x)->regs[2]) 144 #define PT_REGS_PARM4(x) ((x)->regs[3]) 145 #define PT_REGS_PARM5(x) ((x)->regs[4]) 146 #define PT_REGS_RET(x) ((x)->regs[30]) 147 #define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */ 148 #define PT_REGS_RC(x) ((x)->regs[0]) 149 #define PT_REGS_SP(x) ((x)->sp) 150 #define PT_REGS_IP(x) ((x)->pc) 151 152 #elif defined(__mips__) 153 154 #define PT_REGS_PARM1(x) ((x)->regs[4]) 155 #define PT_REGS_PARM2(x) ((x)->regs[5]) 156 #define PT_REGS_PARM3(x) ((x)->regs[6]) 157 #define PT_REGS_PARM4(x) ((x)->regs[7]) 158 #define PT_REGS_PARM5(x) ((x)->regs[8]) 159 #define PT_REGS_RET(x) ((x)->regs[31]) 160 #define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */ 161 #define PT_REGS_RC(x) ((x)->regs[1]) 162 #define PT_REGS_SP(x) ((x)->regs[29]) 163 #define PT_REGS_IP(x) ((x)->cp0_epc) 164 165 #elif defined(__powerpc__) 166 167 #define PT_REGS_PARM1(x) ((x)->gpr[3]) 168 #define PT_REGS_PARM2(x) ((x)->gpr[4]) 169 #define PT_REGS_PARM3(x) ((x)->gpr[5]) 170 #define PT_REGS_PARM4(x) ((x)->gpr[6]) 171 #define PT_REGS_PARM5(x) ((x)->gpr[7]) 172 #define PT_REGS_RC(x) ((x)->gpr[3]) 173 #define PT_REGS_SP(x) ((x)->sp) 174 #define PT_REGS_IP(x) ((x)->nip) 175 176 #elif defined(__sparc__) 177 178 #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0]) 179 #define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1]) 180 #define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2]) 181 #define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3]) 182 #define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4]) 183 #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7]) 184 #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0]) 185 #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP]) 186 #if defined(__arch64__) 187 #define PT_REGS_IP(x) ((x)->tpc) 188 #else 189 #define PT_REGS_IP(x) ((x)->pc) 190 #endif 191 192 #endif 193 194 #ifdef __powerpc__ 195 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) 196 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 197 #elif defined(__sparc__) 198 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) 199 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 200 #else 201 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \ 202 bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) 203 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \ 204 bpf_probe_read(&(ip), sizeof(ip), \ 205 (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) 206 #endif 207 208 #endif 209