libbpf.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* eBPF mini library */
  2. #ifndef __LIBBPF_H
  3. #define __LIBBPF_H
  4. struct bpf_insn;
  5. int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
  6. int max_entries, int map_flags);
  7. int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
  8. int bpf_lookup_elem(int fd, void *key, void *value);
  9. int bpf_delete_elem(int fd, void *key);
  10. int bpf_get_next_key(int fd, void *key, void *next_key);
  11. int bpf_prog_load(enum bpf_prog_type prog_type,
  12. const struct bpf_insn *insns, int insn_len,
  13. const char *license, int kern_version);
  14. int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
  15. int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
  16. int bpf_obj_pin(int fd, const char *pathname);
  17. int bpf_obj_get(const char *pathname);
  18. #define LOG_BUF_SIZE (256 * 1024)
  19. extern char bpf_log_buf[LOG_BUF_SIZE];
  20. /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
  21. #define BPF_ALU64_REG(OP, DST, SRC) \
  22. ((struct bpf_insn) { \
  23. .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
  24. .dst_reg = DST, \
  25. .src_reg = SRC, \
  26. .off = 0, \
  27. .imm = 0 })
  28. #define BPF_ALU32_REG(OP, DST, SRC) \
  29. ((struct bpf_insn) { \
  30. .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
  31. .dst_reg = DST, \
  32. .src_reg = SRC, \
  33. .off = 0, \
  34. .imm = 0 })
  35. /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
  36. #define BPF_ALU64_IMM(OP, DST, IMM) \
  37. ((struct bpf_insn) { \
  38. .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
  39. .dst_reg = DST, \
  40. .src_reg = 0, \
  41. .off = 0, \
  42. .imm = IMM })
  43. #define BPF_ALU32_IMM(OP, DST, IMM) \
  44. ((struct bpf_insn) { \
  45. .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
  46. .dst_reg = DST, \
  47. .src_reg = 0, \
  48. .off = 0, \
  49. .imm = IMM })
  50. /* Short form of mov, dst_reg = src_reg */
  51. #define BPF_MOV64_REG(DST, SRC) \
  52. ((struct bpf_insn) { \
  53. .code = BPF_ALU64 | BPF_MOV | BPF_X, \
  54. .dst_reg = DST, \
  55. .src_reg = SRC, \
  56. .off = 0, \
  57. .imm = 0 })
  58. #define BPF_MOV32_REG(DST, SRC) \
  59. ((struct bpf_insn) { \
  60. .code = BPF_ALU | BPF_MOV | BPF_X, \
  61. .dst_reg = DST, \
  62. .src_reg = SRC, \
  63. .off = 0, \
  64. .imm = 0 })
  65. /* Short form of mov, dst_reg = imm32 */
  66. #define BPF_MOV64_IMM(DST, IMM) \
  67. ((struct bpf_insn) { \
  68. .code = BPF_ALU64 | BPF_MOV | BPF_K, \
  69. .dst_reg = DST, \
  70. .src_reg = 0, \
  71. .off = 0, \
  72. .imm = IMM })
  73. #define BPF_MOV32_IMM(DST, IMM) \
  74. ((struct bpf_insn) { \
  75. .code = BPF_ALU | BPF_MOV | BPF_K, \
  76. .dst_reg = DST, \
  77. .src_reg = 0, \
  78. .off = 0, \
  79. .imm = IMM })
  80. /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
  81. #define BPF_LD_IMM64(DST, IMM) \
  82. BPF_LD_IMM64_RAW(DST, 0, IMM)
  83. #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
  84. ((struct bpf_insn) { \
  85. .code = BPF_LD | BPF_DW | BPF_IMM, \
  86. .dst_reg = DST, \
  87. .src_reg = SRC, \
  88. .off = 0, \
  89. .imm = (__u32) (IMM) }), \
  90. ((struct bpf_insn) { \
  91. .code = 0, /* zero is reserved opcode */ \
  92. .dst_reg = 0, \
  93. .src_reg = 0, \
  94. .off = 0, \
  95. .imm = ((__u64) (IMM)) >> 32 })
  96. #ifndef BPF_PSEUDO_MAP_FD
  97. # define BPF_PSEUDO_MAP_FD 1
  98. #endif
  99. /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
  100. #define BPF_LD_MAP_FD(DST, MAP_FD) \
  101. BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
  102. /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
  103. #define BPF_LD_ABS(SIZE, IMM) \
  104. ((struct bpf_insn) { \
  105. .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
  106. .dst_reg = 0, \
  107. .src_reg = 0, \
  108. .off = 0, \
  109. .imm = IMM })
  110. /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
  111. #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
  112. ((struct bpf_insn) { \
  113. .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
  114. .dst_reg = DST, \
  115. .src_reg = SRC, \
  116. .off = OFF, \
  117. .imm = 0 })
  118. /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
  119. #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
  120. ((struct bpf_insn) { \
  121. .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
  122. .dst_reg = DST, \
  123. .src_reg = SRC, \
  124. .off = OFF, \
  125. .imm = 0 })
  126. /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
  127. #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
  128. ((struct bpf_insn) { \
  129. .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
  130. .dst_reg = DST, \
  131. .src_reg = 0, \
  132. .off = OFF, \
  133. .imm = IMM })
  134. /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
  135. #define BPF_JMP_REG(OP, DST, SRC, OFF) \
  136. ((struct bpf_insn) { \
  137. .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
  138. .dst_reg = DST, \
  139. .src_reg = SRC, \
  140. .off = OFF, \
  141. .imm = 0 })
  142. /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
  143. #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
  144. ((struct bpf_insn) { \
  145. .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
  146. .dst_reg = DST, \
  147. .src_reg = 0, \
  148. .off = OFF, \
  149. .imm = IMM })
  150. /* Raw code statement block */
  151. #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
  152. ((struct bpf_insn) { \
  153. .code = CODE, \
  154. .dst_reg = DST, \
  155. .src_reg = SRC, \
  156. .off = OFF, \
  157. .imm = IMM })
  158. /* Program exit */
  159. #define BPF_EXIT_INSN() \
  160. ((struct bpf_insn) { \
  161. .code = BPF_JMP | BPF_EXIT, \
  162. .dst_reg = 0, \
  163. .src_reg = 0, \
  164. .off = 0, \
  165. .imm = 0 })
  166. /* create RAW socket and bind to interface 'name' */
  167. int open_raw_sock(const char *name);
  168. struct perf_event_attr;
  169. int perf_event_open(struct perf_event_attr *attr, int pid, int cpu,
  170. int group_fd, unsigned long flags);
  171. #endif