linux/tools/testing/selftests/bpf/bpf_helpers.h
<<
>>
Prefs
   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 */
  12static void *(*bpf_map_lookup_elem)(void *map, void *key) =
  13        (void *) BPF_FUNC_map_lookup_elem;
  14static int (*bpf_map_update_elem)(void *map, void *key, void *value,
  15                                  unsigned long long flags) =
  16        (void *) BPF_FUNC_map_update_elem;
  17static int (*bpf_map_delete_elem)(void *map, void *key) =
  18        (void *) BPF_FUNC_map_delete_elem;
  19static int (*bpf_map_push_elem)(void *map, void *value,
  20                                unsigned long long flags) =
  21        (void *) BPF_FUNC_map_push_elem;
  22static int (*bpf_map_pop_elem)(void *map, void *value) =
  23        (void *) BPF_FUNC_map_pop_elem;
  24static int (*bpf_map_peek_elem)(void *map, void *value) =
  25        (void *) BPF_FUNC_map_peek_elem;
  26static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) =
  27        (void *) BPF_FUNC_probe_read;
  28static unsigned long long (*bpf_ktime_get_ns)(void) =
  29        (void *) BPF_FUNC_ktime_get_ns;
  30static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
  31        (void *) BPF_FUNC_trace_printk;
  32static void (*bpf_tail_call)(void *ctx, void *map, int index) =
  33        (void *) BPF_FUNC_tail_call;
  34static unsigned long long (*bpf_get_smp_processor_id)(void) =
  35        (void *) BPF_FUNC_get_smp_processor_id;
  36static unsigned long long (*bpf_get_current_pid_tgid)(void) =
  37        (void *) BPF_FUNC_get_current_pid_tgid;
  38static unsigned long long (*bpf_get_current_uid_gid)(void) =
  39        (void *) BPF_FUNC_get_current_uid_gid;
  40static int (*bpf_get_current_comm)(void *buf, int buf_size) =
  41        (void *) BPF_FUNC_get_current_comm;
  42static unsigned long long (*bpf_perf_event_read)(void *map,
  43                                                 unsigned long long flags) =
  44        (void *) BPF_FUNC_perf_event_read;
  45static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) =
  46        (void *) BPF_FUNC_clone_redirect;
  47static int (*bpf_redirect)(int ifindex, int flags) =
  48        (void *) BPF_FUNC_redirect;
  49static int (*bpf_redirect_map)(void *map, int key, int flags) =
  50        (void *) BPF_FUNC_redirect_map;
  51static int (*bpf_perf_event_output)(void *ctx, void *map,
  52                                    unsigned long long flags, void *data,
  53                                    int size) =
  54        (void *) BPF_FUNC_perf_event_output;
  55static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
  56        (void *) BPF_FUNC_get_stackid;
  57static int (*bpf_probe_write_user)(void *dst, void *src, int size) =
  58        (void *) BPF_FUNC_probe_write_user;
  59static int (*bpf_current_task_under_cgroup)(void *map, int index) =
  60        (void *) BPF_FUNC_current_task_under_cgroup;
  61static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) =
  62        (void *) BPF_FUNC_skb_get_tunnel_key;
  63static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) =
  64        (void *) BPF_FUNC_skb_set_tunnel_key;
  65static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) =
  66        (void *) BPF_FUNC_skb_get_tunnel_opt;
  67static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) =
  68        (void *) BPF_FUNC_skb_set_tunnel_opt;
  69static unsigned long long (*bpf_get_prandom_u32)(void) =
  70        (void *) BPF_FUNC_get_prandom_u32;
  71static int (*bpf_xdp_adjust_head)(void *ctx, int offset) =
  72        (void *) BPF_FUNC_xdp_adjust_head;
  73static int (*bpf_xdp_adjust_meta)(void *ctx, int offset) =
  74        (void *) BPF_FUNC_xdp_adjust_meta;
  75static int (*bpf_get_socket_cookie)(void *ctx) =
  76        (void *) BPF_FUNC_get_socket_cookie;
  77static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval,
  78                             int optlen) =
  79        (void *) BPF_FUNC_setsockopt;
  80static int (*bpf_getsockopt)(void *ctx, int level, int optname, void *optval,
  81                             int optlen) =
  82        (void *) BPF_FUNC_getsockopt;
  83static int (*bpf_sock_ops_cb_flags_set)(void *ctx, int flags) =
  84        (void *) BPF_FUNC_sock_ops_cb_flags_set;
  85static int (*bpf_sk_redirect_map)(void *ctx, void *map, int key, int flags) =
  86        (void *) BPF_FUNC_sk_redirect_map;
  87static int (*bpf_sk_redirect_hash)(void *ctx, void *map, void *key, int flags) =
  88        (void *) BPF_FUNC_sk_redirect_hash;
  89static int (*bpf_sock_map_update)(void *map, void *key, void *value,
  90                                  unsigned long long flags) =
  91        (void *) BPF_FUNC_sock_map_update;
  92static int (*bpf_sock_hash_update)(void *map, void *key, void *value,
  93                                   unsigned long long flags) =
  94        (void *) BPF_FUNC_sock_hash_update;
  95static int (*bpf_perf_event_read_value)(void *map, unsigned long long flags,
  96                                        void *buf, unsigned int buf_size) =
  97        (void *) BPF_FUNC_perf_event_read_value;
  98static int (*bpf_perf_prog_read_value)(void *ctx, void *buf,
  99                                       unsigned int buf_size) =
 100        (void *) BPF_FUNC_perf_prog_read_value;
 101static int (*bpf_override_return)(void *ctx, unsigned long rc) =
 102        (void *) BPF_FUNC_override_return;
 103static int (*bpf_msg_redirect_map)(void *ctx, void *map, int key, int flags) =
 104        (void *) BPF_FUNC_msg_redirect_map;
 105static int (*bpf_msg_redirect_hash)(void *ctx,
 106                                    void *map, void *key, int flags) =
 107        (void *) BPF_FUNC_msg_redirect_hash;
 108static int (*bpf_msg_apply_bytes)(void *ctx, int len) =
 109        (void *) BPF_FUNC_msg_apply_bytes;
 110static int (*bpf_msg_cork_bytes)(void *ctx, int len) =
 111        (void *) BPF_FUNC_msg_cork_bytes;
 112static int (*bpf_msg_pull_data)(void *ctx, int start, int end, int flags) =
 113        (void *) BPF_FUNC_msg_pull_data;
 114static int (*bpf_msg_push_data)(void *ctx, int start, int end, int flags) =
 115        (void *) BPF_FUNC_msg_push_data;
 116static int (*bpf_msg_pop_data)(void *ctx, int start, int cut, int flags) =
 117        (void *) BPF_FUNC_msg_pop_data;
 118static int (*bpf_bind)(void *ctx, void *addr, int addr_len) =
 119        (void *) BPF_FUNC_bind;
 120static int (*bpf_xdp_adjust_tail)(void *ctx, int offset) =
 121        (void *) BPF_FUNC_xdp_adjust_tail;
 122static int (*bpf_skb_get_xfrm_state)(void *ctx, int index, void *state,
 123                                     int size, int flags) =
 124        (void *) BPF_FUNC_skb_get_xfrm_state;
 125static int (*bpf_sk_select_reuseport)(void *ctx, void *map, void *key, __u32 flags) =
 126        (void *) BPF_FUNC_sk_select_reuseport;
 127static int (*bpf_get_stack)(void *ctx, void *buf, int size, int flags) =
 128        (void *) BPF_FUNC_get_stack;
 129static int (*bpf_fib_lookup)(void *ctx, struct bpf_fib_lookup *params,
 130                             int plen, __u32 flags) =
 131        (void *) BPF_FUNC_fib_lookup;
 132static int (*bpf_lwt_push_encap)(void *ctx, unsigned int type, void *hdr,
 133                                 unsigned int len) =
 134        (void *) BPF_FUNC_lwt_push_encap;
 135static int (*bpf_lwt_seg6_store_bytes)(void *ctx, unsigned int offset,
 136                                       void *from, unsigned int len) =
 137        (void *) BPF_FUNC_lwt_seg6_store_bytes;
 138static int (*bpf_lwt_seg6_action)(void *ctx, unsigned int action, void *param,
 139                                  unsigned int param_len) =
 140        (void *) BPF_FUNC_lwt_seg6_action;
 141static int (*bpf_lwt_seg6_adjust_srh)(void *ctx, unsigned int offset,
 142                                      unsigned int len) =
 143        (void *) BPF_FUNC_lwt_seg6_adjust_srh;
 144static int (*bpf_rc_repeat)(void *ctx) =
 145        (void *) BPF_FUNC_rc_repeat;
 146static int (*bpf_rc_keydown)(void *ctx, unsigned int protocol,
 147                             unsigned long long scancode, unsigned int toggle) =
 148        (void *) BPF_FUNC_rc_keydown;
 149static unsigned long long (*bpf_get_current_cgroup_id)(void) =
 150        (void *) BPF_FUNC_get_current_cgroup_id;
 151static void *(*bpf_get_local_storage)(void *map, unsigned long long flags) =
 152        (void *) BPF_FUNC_get_local_storage;
 153static unsigned long long (*bpf_skb_cgroup_id)(void *ctx) =
 154        (void *) BPF_FUNC_skb_cgroup_id;
 155static unsigned long long (*bpf_skb_ancestor_cgroup_id)(void *ctx, int level) =
 156        (void *) BPF_FUNC_skb_ancestor_cgroup_id;
 157static struct bpf_sock *(*bpf_sk_lookup_tcp)(void *ctx,
 158                                             struct bpf_sock_tuple *tuple,
 159                                             int size, unsigned long long netns_id,
 160                                             unsigned long long flags) =
 161        (void *) BPF_FUNC_sk_lookup_tcp;
 162static struct bpf_sock *(*bpf_sk_lookup_udp)(void *ctx,
 163                                             struct bpf_sock_tuple *tuple,
 164                                             int size, unsigned long long netns_id,
 165                                             unsigned long long flags) =
 166        (void *) BPF_FUNC_sk_lookup_udp;
 167static int (*bpf_sk_release)(struct bpf_sock *sk) =
 168        (void *) BPF_FUNC_sk_release;
 169static int (*bpf_skb_vlan_push)(void *ctx, __be16 vlan_proto, __u16 vlan_tci) =
 170        (void *) BPF_FUNC_skb_vlan_push;
 171static int (*bpf_skb_vlan_pop)(void *ctx) =
 172        (void *) BPF_FUNC_skb_vlan_pop;
 173static int (*bpf_rc_pointer_rel)(void *ctx, int rel_x, int rel_y) =
 174        (void *) BPF_FUNC_rc_pointer_rel;
 175static void (*bpf_spin_lock)(struct bpf_spin_lock *lock) =
 176        (void *) BPF_FUNC_spin_lock;
 177static void (*bpf_spin_unlock)(struct bpf_spin_lock *lock) =
 178        (void *) BPF_FUNC_spin_unlock;
 179static struct bpf_sock *(*bpf_sk_fullsock)(struct bpf_sock *sk) =
 180        (void *) BPF_FUNC_sk_fullsock;
 181static struct bpf_tcp_sock *(*bpf_tcp_sock)(struct bpf_sock *sk) =
 182        (void *) BPF_FUNC_tcp_sock;
 183static struct bpf_sock *(*bpf_get_listener_sock)(struct bpf_sock *sk) =
 184        (void *) BPF_FUNC_get_listener_sock;
 185static int (*bpf_skb_ecn_set_ce)(void *ctx) =
 186        (void *) BPF_FUNC_skb_ecn_set_ce;
 187
 188/* llvm builtin functions that eBPF C program may use to
 189 * emit BPF_LD_ABS and BPF_LD_IND instructions
 190 */
 191struct sk_buff;
 192unsigned long long load_byte(void *skb,
 193                             unsigned long long off) asm("llvm.bpf.load.byte");
 194unsigned long long load_half(void *skb,
 195                             unsigned long long off) asm("llvm.bpf.load.half");
 196unsigned long long load_word(void *skb,
 197                             unsigned long long off) asm("llvm.bpf.load.word");
 198
 199/* a helper structure used by eBPF C program
 200 * to describe map attributes to elf_bpf loader
 201 */
 202struct bpf_map_def {
 203        unsigned int type;
 204        unsigned int key_size;
 205        unsigned int value_size;
 206        unsigned int max_entries;
 207        unsigned int map_flags;
 208        unsigned int inner_map_idx;
 209        unsigned int numa_node;
 210};
 211
 212#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val)          \
 213        struct ____btf_map_##name {                             \
 214                type_key key;                                   \
 215                type_val value;                                 \
 216        };                                                      \
 217        struct ____btf_map_##name                               \
 218        __attribute__ ((section(".maps." #name), used))         \
 219                ____btf_map_##name = { }
 220
 221static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) =
 222        (void *) BPF_FUNC_skb_load_bytes;
 223static int (*bpf_skb_load_bytes_relative)(void *ctx, int off, void *to, int len, __u32 start_header) =
 224        (void *) BPF_FUNC_skb_load_bytes_relative;
 225static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
 226        (void *) BPF_FUNC_skb_store_bytes;
 227static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
 228        (void *) BPF_FUNC_l3_csum_replace;
 229static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
 230        (void *) BPF_FUNC_l4_csum_replace;
 231static int (*bpf_csum_diff)(void *from, int from_size, void *to, int to_size, int seed) =
 232        (void *) BPF_FUNC_csum_diff;
 233static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
 234        (void *) BPF_FUNC_skb_under_cgroup;
 235static int (*bpf_skb_change_head)(void *, int len, int flags) =
 236        (void *) BPF_FUNC_skb_change_head;
 237static int (*bpf_skb_pull_data)(void *, int len) =
 238        (void *) BPF_FUNC_skb_pull_data;
 239static unsigned int (*bpf_get_cgroup_classid)(void *ctx) =
 240        (void *) BPF_FUNC_get_cgroup_classid;
 241static unsigned int (*bpf_get_route_realm)(void *ctx) =
 242        (void *) BPF_FUNC_get_route_realm;
 243static int (*bpf_skb_change_proto)(void *ctx, __be16 proto, __u64 flags) =
 244        (void *) BPF_FUNC_skb_change_proto;
 245static int (*bpf_skb_change_type)(void *ctx, __u32 type) =
 246        (void *) BPF_FUNC_skb_change_type;
 247static unsigned int (*bpf_get_hash_recalc)(void *ctx) =
 248        (void *) BPF_FUNC_get_hash_recalc;
 249static unsigned long long (*bpf_get_current_task)(void *ctx) =
 250        (void *) BPF_FUNC_get_current_task;
 251static int (*bpf_skb_change_tail)(void *ctx, __u32 len, __u64 flags) =
 252        (void *) BPF_FUNC_skb_change_tail;
 253static long long (*bpf_csum_update)(void *ctx, __u32 csum) =
 254        (void *) BPF_FUNC_csum_update;
 255static void (*bpf_set_hash_invalid)(void *ctx) =
 256        (void *) BPF_FUNC_set_hash_invalid;
 257static int (*bpf_get_numa_node_id)(void) =
 258        (void *) BPF_FUNC_get_numa_node_id;
 259static int (*bpf_probe_read_str)(void *ctx, __u32 size,
 260                                 const void *unsafe_ptr) =
 261        (void *) BPF_FUNC_probe_read_str;
 262static unsigned int (*bpf_get_socket_uid)(void *ctx) =
 263        (void *) BPF_FUNC_get_socket_uid;
 264static unsigned int (*bpf_set_hash)(void *ctx, __u32 hash) =
 265        (void *) BPF_FUNC_set_hash;
 266static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
 267                                  unsigned long long flags) =
 268        (void *) BPF_FUNC_skb_adjust_room;
 269
 270/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
 271#if defined(__TARGET_ARCH_x86)
 272        #define bpf_target_x86
 273        #define bpf_target_defined
 274#elif defined(__TARGET_ARCH_s930x)
 275        #define bpf_target_s930x
 276        #define bpf_target_defined
 277#elif defined(__TARGET_ARCH_arm64)
 278        #define bpf_target_arm64
 279        #define bpf_target_defined
 280#elif defined(__TARGET_ARCH_mips)
 281        #define bpf_target_mips
 282        #define bpf_target_defined
 283#elif defined(__TARGET_ARCH_powerpc)
 284        #define bpf_target_powerpc
 285        #define bpf_target_defined
 286#elif defined(__TARGET_ARCH_sparc)
 287        #define bpf_target_sparc
 288        #define bpf_target_defined
 289#else
 290        #undef bpf_target_defined
 291#endif
 292
 293/* Fall back to what the compiler says */
 294#ifndef bpf_target_defined
 295#if defined(__x86_64__)
 296        #define bpf_target_x86
 297#elif defined(__s390x__)
 298        #define bpf_target_s930x
 299#elif defined(__aarch64__)
 300        #define bpf_target_arm64
 301#elif defined(__mips__)
 302        #define bpf_target_mips
 303#elif defined(__powerpc__)
 304        #define bpf_target_powerpc
 305#elif defined(__sparc__)
 306        #define bpf_target_sparc
 307#endif
 308#endif
 309
 310#if defined(bpf_target_x86)
 311
 312#define PT_REGS_PARM1(x) ((x)->di)
 313#define PT_REGS_PARM2(x) ((x)->si)
 314#define PT_REGS_PARM3(x) ((x)->dx)
 315#define PT_REGS_PARM4(x) ((x)->cx)
 316#define PT_REGS_PARM5(x) ((x)->r8)
 317#define PT_REGS_RET(x) ((x)->sp)
 318#define PT_REGS_FP(x) ((x)->bp)
 319#define PT_REGS_RC(x) ((x)->ax)
 320#define PT_REGS_SP(x) ((x)->sp)
 321#define PT_REGS_IP(x) ((x)->ip)
 322
 323#elif defined(bpf_target_s390x)
 324
 325#define PT_REGS_PARM1(x) ((x)->gprs[2])
 326#define PT_REGS_PARM2(x) ((x)->gprs[3])
 327#define PT_REGS_PARM3(x) ((x)->gprs[4])
 328#define PT_REGS_PARM4(x) ((x)->gprs[5])
 329#define PT_REGS_PARM5(x) ((x)->gprs[6])
 330#define PT_REGS_RET(x) ((x)->gprs[14])
 331#define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
 332#define PT_REGS_RC(x) ((x)->gprs[2])
 333#define PT_REGS_SP(x) ((x)->gprs[15])
 334#define PT_REGS_IP(x) ((x)->psw.addr)
 335
 336#elif defined(bpf_target_arm64)
 337
 338#define PT_REGS_PARM1(x) ((x)->regs[0])
 339#define PT_REGS_PARM2(x) ((x)->regs[1])
 340#define PT_REGS_PARM3(x) ((x)->regs[2])
 341#define PT_REGS_PARM4(x) ((x)->regs[3])
 342#define PT_REGS_PARM5(x) ((x)->regs[4])
 343#define PT_REGS_RET(x) ((x)->regs[30])
 344#define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
 345#define PT_REGS_RC(x) ((x)->regs[0])
 346#define PT_REGS_SP(x) ((x)->sp)
 347#define PT_REGS_IP(x) ((x)->pc)
 348
 349#elif defined(bpf_target_mips)
 350
 351#define PT_REGS_PARM1(x) ((x)->regs[4])
 352#define PT_REGS_PARM2(x) ((x)->regs[5])
 353#define PT_REGS_PARM3(x) ((x)->regs[6])
 354#define PT_REGS_PARM4(x) ((x)->regs[7])
 355#define PT_REGS_PARM5(x) ((x)->regs[8])
 356#define PT_REGS_RET(x) ((x)->regs[31])
 357#define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */
 358#define PT_REGS_RC(x) ((x)->regs[1])
 359#define PT_REGS_SP(x) ((x)->regs[29])
 360#define PT_REGS_IP(x) ((x)->cp0_epc)
 361
 362#elif defined(bpf_target_powerpc)
 363
 364#define PT_REGS_PARM1(x) ((x)->gpr[3])
 365#define PT_REGS_PARM2(x) ((x)->gpr[4])
 366#define PT_REGS_PARM3(x) ((x)->gpr[5])
 367#define PT_REGS_PARM4(x) ((x)->gpr[6])
 368#define PT_REGS_PARM5(x) ((x)->gpr[7])
 369#define PT_REGS_RC(x) ((x)->gpr[3])
 370#define PT_REGS_SP(x) ((x)->sp)
 371#define PT_REGS_IP(x) ((x)->nip)
 372
 373#elif defined(bpf_target_sparc)
 374
 375#define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
 376#define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1])
 377#define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2])
 378#define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3])
 379#define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4])
 380#define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
 381#define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
 382#define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
 383
 384/* Should this also be a bpf_target check for the sparc case? */
 385#if defined(__arch64__)
 386#define PT_REGS_IP(x) ((x)->tpc)
 387#else
 388#define PT_REGS_IP(x) ((x)->pc)
 389#endif
 390
 391#endif
 392
 393#ifdef bpf_target_powerpc
 394#define BPF_KPROBE_READ_RET_IP(ip, ctx)         ({ (ip) = (ctx)->link; })
 395#define BPF_KRETPROBE_READ_RET_IP               BPF_KPROBE_READ_RET_IP
 396#elif bpf_target_sparc
 397#define BPF_KPROBE_READ_RET_IP(ip, ctx)         ({ (ip) = PT_REGS_RET(ctx); })
 398#define BPF_KRETPROBE_READ_RET_IP               BPF_KPROBE_READ_RET_IP
 399#else
 400#define BPF_KPROBE_READ_RET_IP(ip, ctx)         ({                              \
 401                bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
 402#define BPF_KRETPROBE_READ_RET_IP(ip, ctx)      ({                              \
 403                bpf_probe_read(&(ip), sizeof(ip),                               \
 404                                (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
 405#endif
 406
 407#endif
 408