bpf_helpers.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef __BPF_HELPERS_H
  2. #define __BPF_HELPERS_H
  3. /* helper macro to place programs, maps, license in
  4. * different sections in elf_bpf file. Section names
  5. * are interpreted by elf_bpf loader
  6. */
  7. #define SEC(NAME) __attribute__((section(NAME), used))
  8. /* helper functions called from eBPF programs written in C */
  9. static void *(*bpf_map_lookup_elem)(void *map, void *key) =
  10. (void *) BPF_FUNC_map_lookup_elem;
  11. static int (*bpf_map_update_elem)(void *map, void *key, void *value,
  12. unsigned long long flags) =
  13. (void *) BPF_FUNC_map_update_elem;
  14. static int (*bpf_map_delete_elem)(void *map, void *key) =
  15. (void *) BPF_FUNC_map_delete_elem;
  16. static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) =
  17. (void *) BPF_FUNC_probe_read;
  18. static unsigned long long (*bpf_ktime_get_ns)(void) =
  19. (void *) BPF_FUNC_ktime_get_ns;
  20. static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
  21. (void *) BPF_FUNC_trace_printk;
  22. static void (*bpf_tail_call)(void *ctx, void *map, int index) =
  23. (void *) BPF_FUNC_tail_call;
  24. static unsigned long long (*bpf_get_smp_processor_id)(void) =
  25. (void *) BPF_FUNC_get_smp_processor_id;
  26. static unsigned long long (*bpf_get_current_pid_tgid)(void) =
  27. (void *) BPF_FUNC_get_current_pid_tgid;
  28. static unsigned long long (*bpf_get_current_uid_gid)(void) =
  29. (void *) BPF_FUNC_get_current_uid_gid;
  30. static int (*bpf_get_current_comm)(void *buf, int buf_size) =
  31. (void *) BPF_FUNC_get_current_comm;
  32. static int (*bpf_perf_event_read)(void *map, int index) =
  33. (void *) BPF_FUNC_perf_event_read;
  34. static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) =
  35. (void *) BPF_FUNC_clone_redirect;
  36. static int (*bpf_redirect)(int ifindex, int flags) =
  37. (void *) BPF_FUNC_redirect;
  38. /* llvm builtin functions that eBPF C program may use to
  39. * emit BPF_LD_ABS and BPF_LD_IND instructions
  40. */
  41. struct sk_buff;
  42. unsigned long long load_byte(void *skb,
  43. unsigned long long off) asm("llvm.bpf.load.byte");
  44. unsigned long long load_half(void *skb,
  45. unsigned long long off) asm("llvm.bpf.load.half");
  46. unsigned long long load_word(void *skb,
  47. unsigned long long off) asm("llvm.bpf.load.word");
  48. /* a helper structure used by eBPF C program
  49. * to describe map attributes to elf_bpf loader
  50. */
  51. struct bpf_map_def {
  52. unsigned int type;
  53. unsigned int key_size;
  54. unsigned int value_size;
  55. unsigned int max_entries;
  56. };
  57. static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
  58. (void *) BPF_FUNC_skb_store_bytes;
  59. static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
  60. (void *) BPF_FUNC_l3_csum_replace;
  61. static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
  62. (void *) BPF_FUNC_l4_csum_replace;
  63. #if defined(__x86_64__)
  64. #define PT_REGS_PARM1(x) ((x)->di)
  65. #define PT_REGS_PARM2(x) ((x)->si)
  66. #define PT_REGS_PARM3(x) ((x)->dx)
  67. #define PT_REGS_PARM4(x) ((x)->cx)
  68. #define PT_REGS_PARM5(x) ((x)->r8)
  69. #define PT_REGS_RET(x) ((x)->sp)
  70. #define PT_REGS_FP(x) ((x)->bp)
  71. #define PT_REGS_RC(x) ((x)->ax)
  72. #define PT_REGS_SP(x) ((x)->sp)
  73. #elif defined(__s390x__)
  74. #define PT_REGS_PARM1(x) ((x)->gprs[2])
  75. #define PT_REGS_PARM2(x) ((x)->gprs[3])
  76. #define PT_REGS_PARM3(x) ((x)->gprs[4])
  77. #define PT_REGS_PARM4(x) ((x)->gprs[5])
  78. #define PT_REGS_PARM5(x) ((x)->gprs[6])
  79. #define PT_REGS_RET(x) ((x)->gprs[14])
  80. #define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
  81. #define PT_REGS_RC(x) ((x)->gprs[2])
  82. #define PT_REGS_SP(x) ((x)->gprs[15])
  83. #endif
  84. #endif