socket_trace.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #ifndef DF_BPF_SOCKET_TRACE_H
  2. #define DF_BPF_SOCKET_TRACE_H
  3. #include "bpf_base.h"
  4. #include <linux/version.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <stdbool.h>
  8. #include <errno.h>
  9. #include <stddef.h>
  10. #include "utils.h"
  11. #include "common.h"
  12. #include "kernel.h"
  13. #include "bpf_endian.h"
  14. #ifndef unlikely
  15. #define unlikely(x) __builtin_expect(!!(x), 0)
  16. #endif
  17. #ifndef likely
  18. #define likely(x) __builtin_expect(!!(x), 1)
  19. #endif
  20. #include <sys/socket.h>
  21. #include <stddef.h>
  22. #include <netinet/in.h>
  23. //typedef long unsigned int __kernel_size_t;
  24. enum {
  25. TCPF_ESTABLISHED = (1 << 1),
  26. TCPF_SYN_SENT = (1 << 2),
  27. TCPF_SYN_RECV = (1 << 3),
  28. TCPF_FIN_WAIT1 = (1 << 4),
  29. TCPF_FIN_WAIT2 = (1 << 5),
  30. TCPF_TIME_WAIT = (1 << 6),
  31. TCPF_CLOSE = (1 << 7),
  32. TCPF_CLOSE_WAIT = (1 << 8),
  33. TCPF_LAST_ACK = (1 << 9),
  34. TCPF_LISTEN = (1 << 10),
  35. TCPF_CLOSING = (1 << 11)
  36. };
  37. struct user_msghdr {
  38. void *msg_name;
  39. int msg_namelen;
  40. struct iovec *msg_iov;
  41. __kernel_size_t msg_iovlen;
  42. void *msg_control;
  43. __kernel_size_t msg_controllen;
  44. unsigned int msg_flags;
  45. };
  46. struct mmsghdr {
  47. struct user_msghdr msg_hdr;
  48. unsigned int msg_len;
  49. };
  50. #define CONN_ADD 0
  51. #define CONN_DEL 1
  52. #define SOCK_DIR_SND_REQ 0
  53. #define SOCK_DIR_SND_RES 1
  54. #define SOCK_DIR_RCV_REQ 2
  55. #define SOCK_DIR_RCV_RES 3
  56. #define SOCK_ADD_EVENT 4
  57. #define SOCK_INFO_EVENT 5
  58. #define HTTP_REQUEST_MIN_LEN 7
  59. #define HTTP_CODE_MSG_LEN 16
  60. #define AF_UNKNOWN 0xff
  61. #define SOCK_CHECK_TYPE_ERROR 0
  62. #define SOCK_CHECK_TYPE_UDP 1
  63. #define SOCK_CHECK_TYPE_TCP_ES 2
  64. #ifndef TASK_COMM_LEN
  65. #define TASK_COMM_LEN 16
  66. #endif
  67. #include "socket_trace_common.h"
  68. struct member_fields_offset {
  69. __u32 ready;
  70. __u32 task__files_offset;
  71. __u32 sock__flags_offset;
  72. __u32 tcp_sock__copied_seq_offset;
  73. __u32 tcp_sock__write_seq_offset;
  74. __u32 struct_files_struct_fdt_offset; // offsetof(struct files_struct, fdt)
  75. __u32 struct_files_private_data_offset; // offsetof(struct file, private_data)
  76. __u32 struct_file_f_inode_offset; // offsetof(struct file, f_inode)
  77. __u32 struct_inode_i_mode_offset; // offsetof(struct inode, i_mode)
  78. __u32 struct_file_dentry_offset; // offsetof(struct file, f_path) + offsetof(struct path, dentry)
  79. __u32 struct_dentry_name_offset; // offsetof(struct dentry, d_name) + offsetof(struct qstr, name)
  80. __u32 struct_sock_family_offset; // offsetof(struct sock_common, skc_family)
  81. __u32 struct_sock_saddr_offset; // offsetof(struct sock_common, skc_rcv_saddr)
  82. __u32 struct_sock_daddr_offset; // offsetof(struct sock_common, skc_daddr)
  83. __u32 struct_sock_ip6saddr_offset; // offsetof(struct sock_common, skc_v6_rcv_saddr)
  84. __u32 struct_sock_ip6daddr_offset; // offsetof(struct sock_common, skc_v6_daddr)
  85. __u32 struct_sock_dport_offset; // offsetof(struct sock_common, skc_dport)
  86. __u32 struct_sock_sport_offset; // offsetof(struct sock_common, skc_num)
  87. __u32 struct_sock_skc_state_offset; // offsetof(struct sock_common, skc_state)
  88. __u32 struct_sock_common_ipv6only_offset; // offsetof(struct sock_common, skc_flags)
  89. };
  90. /********************************************************/
  91. // socket trace struct
  92. /********************************************************/
  93. #define socklen_t size_t
  94. union sockaddr_t {
  95. struct sockaddr sa;
  96. struct sockaddr_in in4;
  97. struct sockaddr_in6 in6;
  98. };
  99. struct conn_info_t {
  100. #ifdef PROBE_CONN
  101. __u64 id;
  102. #endif
  103. struct __tuple_t tuple;
  104. __u16 skc_family; /* PF_INET, PF_INET6... */
  105. __u16 sk_type; /* socket type (SOCK_STREAM, etc) */
  106. __u8 skc_ipv6only : 1;
  107. __u8 infer_reliable : 1; // Is protocol inference reliable?
  108. __u8 padding : 6;
  109. bool need_reconfirm; // socket l7协议类型是否需要再次确认。
  110. bool keep_data_seq; // 保持捕获数据的序列号不变为true,否则为false。
  111. __u32 fd;
  112. void *sk;
  113. // The protocol of traffic on the connection (HTTP, MySQL, etc.).
  114. enum traffic_protocol protocol;
  115. // MSG_UNKNOWN, MSG_REQUEST, MSG_RESPONSE
  116. enum message_type message_type;
  117. enum traffic_direction direction; //T_INGRESS or T_EGRESS
  118. enum endpoint_role role;
  119. size_t prev_count;
  120. char prev_buf[4];
  121. __s32 correlation_id; // 目前用于kafka判断
  122. enum traffic_direction prev_direction;
  123. struct socket_info_t *socket_info_ptr; /* lookup __socket_info_map */
  124. /*
  125. The matching logic is:
  126. DNS 1 req ---->
  127. DNS 1 res <-------
  128. DNS 2 req ----> ​
  129. DNS 2 res <-------
  130. and now it is
  131. DNS 1 req ---->
  132. DNS 2 req ---->
  133. DNS 1 res <-------
  134. DNS 2 res <-------
  135. Such a scene affects the whole tracking
  136. DNS 1 req is IPV6, DNS 2 req is IPV4
  137. */
  138. // FIXME: Remove this field when the call chain can correctly handle
  139. // the Go DNS case. Parse DNS save record type and ignore AAAA records
  140. // in call chain trace
  141. __u16 dns_q_type;
  142. };
  143. enum process_data_extra_source {
  144. DATA_SOURCE_SYSCALL,
  145. DATA_SOURCE_GO_TLS_UPROBE,
  146. DATA_SOURCE_GO_HTTP2_UPROBE,
  147. DATA_SOURCE_OPENSSL_UPROBE,
  148. DATA_SOURCE_IO_EVENT,
  149. };
  150. struct process_data_extra {
  151. bool vecs : 1;
  152. bool is_go_process : 1;
  153. enum process_data_extra_source source;
  154. enum traffic_protocol protocol;
  155. __u64 coroutine_id;
  156. enum traffic_direction direction;
  157. enum message_type message_type;
  158. } __attribute__ ((packed));
  159. /*
  160. * BPF Tail Calls context
  161. */
  162. struct tail_calls_context {
  163. int max_size_limit; // The maximum size of the socket data that can be transferred.
  164. enum traffic_direction dir; // Data flow direction.
  165. bool vecs; // Whether a memory vector is used ? (for specific syscall)
  166. struct conn_info_t conn_info;
  167. struct process_data_extra extra;
  168. __u32 bytes_count;
  169. struct member_fields_offset *offset;
  170. };
  171. enum syscall_src_func {
  172. SYSCALL_FUNC_UNKNOWN,
  173. SYSCALL_FUNC_WRITE,
  174. SYSCALL_FUNC_READ,
  175. SYSCALL_FUNC_SEND,
  176. SYSCALL_FUNC_RECV,
  177. SYSCALL_FUNC_SENDTO,
  178. SYSCALL_FUNC_RECVFROM,
  179. SYSCALL_FUNC_SENDMSG,
  180. SYSCALL_FUNC_RECVMSG,
  181. SYSCALL_FUNC_SENDMMSG,
  182. SYSCALL_FUNC_RECVMMSG,
  183. SYSCALL_FUNC_WRITEV,
  184. SYSCALL_FUNC_READV,
  185. SYSCALL_FUNC_SENDFILE
  186. };
  187. struct data_args_t {
  188. // Represents the function from which this argument group originates.
  189. enum syscall_src_func source_fn;
  190. __u32 fd;
  191. // For send()/recv()/write()/read().
  192. const char *buf;
  193. // For sendmsg()/recvmsg()/writev()/readv().
  194. const struct iovec *iov;
  195. size_t iovlen;
  196. union {
  197. // For sendmmsg()
  198. unsigned int *msg_len;
  199. // For clock_gettime()
  200. struct timespec *timestamp_ptr;
  201. };
  202. // Timestamp for enter syscall function.
  203. __u64 enter_ts;
  204. __u32 tcp_seq; // Used to record the entry of syscalls
  205. ssize_t bytes_count; // io event
  206. } __attribute__ ((packed));
  207. struct syscall_comm_enter_ctx {
  208. __u64 __pad_0; /* 0 8 */
  209. int __syscall_nr; /* offset:8 4 */
  210. __u32 __pad_1; /* 12 4 */
  211. union {
  212. struct {
  213. __u64 fd; /* offset:16 8 */
  214. char *buf; /* offset:24 8 */
  215. };
  216. // For clock_gettime()
  217. struct {
  218. clockid_t which_clock; /* offset:16 8 */
  219. struct timespec * tp; /* offset:24 8 */
  220. };
  221. };
  222. size_t count; /* 32 8 */
  223. unsigned int flags;
  224. };
  225. struct sched_comm_exit_ctx {
  226. __u64 __pad_0; /* 0 8 */
  227. char comm[16]; /* offset:8; size:16 */
  228. pid_t pid; /* offset:24; size:4 */
  229. int prio; /* offset:28; size:4 */
  230. };
  231. struct sched_comm_fork_ctx {
  232. __u64 __pad_0;
  233. char parent_comm[16];
  234. __u32 parent_pid;
  235. char child_comm[16];
  236. __u32 child_pid;
  237. };
  238. struct sched_comm_exec_ctx {
  239. __u64 __pad_0; /* 0 8 */
  240. int __data_loc; /* offset:8 4 */
  241. __u32 pid; /* offset:12 4 */
  242. __u32 old_pid; /* offset:16 4 */
  243. };
  244. struct syscall_comm_exit_ctx {
  245. __u64 __pad_0; /* 0 8 */
  246. int __syscall_nr; /* offset:8 4 */
  247. __u32 __pad_1; /* 12 4 */
  248. __u64 ret; /* offset:16 8 */
  249. };
  250. static __inline __u64 gen_conn_key_id(__u64 param_1, __u64 param_2)
  251. {
  252. /*
  253. * key:
  254. * - param_1 low 32bits as key high bits.
  255. * - param_2 low 32bits as key low bits.
  256. */
  257. return ((param_1 << 32) | (__u32)param_2);
  258. }
  259. #define MAX_SYSTEM_THREADS 40960
  260. struct go_interface {
  261. unsigned long long type;
  262. void *ptr;
  263. };
  264. //struct go_slice {
  265. // void *ptr;
  266. // unsigned long long len;
  267. // unsigned long long cap;
  268. //};
  269. //struct go_string {
  270. // const char *ptr;
  271. // unsigned long long len;
  272. //};
  273. struct tls_conn {
  274. int fd;
  275. char *buffer;
  276. __u32 tcp_seq;
  277. void *sp; // stack pointer
  278. };
  279. struct tls_conn_key
  280. {
  281. __u32 tgid;
  282. __u64 goid;
  283. };
  284. // Protocol inference fast cache structure
  285. struct proto_infer_cache_t {
  286. /*
  287. * The lower 16 bits of the process-ID/thread-ID
  288. * are used as the index and correspond to the protocol type.
  289. */
  290. __u8 protocols[65536];
  291. };
  292. #endif /* DF_BPF_SOCKET_TRACE_H */