bpf_helpers.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. static int (*bpf_perf_event_output)(void *ctx, void *map,
  39. unsigned long long flags, void *data,
  40. int size) =
  41. (void *) BPF_FUNC_perf_event_output;
  42. static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
  43. (void *) BPF_FUNC_get_stackid;
  44. static int (*bpf_probe_write_user)(void *dst, void *src, int size) =
  45. (void *) BPF_FUNC_probe_write_user;
  46. static int (*bpf_current_task_under_cgroup)(void *map, int index) =
  47. (void *) BPF_FUNC_current_task_under_cgroup;
  48. static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) =
  49. (void *) BPF_FUNC_skb_get_tunnel_key;
  50. static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) =
  51. (void *) BPF_FUNC_skb_set_tunnel_key;
  52. static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) =
  53. (void *) BPF_FUNC_skb_get_tunnel_opt;
  54. static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) =
  55. (void *) BPF_FUNC_skb_set_tunnel_opt;
  56. static unsigned long long (*bpf_get_prandom_u32)(void) =
  57. (void *) BPF_FUNC_get_prandom_u32;
  58. /* llvm builtin functions that eBPF C program may use to
  59. * emit BPF_LD_ABS and BPF_LD_IND instructions
  60. */
  61. struct sk_buff;
  62. unsigned long long load_byte(void *skb,
  63. unsigned long long off) asm("llvm.bpf.load.byte");
  64. unsigned long long load_half(void *skb,
  65. unsigned long long off) asm("llvm.bpf.load.half");
  66. unsigned long long load_word(void *skb,
  67. unsigned long long off) asm("llvm.bpf.load.word");
  68. /* a helper structure used by eBPF C program
  69. * to describe map attributes to elf_bpf loader
  70. */
  71. struct bpf_map_def {
  72. unsigned int type;
  73. unsigned int key_size;
  74. unsigned int value_size;
  75. unsigned int max_entries;
  76. unsigned int map_flags;
  77. };
  78. static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
  79. (void *) BPF_FUNC_skb_store_bytes;
  80. static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
  81. (void *) BPF_FUNC_l3_csum_replace;
  82. static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
  83. (void *) BPF_FUNC_l4_csum_replace;
  84. static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
  85. (void *) BPF_FUNC_skb_under_cgroup;
  86. #if defined(__x86_64__)
  87. #define PT_REGS_PARM1(x) ((x)->di)
  88. #define PT_REGS_PARM2(x) ((x)->si)
  89. #define PT_REGS_PARM3(x) ((x)->dx)
  90. #define PT_REGS_PARM4(x) ((x)->cx)
  91. #define PT_REGS_PARM5(x) ((x)->r8)
  92. #define PT_REGS_RET(x) ((x)->sp)
  93. #define PT_REGS_FP(x) ((x)->bp)
  94. #define PT_REGS_RC(x) ((x)->ax)
  95. #define PT_REGS_SP(x) ((x)->sp)
  96. #define PT_REGS_IP(x) ((x)->ip)
  97. #elif defined(__s390x__)
  98. #define PT_REGS_PARM1(x) ((x)->gprs[2])
  99. #define PT_REGS_PARM2(x) ((x)->gprs[3])
  100. #define PT_REGS_PARM3(x) ((x)->gprs[4])
  101. #define PT_REGS_PARM4(x) ((x)->gprs[5])
  102. #define PT_REGS_PARM5(x) ((x)->gprs[6])
  103. #define PT_REGS_RET(x) ((x)->gprs[14])
  104. #define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
  105. #define PT_REGS_RC(x) ((x)->gprs[2])
  106. #define PT_REGS_SP(x) ((x)->gprs[15])
  107. #define PT_REGS_IP(x) ((x)->psw.addr)
  108. #elif defined(__aarch64__)
  109. #define PT_REGS_PARM1(x) ((x)->regs[0])
  110. #define PT_REGS_PARM2(x) ((x)->regs[1])
  111. #define PT_REGS_PARM3(x) ((x)->regs[2])
  112. #define PT_REGS_PARM4(x) ((x)->regs[3])
  113. #define PT_REGS_PARM5(x) ((x)->regs[4])
  114. #define PT_REGS_RET(x) ((x)->regs[30])
  115. #define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
  116. #define PT_REGS_RC(x) ((x)->regs[0])
  117. #define PT_REGS_SP(x) ((x)->sp)
  118. #define PT_REGS_IP(x) ((x)->pc)
  119. #elif defined(__powerpc__)
  120. #define PT_REGS_PARM1(x) ((x)->gpr[3])
  121. #define PT_REGS_PARM2(x) ((x)->gpr[4])
  122. #define PT_REGS_PARM3(x) ((x)->gpr[5])
  123. #define PT_REGS_PARM4(x) ((x)->gpr[6])
  124. #define PT_REGS_PARM5(x) ((x)->gpr[7])
  125. #define PT_REGS_RC(x) ((x)->gpr[3])
  126. #define PT_REGS_SP(x) ((x)->sp)
  127. #define PT_REGS_IP(x) ((x)->nip)
  128. #endif
  129. #ifdef __powerpc__
  130. #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
  131. #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
  132. #else
  133. #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \
  134. bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
  135. #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \
  136. bpf_probe_read(&(ip), sizeof(ip), \
  137. (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
  138. #endif
  139. #endif