socket.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package tracer
  2. import (
  3. "fmt"
  4. "github.com/cilium/ebpf"
  5. "github.com/cilium/ebpf/btf"
  6. "k8s.io/klog/v2"
  7. "net"
  8. "os"
  9. "runtime"
  10. "syscall"
  11. "time"
  12. )
  13. func init() {
  14. enable_ebpf_protocol(PROTO_HTTP1)
  15. enable_ebpf_protocol(PROTO_HTTP2)
  16. enable_ebpf_protocol(PROTO_TLS_HTTP1)
  17. enable_ebpf_protocol(PROTO_TLS_HTTP2)
  18. enable_ebpf_protocol(PROTO_DUBBO)
  19. enable_ebpf_protocol(PROTO_SOFARPC)
  20. enable_ebpf_protocol(PROTO_MYSQL)
  21. enable_ebpf_protocol(PROTO_POSTGRESQL)
  22. enable_ebpf_protocol(PROTO_REDIS)
  23. enable_ebpf_protocol(PROTO_KAFKA)
  24. enable_ebpf_protocol(PROTO_MQTT)
  25. enable_ebpf_protocol(PROTO_DNS)
  26. }
  27. func MapInit(collectionSpec *ebpf.CollectionSpec, opts *ebpf.CollectionOptions) {
  28. set_offset_map(collectionSpec, opts)
  29. set_conf_map_default(collectionSpec, opts)
  30. //offsetData := make([]any, runtime.NumCPU())
  31. //for i := range offsetData {
  32. // offsetData[i] = testStruct{
  33. // test_id: 99999,
  34. // }
  35. //}
  36. //if bpf_table_set_value(collectionSpec, opts, "test_heap", offsetData) != ETR_OK {}
  37. //insert_output_prog_to_map(collectionSpec, opts)
  38. }
  39. func MapInsert(collection *ebpf.Collection) {
  40. insert_output_prog_to_map(collection)
  41. insert_adapt_kern_uid_to_map(collection)
  42. // Update protocol filter array
  43. update_protocol_filter_array(collection)
  44. }
  45. func insert_output_prog_to_map(collection *ebpf.Collection) {
  46. // jmp for tracepoints
  47. __insert_output_prog_to_map(collection, MAP_PROGS_JMP_TP_NAME, PROG_DATA_SUBMIT_NAME_FOR_TP, PROG_DATA_SUBMIT_TP_IDX)
  48. __insert_output_prog_to_map(collection, MAP_PROGS_JMP_TP_NAME, PROG_OUTPUT_DATA_NAME_FOR_TP, PROG_OUTPUT_DATA_TP_IDX)
  49. __insert_output_prog_to_map(collection, MAP_PROGS_JMP_TP_NAME, PROG_IO_EVENT_NAME_FOR_TP, PROG_IO_EVENT_TP_IDX)
  50. // jmp for kprobe/uprobe
  51. __insert_output_prog_to_map(collection, MAP_PROGS_JMP_KP_NAME, PROG_DATA_SUBMIT_NAME_FOR_KP, PROG_DATA_SUBMIT_KP_IDX)
  52. __insert_output_prog_to_map(collection, MAP_PROGS_JMP_KP_NAME, PROG_OUTPUT_DATA_NAME_FOR_KP, PROG_OUTPUT_DATA_KP_IDX)
  53. }
  54. func __insert_output_prog_to_map(collection *ebpf.Collection, mapName string, progName string, key uint32) {
  55. // find in programs
  56. prog, ok := collection.Programs[progName]
  57. fmt.Println(prog, ok)
  58. if ok {
  59. progFd := prog.FD()
  60. //fmt.Println("progFd", progFd)
  61. if bpf_table_set_value(collection, mapName, key, uint32(progFd)) != ETR_OK {
  62. //fmt.Println("no")
  63. } else {
  64. //fmt.Println("ok")
  65. }
  66. }
  67. }
  68. func update_protocol_filter_array(collection *ebpf.Collection) {
  69. for i := 0; i < PROTO_NUM; i++ {
  70. if bpf_table_set_value(collection, MAP_PROTO_FILTER_NAME, uint32(i), EbpfConfigProtocolFilter[i]) != ETR_OK {
  71. //fmt.Println("no")
  72. } else {
  73. //fmt.Println("ok")
  74. }
  75. }
  76. }
  77. //func update_allow_port_bitmap(collection *ebpf.Collection) {
  78. // for i := 0; i < PROTO_NUM; i++ {
  79. // if bpf_table_set_value(collection, MAP_ALLOW_PORT_BITMAP_NAME, 0, &allow_port_bitmap) != ETR_OK {
  80. // fmt.Println("no")
  81. // } else {
  82. // fmt.Println("ok")
  83. // }
  84. // }
  85. //}
  86. func insert_adapt_kern_uid_to_map(collection *ebpf.Collection) {
  87. pid := os.Getpid()
  88. tid := syscall.Gettid()
  89. adaptKernUID := uint64(pid)<<32 | uint64(tid)
  90. if bpf_table_set_value(collection, MAP_ADAPT_KERN_UID_NAME, 0, uint32(adaptKernUID)) != ETR_OK {
  91. fmt.Println("no")
  92. } else {
  93. fmt.Println("ok")
  94. }
  95. }
  96. func enable_ebpf_protocol(protocol int) {
  97. if protocol < PROTO_NUM {
  98. EbpfConfigProtocolFilter[protocol] = 1
  99. }
  100. }
  101. func Offset() {
  102. listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", offsetInferServerAddr, offsetInferServerPort))
  103. if err != nil {
  104. fmt.Errorf("failed to create server listener: %v", err)
  105. return
  106. }
  107. if err := kernelOffsetInferServer(listener); err != nil {
  108. fmt.Printf("Error in kernel_offset_infer_server: %v\n", err)
  109. }
  110. if err := kernelOffsetInferClient(); err != nil {
  111. fmt.Printf("Error in kernel_offset_infer_client: %v\n", err)
  112. }
  113. defer listener.Close()
  114. }
  115. func set_offset_map(collectionSpec *ebpf.CollectionSpec, opts *ebpf.CollectionOptions) {
  116. // 解析BTF数据
  117. if update_offset_map_from_btf_vmlinux(collectionSpec, opts) != ETR_OK {
  118. klog.Infof("[eBPF Kernel Adapt] Set offsets map from btf_vmlinux, not support.\n")
  119. if update_offset_map_default(collectionSpec, opts) != ETR_OK {
  120. klog.Infof("Fatal error, failed to update default offset\n")
  121. }
  122. } else {
  123. klog.Infof("[eBPF Kernel Adapt] Set offsets map from btf_vmlinux, success.\n")
  124. }
  125. }
  126. func set_conf_map_default(collectionSpec *ebpf.CollectionSpec, opts *ebpf.CollectionOptions) {
  127. uidBase := uint64(time.Now().UnixNano()/int64(time.Millisecond)) & 0xffffffffffffff
  128. numCPU := runtime.NumCPU()
  129. tConf := make([]any, numCPU)
  130. for i := range tConf {
  131. socketID := uint64(i)<<56 | uint64(uidBase)
  132. tracerConf := traceConf{
  133. SocketID: socketID,
  134. CoroutineTraceID: socketID,
  135. ThreadTraceID: socketID,
  136. DataLimitMax: 4096,
  137. GoTracingTimeout: 120,
  138. IOEventCollectMode: 1,
  139. IOEventMinimalDuartion: 1000000,
  140. }
  141. tConf[i] = tracerConf
  142. }
  143. if bpf_table_pre_set_value(collectionSpec, opts, MAP_TRACE_CONF_NAME, tConf) != ETR_OK {
  144. klog.Infof("[eBPF Kernel Adapt] Set config map from btf_vmlinux, not support.\n")
  145. } else {
  146. klog.Infof("[eBPF Kernel Adapt] Set config map from btf_vmlinux, success.\n")
  147. }
  148. }
  149. // 解析BTF数据
  150. func update_offset_map_from_btf_vmlinux(collectionSpec *ebpf.CollectionSpec, opts *ebpf.CollectionOptions) int {
  151. btfSpec, err := btf.LoadKernelSpec()
  152. if err != nil || btfSpec == nil {
  153. return ETR_NOTSUPP
  154. }
  155. copied_seq_offs := kernel_struct_field_offset(btfSpec, "tcp_sock", "copied_seq")
  156. write_seq_offs := kernel_struct_field_offset(btfSpec, "tcp_sock", "write_seq")
  157. files_offs := kernel_struct_field_offset(btfSpec, "task_struct", "files")
  158. sk_flags_offs := kernel_struct_field_offset(btfSpec, "sock", "__sk_flags_offset")
  159. if sk_flags_offs == ETR_NOTEXIST {
  160. sk_flags_offs = kernel_struct_field_offset(btfSpec, "sock", "sk_pacing_shift")
  161. if sk_flags_offs > 0 {
  162. sk_flags_offs -= 1
  163. }
  164. }
  165. struct_files_struct_fdt_offset := kernel_struct_field_offset(btfSpec, "files_struct", "fdt")
  166. struct_files_private_data_offset := kernel_struct_field_offset(btfSpec, "file", "private_data")
  167. struct_file_f_inode_offset := kernel_struct_field_offset(btfSpec, "file", "f_inode")
  168. struct_inode_i_mode_offset := kernel_struct_field_offset(btfSpec, "inode", "i_mode")
  169. struct_file_dentry_offset_1 := kernel_struct_field_offset(btfSpec, "file", "f_path")
  170. struct_file_dentry_offset_2 := kernel_struct_field_offset(btfSpec, "path", "dentry")
  171. if struct_file_dentry_offset_1 < 0 ||
  172. struct_file_dentry_offset_2 < 0 {
  173. return ETR_NOTSUPP
  174. }
  175. struct_file_dentry_offset := struct_file_dentry_offset_1 + struct_file_dentry_offset_2
  176. struct_dentry_name_offset_1 := kernel_struct_field_offset(btfSpec, "dentry", "d_name")
  177. struct_dentry_name_offset_2 := kernel_struct_field_offset(btfSpec, "qstr", "name")
  178. if struct_dentry_name_offset_1 < 0 ||
  179. struct_dentry_name_offset_2 < 0 {
  180. return ETR_NOTSUPP
  181. }
  182. struct_dentry_name_offset := struct_dentry_name_offset_1 + struct_dentry_name_offset_2
  183. struct_sock_family_offset := kernel_struct_field_offset(btfSpec, "sock_common", "skc_family")
  184. struct_sock_saddr_offset := kernel_struct_field_offset(btfSpec, "sock_common", "skc_rcv_saddr")
  185. struct_sock_daddr_offset := kernel_struct_field_offset(btfSpec, "sock_common", "skc_daddr")
  186. struct_sock_ip6saddr_offset := kernel_struct_field_offset(btfSpec, "sock_common", "skc_v6_rcv_saddr")
  187. struct_sock_ip6daddr_offset := kernel_struct_field_offset(btfSpec, "sock_common", "skc_v6_daddr")
  188. struct_sock_dport_offset := kernel_struct_field_offset(btfSpec, "sock_common", "skc_dport")
  189. struct_sock_sport_offset := kernel_struct_field_offset(btfSpec, "sock_common", "skc_num")
  190. struct_sock_skc_state_offset := kernel_struct_field_offset(btfSpec, "sock_common", "skc_state")
  191. struct_sock_common_ipv6only_offset := kernel_struct_field_offset(btfSpec, "sock_common", "skc_flags")
  192. klog.Infof("Offsets from BTF vmlinux:\n")
  193. klog.Infof(" copied_seq_offs: 0x%x\n", copied_seq_offs)
  194. klog.Infof(" write_seq_offs: 0x%x\n", write_seq_offs)
  195. klog.Infof(" files_offs: 0x%x\n", files_offs)
  196. klog.Infof(" sk_flags_offs: 0x%x\n", sk_flags_offs)
  197. klog.Infof(" struct_files_struct_fdt_offset: 0x%x\n", struct_files_struct_fdt_offset)
  198. klog.Infof(" struct_files_private_data_offset: 0x%x\n", struct_files_private_data_offset)
  199. klog.Infof(" struct_file_f_inode_offset: 0x%x\n", struct_file_f_inode_offset)
  200. klog.Infof(" struct_inode_i_mode_offset: 0x%x\n", struct_inode_i_mode_offset)
  201. klog.Infof(" struct_file_dentry_offset: 0x%x\n", struct_file_dentry_offset)
  202. klog.Infof(" struct_dentry_name_offset: 0x%x\n", struct_dentry_name_offset)
  203. klog.Infof(" struct_sock_family_offset: 0x%x\n", struct_sock_family_offset)
  204. klog.Infof(" struct_sock_saddr_offset: 0x%x\n", struct_sock_saddr_offset)
  205. klog.Infof(" struct_sock_daddr_offset: 0x%x\n", struct_sock_daddr_offset)
  206. klog.Infof(" struct_sock_ip6saddr_offset: 0x%x\n", struct_sock_ip6saddr_offset)
  207. klog.Infof(" struct_sock_ip6daddr_offset: 0x%x\n", struct_sock_ip6daddr_offset)
  208. klog.Infof(" struct_sock_dport_offset: 0x%x\n", struct_sock_dport_offset)
  209. klog.Infof(" struct_sock_sport_offset: 0x%x\n", struct_sock_sport_offset)
  210. klog.Infof(" struct_sock_skc_state_offset: 0x%x\n", struct_sock_skc_state_offset)
  211. klog.Infof(" struct_sock_common_ipv6only_offset: 0x%x\n", struct_sock_common_ipv6only_offset)
  212. if copied_seq_offs < 0 || write_seq_offs < 0 || files_offs < 0 ||
  213. sk_flags_offs < 0 || struct_files_struct_fdt_offset < 0 ||
  214. struct_files_private_data_offset < 0 ||
  215. struct_file_f_inode_offset < 0 || struct_inode_i_mode_offset < 0 ||
  216. struct_inode_i_mode_offset < 0 || struct_file_dentry_offset < 0 ||
  217. struct_dentry_name_offset < 0 || struct_sock_family_offset < 0 ||
  218. struct_sock_saddr_offset < 0 || struct_sock_daddr_offset < 0 ||
  219. struct_sock_ip6saddr_offset < 0 ||
  220. struct_sock_ip6daddr_offset < 0 || struct_sock_dport_offset < 0 ||
  221. struct_sock_sport_offset < 0 || struct_sock_skc_state_offset < 0 ||
  222. struct_sock_common_ipv6only_offset < 0 {
  223. return ETR_NOTSUPP
  224. }
  225. offset := bpfOffsetParam{}
  226. offset.Ready = 1
  227. offset.TaskFilesOffset = uint32(files_offs)
  228. offset.SockFlagsOffset = uint32(sk_flags_offs)
  229. offset.TcpSockCopiedSeqOffset = uint32(copied_seq_offs)
  230. offset.TcpSockWriteSeqOffset = uint32(write_seq_offs)
  231. offset.StructFilesStructFdtOffset = uint32(struct_files_struct_fdt_offset)
  232. offset.StructFilesPrivateDataOffset = uint32(struct_files_private_data_offset)
  233. offset.StructFileFInodeOffset = uint32(struct_file_f_inode_offset)
  234. offset.StructInodeIModeOffset = uint32(struct_inode_i_mode_offset)
  235. offset.StructFileDentryOffset = uint32(struct_file_dentry_offset)
  236. offset.StructDentryNameOffset = uint32(struct_dentry_name_offset)
  237. offset.StructSockFamilyOffset = uint32(struct_sock_family_offset)
  238. offset.StructSockSaddrOffset = uint32(struct_sock_saddr_offset)
  239. offset.StructSockDaddrOffset = uint32(struct_sock_daddr_offset)
  240. offset.StructSockIp6saddrOffset = uint32(struct_sock_ip6saddr_offset)
  241. offset.StructSockIp6daddrOffset = uint32(struct_sock_ip6daddr_offset)
  242. offset.StructSockDportOffset = uint32(struct_sock_dport_offset)
  243. offset.StructSockSportOffset = uint32(struct_sock_sport_offset)
  244. offset.StructSockSkcStateOffset = uint32(struct_sock_skc_state_offset)
  245. offset.StructSockCommonIpv6onlyOffset = uint32(struct_sock_common_ipv6only_offset)
  246. if update_offsets_table(collectionSpec, opts, offset) != ETR_OK {
  247. return ETR_UPDATE_MAP_FAILD
  248. }
  249. return ETR_OK
  250. }
  251. func update_offset_map_default(collectionSpec *ebpf.CollectionSpec, opts *ebpf.CollectionOptions) int {
  252. offset := bpfOffsetParam{}
  253. offset.StructFilesStructFdtOffset = 0x20
  254. offset.StructFilesPrivateDataOffset = 0xc8
  255. offset.StructFileFInodeOffset = 0x20
  256. offset.StructInodeIModeOffset = 0x00
  257. offset.StructFileDentryOffset = 0x18
  258. offset.StructDentryNameOffset = 0x28
  259. offset.StructSockFamilyOffset = 0x10
  260. offset.StructSockSaddrOffset = 0x4
  261. offset.StructSockDaddrOffset = 0x0
  262. offset.StructSockIp6saddrOffset = 0x48
  263. offset.StructSockIp6daddrOffset = 0x38
  264. offset.StructSockDportOffset = 0xc
  265. offset.StructSockSportOffset = 0xe
  266. offset.StructSockSkcStateOffset = 0x12
  267. offset.StructSockCommonIpv6onlyOffset = 0x13
  268. if update_offsets_table(collectionSpec, opts, offset) != ETR_OK {
  269. return ETR_UPDATE_MAP_FAILD
  270. }
  271. return ETR_OK
  272. }
  273. func update_offsets_table(collectionSpec *ebpf.CollectionSpec, opts *ebpf.CollectionOptions, offset bpfOffsetParam) int {
  274. numCPU := runtime.NumCPU()
  275. offsetData := make([]any, numCPU)
  276. for i := range offsetData {
  277. offsetData[i] = offset
  278. }
  279. if bpf_table_pre_set_value(collectionSpec, opts, MAP_MEMBERS_OFFSET_NAME, offsetData) != ETR_OK {
  280. return ETR_UPDATE_MAP_FAILD
  281. }
  282. return ETR_OK
  283. }