libbpf.h 5.4 KB

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