l7.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. #define PROTOCOL_TRACE 200
  2. #define PROTOCOL_UNKNOWN 0
  3. #define PROTOCOL_HTTP 1
  4. #define PROTOCOL_POSTGRES 2
  5. #define PROTOCOL_REDIS 3
  6. #define PROTOCOL_MEMCACHED 4
  7. #define PROTOCOL_MYSQL 5
  8. #define PROTOCOL_MONGO 6
  9. #define PROTOCOL_KAFKA 7
  10. #define PROTOCOL_CASSANDRA 8
  11. #define PROTOCOL_RABBITMQ 9
  12. #define PROTOCOL_NATS 10
  13. #define PROTOCOL_HTTP2 11
  14. #define PROTOCOL_DUBBO2 12
  15. #define PROTOCOL_DNS 13
  16. #define PROTOCOL_DM 14
  17. #define PROTOCOL_MARIADB 15
  18. #define PROTOCOL_GRPC 16
  19. #define STATUS_UNKNOWN 0
  20. #define STATUS_OK 200
  21. #define STATUS_FAILED 500
  22. #define METHOD_UNKNOWN 0
  23. #define METHOD_PRODUCE 1
  24. #define METHOD_CONSUME 2
  25. #define METHOD_STATEMENT_PREPARE 3
  26. #define METHOD_STATEMENT_CLOSE 4
  27. #define METHOD_HTTP2_CLIENT_FRAMES 5
  28. #define METHOD_HTTP2_SERVER_FRAMES 6
  29. #define ERROR_MSG_PAYLOAD_SIZE 128
  30. #define MAX_PAYLOAD_SIZE 1024 // must be power of 2
  31. #define TRUNCATE_PAYLOAD_SIZE(size) ({ \
  32. size = MIN(size, MAX_PAYLOAD_SIZE-1); \
  33. asm volatile ("%0 &= %1" : "+r"(size) : "i"(MAX_PAYLOAD_SIZE-1)); \
  34. })
  35. #define COPY_PAYLOAD(dst, size, src) ({ \
  36. TRUNCATE_PAYLOAD_SIZE(size); \
  37. if (bpf_probe_read(dst, size, src)) { \
  38. return 0; \
  39. } \
  40. })
  41. #define NS_PER_SEC 1000000000ULL
  42. #define IOVEC_BUF_SIZE MAX_PAYLOAD_SIZE * 2 // must be double of MAX_PAYLOAD_SIZE
  43. #define MAX_IOVEC_SIZE 32
  44. #include "http.c"
  45. #include "postgres.c"
  46. #include "redis.c"
  47. #include "memcached.c"
  48. #include "mysql.c"
  49. #include "mongo.c"
  50. #include "kafka.c"
  51. #include "cassandra.c"
  52. #include "rabbitmq.c"
  53. #include "nats.c"
  54. #include "http2.c"
  55. #include "dubbo2.c"
  56. #include "dns.c"
  57. #include "dm.c"
  58. // go type l7Event struct && type RequestData struct
  59. struct l7_event {
  60. __u64 fd;
  61. __u64 connection_timestamp;
  62. __u32 pid;
  63. __u32 status;
  64. __u64 duration;
  65. __u8 protocol;
  66. __u8 method;
  67. __u16 padding;
  68. __u32 statement_id;
  69. __u64 payload_size;
  70. __u64 trace_id;
  71. __u64 start_at;
  72. __u64 end_at;
  73. __u32 trace_start;
  74. __u32 trace_end;
  75. __u32 trace_type; // 0: normal, 1: grpc-server, 2: https 3 mq
  76. __u32 event_count;
  77. __u16 sport;
  78. __u16 dport;
  79. __u8 saddr[16];
  80. __u8 daddr[16];
  81. __u16 component_sport;
  82. __u16 component_dport;
  83. __u16 is_tls;
  84. __u8 component_saddr[16];
  85. __u8 component_daddr[16];
  86. unsigned char assumed_app_id[APM_ASSUMED_APP_ID_SIZE];
  87. unsigned char span_id[APM_SPAN_ID_SIZE];
  88. unsigned char trace_id_from[APM_TRACE_ID_SIZE];
  89. unsigned char called_id[APM_ASSUMED_APP_ID_SIZE];
  90. unsigned char instance_id_from[APM_INSTANCE_ID_SIZE];
  91. unsigned char app_id_from[APM_APP_ID_SIZE];
  92. unsigned char span_id_from[APM_SPAN_ID_SIZE];
  93. unsigned char type_from[APM_TYPE_FROM_SIZE];
  94. unsigned char target_addr[64];
  95. // 错误消息字段
  96. unsigned char error_message[ERROR_MSG_PAYLOAD_SIZE];
  97. // __u32 test_id;
  98. // MQ 相关信息(用于 Kafka、RabbitMQ 等消息队列)
  99. struct mq_info_t mq;
  100. char payload[MAX_PAYLOAD_SIZE];
  101. } ;
  102. struct test_t {
  103. __u32 test_id;
  104. };
  105. struct {
  106. __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  107. __type(key, int);
  108. __type(value, struct l7_event);
  109. __uint(max_entries, 1);
  110. } l7_event_heap SEC(".maps");
  111. struct {
  112. __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  113. __type(key, int);
  114. __type(value, struct test_t);
  115. __uint(max_entries, 1);
  116. } test_heap SEC(".maps");
  117. struct {
  118. __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
  119. __uint(key_size, sizeof(int));
  120. __uint(value_size, sizeof(int));
  121. } l7_events SEC(".maps");
  122. struct read_args {
  123. __u64 fd;
  124. char* buf;
  125. __u64* ret;
  126. __u64 iovlen;
  127. };
  128. struct {
  129. __uint(type, BPF_MAP_TYPE_LRU_HASH);
  130. __uint(key_size, sizeof(__u64));
  131. __uint(value_size, sizeof(struct read_args));
  132. __uint(max_entries, 10240);
  133. } active_reads SEC(".maps");
  134. struct l7_request_key {
  135. __u64 fd;
  136. __u32 pid;
  137. __u16 is_tls;
  138. __s16 stream_id;
  139. };
  140. struct l7_request {
  141. __u64 ns;
  142. __u8 protocol;
  143. __u8 partial;
  144. __u8 request_type;
  145. __s32 request_id;
  146. __u64 trace_id;
  147. unsigned char assumed_app_id[APM_ASSUMED_APP_ID_SIZE];
  148. unsigned char span_id[APM_SPAN_ID_SIZE];
  149. __u64 payload_size;
  150. char payload[MAX_PAYLOAD_SIZE];
  151. };
  152. struct {
  153. __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  154. __type(key, int);
  155. __type(value, struct l7_request);
  156. __uint(max_entries, 1);
  157. } l7_request_heap SEC(".maps");
  158. struct {
  159. __uint(type, BPF_MAP_TYPE_LRU_HASH);
  160. __uint(key_size, sizeof(struct l7_request_key));
  161. __uint(value_size, sizeof(struct l7_request));
  162. __uint(max_entries, 32768);
  163. } active_l7_requests SEC(".maps");
  164. struct l7_request_dm_ctx {
  165. // __u8 request_type;
  166. __u64 req_start_at_ns;
  167. // char db_name[128] ;
  168. // __s32 srv_encoding ;
  169. __u64 query_sql_payload_size;
  170. char query_sql_payload[MAX_PAYLOAD_SIZE];
  171. __u32 status;
  172. // __s32 svr_err_code;
  173. // __u64 svr_err_msg_size;
  174. // char svr_err_msg[MAX_PAYLOAD_SIZE];
  175. };
  176. struct {
  177. __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  178. __type(key, int);
  179. __type(value, struct l7_request_dm_ctx);
  180. __uint(max_entries, 1);
  181. } l7_request_dm_ctx_heap SEC(".maps");
  182. struct {
  183. __uint(type, BPF_MAP_TYPE_LRU_HASH);
  184. __uint(key_size, sizeof(struct l7_request_key));
  185. __uint(value_size, sizeof(struct l7_request_dm_ctx));
  186. __uint(max_entries, 32768);
  187. } active_l7_requests_dm_ctx SEC(".maps");
  188. struct {
  189. __uint(type, BPF_MAP_TYPE_LRU_HASH);
  190. __uint(key_size, sizeof(struct l7_request_key));
  191. __uint(value_size, MYSQL_PACKAGE_HEADER_LEN);
  192. __uint(max_entries, 32768);
  193. } active_l7_requests_mysql_resp_header_ctx SEC(".maps");
  194. struct {
  195. __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  196. __type(key, int);
  197. __type(value, char[IOVEC_BUF_SIZE]);
  198. __uint(max_entries, 1);
  199. } iovec_buf_heap SEC(".maps");
  200. struct trace_event_raw_sys_enter_rw__stub {
  201. __u64 unused;
  202. long int id;
  203. __u64 fd;
  204. char* buf;
  205. __u64 size;
  206. };
  207. struct trace_event_raw_sys_exit_rw__stub {
  208. __u64 unused;
  209. long int id;
  210. long int ret;
  211. };
  212. struct l7_iovec {
  213. char* buf;
  214. __u64 size;
  215. };
  216. struct l7_user_msghdr {
  217. void *msg_name;
  218. int msg_namelen;
  219. struct l7_iovec *msg_iov;
  220. __u64 msg_iovlen;
  221. void *msg_control;
  222. __u64 msg_controllen;
  223. __u32 msg_flags;
  224. };
  225. struct {
  226. __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  227. __type(key, int);
  228. __type(value, struct apm_span_context);
  229. __uint(max_entries, 1);
  230. } apm_span_context_heap3 SEC(".maps");
  231. #define TRACE_ID_SIZE 16
  232. static __inline __u32 has_cw_header(const char *data);
  233. static __always_inline void cw_string_to_span_context(char *str, struct apm_span_context *ctx);
  234. static __always_inline void generate_random_bytes(unsigned char *buff, __u32 size);
  235. static __inline __attribute__((__always_inline__)) void cw_save_parent_tracking_span(struct apm_span_context *sc);
  236. static inline __attribute__((__always_inline__))
  237. void send_event(void *ctx, struct l7_event *e, struct connection_id cid, struct connection *conn) {
  238. e->connection_timestamp = conn->timestamp;
  239. e->fd = cid.fd;
  240. e->pid = cid.pid;
  241. long error = bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, e, sizeof(*e));
  242. if (error ==0){
  243. cw_add_event_count(e->trace_id);
  244. }
  245. }
  246. static inline __attribute__((__always_inline__))
  247. __u64 read_iovec(char *iovec, __u64 iovlen, __u64 ret, char *buf, __u64 *total_size) {
  248. struct l7_iovec iov = {};
  249. __u64 max = (ret) ? MIN(ret, MAX_PAYLOAD_SIZE) : MAX_PAYLOAD_SIZE;
  250. __u64 offset = 0;
  251. __u64 size = 0;
  252. #pragma unroll
  253. for (int i = 0; i < MAX_IOVEC_SIZE; i++) {
  254. if (i >= iovlen) {
  255. break;
  256. }
  257. if (bpf_probe_read(&iov, sizeof(iov), (void *)(iovec+i*sizeof(iov)))) {
  258. return 0;
  259. }
  260. if (iov.size <= 0) {
  261. continue;
  262. }
  263. *total_size += iov.size;
  264. if (offset < max) {
  265. size = MIN(iov.size, max-offset);
  266. TRUNCATE_PAYLOAD_SIZE(size);
  267. TRUNCATE_PAYLOAD_SIZE(offset);
  268. if (bpf_probe_read(buf + offset, size, (void *)iov.buf)) {
  269. return 0;
  270. }
  271. offset += size;
  272. }
  273. }
  274. return offset;
  275. }
  276. static inline __attribute__((__always_inline__))
  277. int trace_dns_enter_write(void *ctx, __u64 fd, __u16 is_tls, char *buf, __u64 size, __u64 iovlen) {
  278. __u64 id = bpf_get_current_pid_tgid();
  279. __u32 zero = 0;
  280. __u32 pid, tid;
  281. __u32 http_status ;
  282. pid = id >> 32;
  283. tid = (__u32)id;
  284. if (load_filter_pid() != 0 && pid != load_filter_pid()) {
  285. return 0;
  286. }
  287. char* payload = buf;
  288. __u64 total_size = 0;
  289. if (iovlen) {
  290. payload = bpf_map_lookup_elem(&iovec_buf_heap, &zero);
  291. if (!payload) {
  292. return 0;
  293. }
  294. size = read_iovec(buf, iovlen, 0, payload, &total_size);
  295. }
  296. if (!size) {
  297. return 0;
  298. }
  299. struct l7_request *req = bpf_map_lookup_elem(&l7_request_heap, &zero);
  300. if (!req) {
  301. return 0;
  302. }
  303. req->protocol = PROTOCOL_UNKNOWN;
  304. req->partial = 0;
  305. req->request_id = 0;
  306. req->ns = 0;
  307. req->payload_size = size;
  308. struct l7_request_key k = {};
  309. k.pid = id >> 32;
  310. k.fd = fd;
  311. k.is_tls = is_tls;
  312. k.stream_id = -1;
  313. if (is_dns_request(payload, size, &k.stream_id)) {
  314. req->protocol = PROTOCOL_DNS;
  315. }
  316. if (req->protocol == PROTOCOL_UNKNOWN) {
  317. return 0;
  318. }
  319. if (req->ns == 0) {
  320. req->ns = bpf_ktime_get_ns();
  321. }
  322. COPY_PAYLOAD(req->payload, size, payload);
  323. bpf_map_update_elem(&active_l7_requests, &k, req, BPF_NOEXIST);
  324. return 0;
  325. }
  326. static inline __attribute__((__always_inline__))
  327. int trace_enter_write(void *ctx, __u64 fd, __u16 is_tls, char *buf, __u64 size, __u64 iovlen) {
  328. __u64 id = bpf_get_current_pid_tgid();
  329. __u32 zero = 0;
  330. __u32 pid, tid;
  331. __u32 http_status ;
  332. pid = id >> 32;
  333. tid = (__u32)id;
  334. __u64 total_size = size;
  335. if (load_filter_pid() != 0 && pid != load_filter_pid()) {
  336. return 0;
  337. }
  338. // bpf_printk("trace_enter_write fd(%d) ,size(%d)", fd, size);
  339. char* payload = buf;
  340. if (iovlen) {
  341. payload = bpf_map_lookup_elem(&iovec_buf_heap, &zero);
  342. if (!payload) {
  343. return 0;
  344. }
  345. total_size = 0;
  346. size = read_iovec(buf, iovlen, 0, payload, &total_size);
  347. }
  348. if (!size) {
  349. return 0;
  350. }
  351. struct l7_request *req = bpf_map_lookup_elem(&l7_request_heap, &zero);
  352. if (!req) {
  353. return 0;
  354. }
  355. req->protocol = PROTOCOL_UNKNOWN;
  356. req->partial = 0;
  357. req->request_id = 0;
  358. req->ns = 0;
  359. req->payload_size = size;
  360. struct l7_request_key k = {};
  361. k.pid = pid;
  362. k.fd = fd;
  363. k.is_tls = is_tls;
  364. k.stream_id = -1;
  365. struct connection_id cid = {};
  366. cid.pid = pid;
  367. cid.fd = fd;
  368. // cw_bpf_debug("enter-payload:%s|type:%s|FD:%d\n",payload,"type",k.fd);
  369. if (is_http_response(payload, &http_status))
  370. {
  371. //处理http请求之前,确认进程信息是否存在
  372. struct ebpf_proc_info *proc_info = bpf_map_lookup_elem(&proc_info_map, &pid);
  373. if (!proc_info) {
  374. cw_bpf_debug("[Trace End in l7][Response][HTTP]:no proc info. pid:%d \n",k.pid);
  375. return 0;
  376. }
  377. struct apm_trace_info_t * start_trace_info = get_trace_info_by_fd(pid, fd);
  378. if (!start_trace_info) {
  379. return -1;
  380. }
  381. __u64 trace_id = start_trace_info->trace_id;
  382. __u32 event_count = cw_get_event_count(trace_id);
  383. // bpf_printk("[Trace End in l7] count(%d) %llu ", event_count, trace_id);
  384. cw_bpf_debug("[uprobeThread/pidpidpidpid][Trace End in l7][HTTP]pid:[%d]--[%lld]", tid, bpf_ktime_get_ns());
  385. cw_bpf_debug("[Trace End in l7][Response][HTTP] event_count:%d", event_count);
  386. cw_bpf_debug("[Trace End in l7][Response][HTTP] pid:%d,fd:%d,trace_id:%llu", tid, fd, trace_id);
  387. // 发送事件到用户空间 start
  388. struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
  389. if (!e) {
  390. cw_clear_trace(pid, tid, fd);
  391. return -1;
  392. }
  393. // parent sc
  394. struct apm_span_context *cw_psc = cw_get_parent_tracking_span_by_trace_key(start_trace_info->trace_key);
  395. if(cw_psc){
  396. // bpf_printk("[pid:%d][goid:%d] find header",pid,start_trace_info->trace_key.goid);
  397. cw_copy_byte_arrays(cw_psc->trace_id, e->trace_id_from, APM_TRACE_ID_SIZE);
  398. cw_copy_byte_arrays(cw_psc->assumed_app_id, e->called_id, APM_ASSUMED_APP_ID_SIZE);
  399. cw_copy_byte_arrays(cw_psc->instance_id, e->instance_id_from, APM_INSTANCE_ID_SIZE);
  400. cw_copy_byte_arrays(cw_psc->app_id, e->app_id_from, APM_APP_ID_SIZE);
  401. cw_copy_byte_arrays(cw_psc->span_id, e->span_id_from, APM_SPAN_ID_SIZE);
  402. cw_copy_byte_arrays(cw_psc->type_from, e->type_from, APM_TYPE_FROM_SIZE);
  403. // for (int i = 0; i < APM_TYPE_FROM_SIZE; i++) {
  404. // bpf_printk("trace_enter_write - type_from = %02x", e->type_from[i]);
  405. // }
  406. }
  407. struct l7_request *req = bpf_map_lookup_elem(&active_l7_requests, &k);
  408. if (!req)
  409. {
  410. cw_clear_trace(pid, tid, fd);
  411. return 0;
  412. }
  413. SET_TRACE_END(e, PROTOCOL_HTTP);
  414. e->start_at = req->ns;
  415. cw_bpf_debug("req->ns:%llu",req->ns);
  416. e->end_at = bpf_ktime_get_ns();
  417. e->duration = e->end_at - e->start_at;
  418. // cw_bpf_debug("[Response][HTTP]:duration->ns:%d\n",e->duration);
  419. e->status = http_status;
  420. e->pid = k.pid;
  421. e->fd = k.fd;
  422. // e->connection_timestamp = get_connection_timestamp(k.pid, k.fd);
  423. e->trace_type = 0;
  424. e->trace_id = trace_id;
  425. e->payload_size = size;
  426. e->event_count = event_count;
  427. COPY_PAYLOAD(e->payload, size, payload);
  428. e->is_tls = is_tls;
  429. bpf_map_delete_elem(&active_l7_requests, &k);
  430. // 清除事件计数
  431. bpf_map_delete_elem(&trace_event_count_heap, &trace_id);
  432. // 清除业务层trace信息
  433. clear_parent_span_context_by_trace_key(start_trace_info->trace_key);
  434. // 清除trace信息
  435. cw_clear_trace(pid, tid, fd);
  436. cw_bpf_debug("socket accept bytes_sent cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  437. struct accept_connection *accept_conn = bpf_map_lookup_elem(&active_accepts, &cid);
  438. if (accept_conn) {
  439. cw_bpf_debug("socket accept bytes_sent after cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  440. cw_bpf_debug("rock enter the accept_conn function cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  441. e->sport = accept_conn->sport;
  442. e->dport = accept_conn->dport;
  443. __builtin_memcpy(&e->saddr, &accept_conn->saddr, sizeof(e->saddr));
  444. __builtin_memcpy(&e->daddr, &accept_conn->daddr, sizeof(e->daddr));
  445. // __sync_fetch_and_add(&accept_conn->bytes_sent, total_size);
  446. cw_bpf_debug("socket sys_exit_accept--- accept_conn daddr=%llu, daddr=%llu\n", e->saddr[10], e->saddr[11]);
  447. cw_bpf_debug("socket sys_exit_accept--- accept_conn daddr=%llu, daddr=%llu\n", e->saddr[12], e->saddr[13]);
  448. cw_bpf_debug("socket sys_exit_accept--- accept_conn daddr=%llu, daddr=%llu\n", e->saddr[14], e->saddr[15]);
  449. }
  450. bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, e, sizeof(*e));
  451. // 发送事件到用户空间 end
  452. // __u64 k_version = load_filter_pid();
  453. // cw_bpf_debug("filter_pid:%d", k_version);
  454. //
  455. // struct test_t *ttt = bpf_map_lookup_elem(&test_heap, &zero);
  456. // if (!ttt) {
  457. // return 0;
  458. // }
  459. // struct member_fields_offset *off = bpf_map_lookup_elem(&__members_offset, &zero);
  460. // if (!off) {
  461. // return 0;
  462. // }
  463. // cw_bpf_debug("off->task__files_offset:%x", off->task__files_offset);
  464. // cw_bpf_debug("e->test_id1111:%d", ttt->test_id);
  465. cw_bpf_debug("HTTP_END");
  466. // //TODO 4 查询
  467. // cw_bpf_debug("socket accept bytes_sent cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  468. // struct accept_connection *accept_conn = bpf_map_lookup_elem(&active_accepts, &cid);
  469. // if (accept_conn && !is_tls) {
  470. // cw_bpf_debug("socket accept bytes_sent after cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  471. // e.sport = accept_conn.sport;
  472. // e.dport = accept_conn.dport;
  473. // __builtin_memcpy(&e.saddr, &accept_conn.saddr, sizeof(e.saddr));
  474. // __builtin_memcpy(&e.daddr, &accept_conn.daddr, sizeof(e.daddr));
  475. // // __sync_fetch_and_add(&accept_conn->bytes_sent, total_size);
  476. // }
  477. return 0;
  478. }
  479. struct connection *conn = bpf_map_lookup_elem(&active_connections, &cid);
  480. if (!conn) {
  481. //TODO 4 查询
  482. // cw_bpf_debug("socket accept bytes_sent cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  483. // struct connection *accept_conn = bpf_map_lookup_elem(&active_accepts, &cid);
  484. // if (accept_conn && !is_tls) {
  485. // cw_bpf_debug("socket accept bytes_sent after cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  486. // __sync_fetch_and_add(&accept_conn->bytes_sent, total_size);
  487. // }
  488. return 0;
  489. }
  490. if (!is_tls) {
  491. __sync_fetch_and_add(&conn->bytes_sent, total_size);
  492. if(conn->first_write_time == 0){
  493. conn->first_write_time = bpf_ktime_get_ns();
  494. }
  495. }
  496. if (is_http_request(payload)) {
  497. cw_bpf_debug("");
  498. cw_bpf_debug("-----[Kernel HTTP Enter]:pid:[%d]|CURRENT-GOID:[%llu]|FD:[%d]", tid, get_current_goroutine(), k.fd);
  499. __u8 type = 0;
  500. __u64 trace_id = 0;
  501. struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
  502. struct apm_trace_info_t * trace_info = get_apm_trace_info_by_trace_key(trace_key);
  503. if (trace_info == NULL) {
  504. trace_info = get_apm_trace_info_v3(trace_key,id, pid, tid);
  505. }
  506. if (trace_info) {
  507. // cw_bpf_debug("%llu",trace_info->trace_id);
  508. trace_id = trace_info->trace_id;
  509. type = trace_info->type;
  510. }
  511. req->protocol = PROTOCOL_HTTP;
  512. req->trace_id = trace_id;
  513. // cw_bpf_debug("l7.c111 addr is --------:%d,%s",conn->sport,conn->saddr);
  514. struct apm_span_context * sc = cw_get_current_tracking_span(trace_info);
  515. if (sc) {
  516. cw_copy_byte_arrays(sc->assumed_app_id, req->assumed_app_id, APM_ASSUMED_APP_ID_SIZE);
  517. cw_copy_byte_arrays(sc->span_id, req->span_id, APM_SPAN_ID_SIZE);
  518. // for (int i = 0; i < APM_ASSUMED_APP_ID_SIZE; i++) {
  519. // cw_bpf_debug("assumed_app_id-assumed_app_id[%d] = %02x", i, req->assumed_app_id[i]);
  520. // }
  521. // for (int i = 0; i < APM_SPAN_ID_SIZE; i++) {
  522. // cw_bpf_debug("cw_get_current_tracking_span-span_id[%d] = %02x", i, req->span_id[i]);
  523. // }
  524. }
  525. // 0默认,1
  526. cw_bpf_debug("[Kernel HTTP Enter]req-payload:%s [traceid:%llu][type:%d]",payload,trace_id,type);
  527. } else if (is_postgres_query(payload, size, &req->request_type)) {
  528. // if (req->request_type == POSTGRES_FRAME_CLOSE) {
  529. // struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
  530. // if (!e) {
  531. // return 0;
  532. // }
  533. // __u64 trace_id = get_apm_trace_id(pid, tid);
  534. // e->trace_id = trace_id;
  535. // e->protocol = PROTOCOL_POSTGRES;
  536. // e->method = METHOD_STATEMENT_CLOSE;
  537. // e->payload_size = size;
  538. // COPY_PAYLOAD(e->payload, size, payload);
  539. // send_event(ctx, e, cid, conn);
  540. // return 0;
  541. // }
  542. req->protocol = PROTOCOL_POSTGRES;
  543. } else if (is_redis_query(payload, size)) {
  544. cw_bpf_debug("[Enter][Redis]:TGID:%d|type:%s|FD:%d\n",k.pid,"type",k.fd);
  545. req->protocol = PROTOCOL_REDIS;
  546. } else if (is_memcached_query(payload, size)) {
  547. cw_bpf_debug("[Enter][MEMCACHE]:TGID:%d|type:%s|FD:%d\n",k.pid,"type",k.fd);
  548. req->protocol = PROTOCOL_MEMCACHED;
  549. } else if (is_mysql_query(payload, size, &req->request_type)) {
  550. cw_bpf_debug("[Enter][Mysql]:thread_id:%d\n",tid);
  551. if (req->request_type == MYSQL_COM_STMT_CLOSE) {
  552. return 0;
  553. struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
  554. if (!e) {
  555. return 0;
  556. }
  557. __u64 trace_id = get_apm_trace_id(pid, tid);
  558. e->trace_id = trace_id;
  559. e->protocol = PROTOCOL_MYSQL;
  560. e->method = METHOD_STATEMENT_CLOSE;
  561. e->payload_size = size;
  562. COPY_PAYLOAD(e->payload, size, payload);
  563. cw_bpf_debug("[Enter][Mysql][Send]:thread_id:%d\n",tid);
  564. send_event(ctx, e, cid, conn);
  565. return 0;
  566. }
  567. req->protocol = PROTOCOL_MYSQL;
  568. } else if (is_dm_query(payload, size,&req->request_type)) {
  569. // cw_bpf_debug("[Request][DM] start -------->");
  570. req->protocol = PROTOCOL_DM;
  571. struct l7_request_dm_ctx *dm_ctx ;
  572. dm_ctx = bpf_map_lookup_elem(&active_l7_requests_dm_ctx, &k);
  573. if (!dm_ctx) {
  574. dm_ctx = bpf_map_lookup_elem(&l7_request_dm_ctx_heap, &zero);
  575. if (!dm_ctx) {
  576. return 0;
  577. }
  578. dm_ctx->req_start_at_ns = 0 ;
  579. dm_ctx->status = 0;
  580. bpf_map_update_elem(&active_l7_requests_dm_ctx, &k, dm_ctx, BPF_NOEXIST);
  581. // cw_bpf_debug("[Request][DM] init active_l7_requests_dm_ctx,request_type <0x%x> [%d]",req->request_type,req->request_type);
  582. }
  583. if (req->request_type == DM_QUERY) {
  584. dm_ctx->req_start_at_ns=bpf_ktime_get_ns();
  585. req->ns = dm_ctx->req_start_at_ns ;
  586. dm_ctx->query_sql_payload_size = size;
  587. COPY_PAYLOAD(dm_ctx->query_sql_payload, size, payload);
  588. bpf_map_update_elem(&active_l7_requests_dm_ctx, &k, dm_ctx, BPF_ANY);
  589. }
  590. if(req->request_type == DM_STMT_EXECUTE) {
  591. bpf_map_update_elem(&active_l7_requests, &k, req, BPF_ANY);
  592. return 0 ;
  593. }
  594. // cw_bpf_debug("[Request][DM] is request ,request_type <0x%x> [%d]",req->request_type,req->request_type);
  595. // cw_bpf_debug("[Request][DM] end <--------");
  596. } else if (is_mongo_query(payload, size)) {
  597. req->protocol = PROTOCOL_MONGO;
  598. } else if (is_rabbitmq_produce(payload, size)) {
  599. struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
  600. if (!e) {
  601. return 0;
  602. }
  603. e->protocol = PROTOCOL_RABBITMQ;
  604. e->method = METHOD_PRODUCE;
  605. send_event(ctx, e, cid, conn);
  606. return 0;
  607. } else if (nats_method(payload, size) == METHOD_PRODUCE) {
  608. struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
  609. if (!e) {
  610. return 0;
  611. }
  612. e->protocol = PROTOCOL_NATS;
  613. e->method = METHOD_PRODUCE;
  614. send_event(ctx, e, cid, conn);
  615. return 0;
  616. } else if (is_cassandra_request(payload, size, &k.stream_id)) {
  617. // bpf_printk("[cassandra] [start] fd(%d) stream_id(%d) goid(%d)", k.fd, k.stream_id, get_current_goroutine());
  618. __u32 ctx_id =get_current_goroutine();
  619. struct thread_ctx_t *current_ctx = bpf_map_lookup_elem(&thread_ctx_map, &ctx_id);
  620. if (current_ctx) {
  621. req->trace_id = current_ctx->token;
  622. bpf_map_delete_elem(&thread_ctx_map, &ctx_id);
  623. }
  624. req->protocol = PROTOCOL_CASSANDRA;
  625. } else if (is_kafka_request(payload, size, &req->request_id)) {
  626. req->protocol = PROTOCOL_KAFKA;
  627. struct l7_request *prev_req = bpf_map_lookup_elem(&active_l7_requests, &k);
  628. if (prev_req && prev_req->protocol == PROTOCOL_KAFKA) {
  629. req->ns = prev_req->ns;
  630. }
  631. // } else if (looks_like_http2_frame(payload, size, METHOD_HTTP2_CLIENT_FRAMES)) {
  632. // struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
  633. // if (!e) {
  634. // return 0;
  635. // }
  636. // e->protocol = PROTOCOL_HTTP2;
  637. // e->method = METHOD_HTTP2_CLIENT_FRAMES;
  638. // e->duration = bpf_ktime_get_ns();
  639. // e->payload_size = size;
  640. // e->trace_id = get_apm_trace_id(pid, tid);
  641. // COPY_PAYLOAD(e->payload, size, payload);
  642. // send_event(ctx, e, cid, conn);
  643. // return 0;
  644. } else if (is_dubbo2_request(payload, size)) {
  645. req->protocol = PROTOCOL_DUBBO2;
  646. } else if (is_dns_request(payload, size, &k.stream_id)) {
  647. req->protocol = PROTOCOL_DNS;
  648. }
  649. if (req->protocol == PROTOCOL_UNKNOWN) {
  650. return 0;
  651. }
  652. if (req->ns == 0) {
  653. req->ns = bpf_ktime_get_ns();
  654. }
  655. COPY_PAYLOAD(req->payload, size, payload);
  656. bpf_map_update_elem(&active_l7_requests, &k, req, BPF_NOEXIST);
  657. return 0;
  658. }
  659. static inline __attribute__((__always_inline__))
  660. int trace_enter_read(__u64 id, __u32 pid, __u64 fd, char *buf, __u64 *ret, __u64 iovlen) {
  661. if (load_filter_pid() != 0 && pid != load_filter_pid()) {
  662. return 0;
  663. }
  664. struct read_args args = {};
  665. args.fd = fd;
  666. args.buf = buf;
  667. args.ret = ret;
  668. args.iovlen = iovlen;
  669. bpf_map_update_elem(&active_reads, &id, &args, BPF_ANY);
  670. return 0;
  671. }
  672. // 通用的trace_exit_read逻辑,通过参数控制是否执行bpf_tail_call
  673. static inline __attribute__((__always_inline__))
  674. int trace_exit_read_common(void *ctx, __u64 id, __u32 pid, __u16 is_tls, long int ret, int tp_tail_call) {
  675. __u32 tid = (__u32)id;
  676. if (load_filter_pid() != 0 && pid != load_filter_pid()) {
  677. return 0;
  678. }
  679. struct read_args *args = bpf_map_lookup_elem(&active_reads, &id);
  680. if (!args) {
  681. return 0;
  682. }
  683. struct l7_request_key k = {};
  684. k.pid = pid;
  685. k.fd = args->fd;
  686. k.is_tls = is_tls;
  687. k.stream_id = -1;
  688. bpf_map_delete_elem(&active_reads, &id);
  689. if (ret <= 0) {
  690. return 0;
  691. }
  692. if (args->ret) {
  693. if (bpf_probe_read(&ret, sizeof(ret), (void*)args->ret)) {
  694. return 0;
  695. };
  696. if (ret <= 0) {
  697. return 0;
  698. }
  699. }
  700. __u64 total_size = ret;
  701. int zero = 0;
  702. char* payload = args->buf;
  703. if (args->iovlen) {
  704. payload = bpf_map_lookup_elem(&iovec_buf_heap, &zero);
  705. if (!payload) {
  706. return 0;
  707. }
  708. total_size = 0;
  709. ret = read_iovec(args->buf, args->iovlen, ret, payload, &total_size);
  710. if (!ret) {
  711. return 0;
  712. }
  713. }
  714. struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
  715. if (!e) {
  716. return 0;
  717. }
  718. e->fd = k.fd;
  719. e->pid = k.pid;
  720. e->protocol = PROTOCOL_UNKNOWN;
  721. e->status = STATUS_UNKNOWN;
  722. e->method = METHOD_UNKNOWN;
  723. e->statement_id = 0;
  724. e->payload_size = 0;
  725. e->trace_id = 0;
  726. e->is_tls = is_tls;
  727. __u8 b[8];
  728. // bpf_read(payload, b);
  729. struct connection_id cid = {};
  730. cid.pid = pid;
  731. cid.fd = args->fd;
  732. // 被调用方http入口
  733. // 作为服务端在走。coroot 原有逻辑是没有的
  734. if (is_http_request(payload)) {
  735. //处理http请求之前,确认进程信息是否存在
  736. struct ebpf_proc_info *proc_info = bpf_map_lookup_elem(&proc_info_map, &pid);
  737. if (!proc_info) {
  738. cw_bpf_debug("[Receive][HTTP]:no proc info. pid:%d",k.pid);
  739. return 0;
  740. }
  741. struct l7_request *req = bpf_map_lookup_elem(&l7_request_heap, &zero);
  742. if (!req)
  743. {
  744. return 0;
  745. } else
  746. {
  747. req->protocol = PROTOCOL_HTTP;
  748. req->payload_size = ret;
  749. req->ns = bpf_ktime_get_ns();
  750. COPY_PAYLOAD(req->payload, ret, payload);
  751. // cw_bpf_debug("[Receive][HTTP]:pid:%d|tid:%d",k.pid,k.fd);
  752. // cw_bpf_debug("[Receive][HTTP]:is_tls:%d|tid:%d",k.is_tls,k.stream_id);
  753. bpf_map_update_elem(&active_l7_requests, &k, req, BPF_NOEXIST);
  754. }
  755. // cw_bpf_debug("[Receive][HTTP] payload1:%s|type:%s\n",payload,"type");
  756. // __u64 goid = 0;
  757. // goid = get_rw_goid(120 * NS_PER_SEC, 1);
  758. // cw_bpf_debug("[Receive][HTTP] goid:%llu\n",goid);
  759. // __u64 goid = get_rw_goid(120 * NS_PER_SEC, 1);
  760. // cw_bpf_debug("[Receive][HTTP]:thread_id:%d|goid:%d|FD:%d\n", tid, goid, k.fd);
  761. // // apm trace
  762. // struct apm_trace_key_t trace_key = {0};
  763. // trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
  764. //
  765. // // fd trace
  766. // struct fd_trace_key_t fd_trace_key = {0};
  767. // fd_trace_key = get_fd_trace_key(pid, k.fd);
  768. //
  769. // // trace info
  770. struct apm_trace_info_t trace_info = cw_save_trace_info(id,pid, k.fd);
  771. // __u64 uid_base = bpf_ktime_get_ns();
  772. // trace_info.trace_id = bpf_get_current_pid_tgid() + uid_base;
  773. SET_TRACE_START(e, PROTOCOL_HTTP);
  774. e->trace_type = 0;
  775. e->trace_id = trace_info.trace_id;
  776. cw_bpf_debug("\n");
  777. cw_bpf_debug("[Trace Start in l7][HTTP]pid:[%d]--[%lld]--trace_id:%llu\n", pid, bpf_ktime_get_ns(),trace_info.trace_id);
  778. cw_bpf_debug("[Trace Start in l7][Receive][HTTP]tid:[%d]|GOID:[%d]|FD:%d\n", tid, trace_info.trace_key.goid,k.fd);
  779. e->payload_size = ret;
  780. COPY_PAYLOAD(e->payload, ret, payload);
  781. // pid_tgid:trace_id
  782. // thread_trace_key =
  783. // 入口方法缓存
  784. // bpf_map_update_elem(&trace_info_heap, &trace_key, &trace_info, BPF_NOEXIST);
  785. // bpf_map_update_elem(&fd_trace_info_heap, &fd_trace_key, &trace_info, BPF_NOEXIST);
  786. bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, e, sizeof(*e));
  787. cw_bpf_debug("[Receive][HTTP] to user space");
  788. // 作为服务端统计 bytes_received 使用
  789. // struct connection *accept_conn = bpf_map_lookup_elem(&active_accepts, &cid);
  790. // cw_bpf_debug("socket accept bytes_received cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  791. // if (accept_conn && !is_tls){
  792. // cw_bpf_debug("socket accept bytes_received after cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  793. // __sync_fetch_and_add(&accept_conn->bytes_received, total_size);
  794. // }
  795. //bpf_tail_call PROGUP(l7_http_request)
  796. cw_bpf_debug("======== PROG_DATA_L7_HTTP_TRACE_ID_UP_IDX ========== __KERNEL_FROM < 512 pid:[%d] ",tid);
  797. if (tp_tail_call && proc_info->code_type != CodeTypeGo) {
  798. bpf_tail_call(ctx, &NAME(progs_jmp_tp_map), PROG_DATA_L7_HTTP_TRACE_ID_TP_IDX);
  799. }
  800. return 0;
  801. }
  802. struct connection *conn = bpf_map_lookup_elem(&active_connections, &cid);
  803. if (args && !conn) {
  804. bpf_map_delete_elem(&active_reads, &id);
  805. // struct connection *accept_conn = bpf_map_lookup_elem(&active_accepts, &cid);
  806. // cw_bpf_debug("socket accept bytes_received cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  807. // if (accept_conn && !is_tls){
  808. // cw_bpf_debug("socket accept bytes_received after cid.pid=%d, cid.fd=%d\n", cid.pid, cid.fd);
  809. // __sync_fetch_and_add(&accept_conn->bytes_received, total_size);
  810. // }
  811. return 0;
  812. }
  813. //TODO 5 同发送逻辑。
  814. /// coroot 是以客户端为主体做统计的,所以这里是客户端逻辑
  815. if (!is_tls) {
  816. __sync_fetch_and_add(&conn->bytes_received, total_size);
  817. if(conn->first_read_time == 0){
  818. conn->first_read_time = bpf_ktime_get_ns();
  819. }
  820. conn->new_read_time = bpf_ktime_get_ns();
  821. }
  822. if (is_rabbitmq_consume(payload, ret)) {
  823. e->protocol = PROTOCOL_RABBITMQ;
  824. e->method = METHOD_CONSUME;
  825. send_event(ctx, e, cid, conn);
  826. return 0;
  827. }
  828. if (nats_method(payload, ret) == METHOD_CONSUME) {
  829. e->protocol = PROTOCOL_NATS;
  830. e->method = METHOD_CONSUME;
  831. send_event(ctx, e, cid, conn);
  832. return 0;
  833. }
  834. struct l7_request *req = bpf_map_lookup_elem(&active_l7_requests, &k);
  835. int response = 0;
  836. if (!req) {
  837. // cw_bpf_debug("no req? 6:[0x%x] k.pid:%d, k.fd:%d",b[4],k.pid,k.fd);
  838. if (is_dns_response(payload, ret, &k.stream_id, &e->status)) {
  839. // cw_bpf_debug("dns");
  840. req = bpf_map_lookup_elem(&active_l7_requests, &k);
  841. if (!req) {
  842. return 0;
  843. }
  844. e->protocol = PROTOCOL_DNS;
  845. e->start_at = req->ns;
  846. e->end_at = bpf_ktime_get_ns();
  847. e->duration = e->end_at - req->ns;
  848. e->payload_size = ret;
  849. e->trace_id = get_apm_trace_id(pid, tid);
  850. COPY_PAYLOAD(e->payload, ret, payload);
  851. send_event(ctx, e, cid, conn);
  852. bpf_map_delete_elem(&active_l7_requests, &k);
  853. return 0;
  854. } else if (is_cassandra_response(payload, ret, &k.stream_id, &e->status)) {
  855. // bpf_printk("[cassandra end] fd(%d) k.stream_id(%d) goid(%d)", k.fd,k.stream_id,get_current_goroutine());
  856. req = bpf_map_lookup_elem(&active_l7_requests, &k);
  857. if (!req) {
  858. return 0;
  859. }
  860. e->trace_id = req->trace_id;
  861. response = 1;
  862. } else {
  863. // cw_bpf_debug("bb 6:[0x%x] k.pid:%d, k.fd:%d",b[4],k.pid,k.fd);
  864. return 0;
  865. }
  866. }
  867. e->protocol = req->protocol;
  868. e->payload_size = req->payload_size;
  869. COPY_PAYLOAD(e->payload, req->payload_size, req->payload);
  870. bpf_map_delete_elem(&active_l7_requests, &k);
  871. // cw_bpf_debug("delete req--------:[0x%x] k.pid:%d, k.fd:%d",b[4],k.pid,k.fd);
  872. if (e->protocol == PROTOCOL_HTTP) {
  873. // __u64 trace_id = req->trace_id;
  874. e->trace_id = req->trace_id;
  875. cw_bpf_debug("l7.c addr is --------:%d,%s",conn->sport,conn->saddr);
  876. e->component_sport = conn->sport;
  877. e->component_dport = conn->dport;
  878. __builtin_memcpy(&e->component_saddr, &conn->saddr, sizeof(e->component_saddr));
  879. __builtin_memcpy(&e->component_daddr, &conn->daddr, sizeof(e->component_daddr));
  880. // struct apm_span_context * sc = cw_get_current_tracking_span();
  881. // if (sc) {
  882. cw_copy_byte_arrays(req->assumed_app_id, e->assumed_app_id, APM_ASSUMED_APP_ID_SIZE);
  883. cw_copy_byte_arrays(req->span_id, e->span_id, APM_SPAN_ID_SIZE);
  884. // for (int i = 0; i < APM_ASSUMED_APP_ID_SIZE; i++) {
  885. // cw_bpf_debug("assumed_app_id-assumed_app_id[%d] = %02x", i, req->assumed_app_id[i]);
  886. // }
  887. // for (int i = 0; i < APM_SPAN_ID_SIZE; i++) {
  888. // cw_bpf_debug("cw_get_current_tracking_span-span_id[%d] = %02x", i, req->span_id[i]);
  889. // }
  890. // }
  891. // cw_bpf_debug("[Response][HTTP222]:thread_id:%d|type:%s|FD:%d\n",k.pid,"",k.fd);
  892. // cw_bpf_debug("[Response][HTTP222] trace_id:%llu", trace_id);
  893. // // 请求报文
  894. // cw_bpf_debug("[Response][HTTP222] req-payload:%s",e->payload);
  895. // // 响应报文
  896. // cw_bpf_debug("[Response][HTTP222] resp-payload:%s",payload);
  897. response = is_http_response(payload, &e->status);
  898. // cw_bpf_debug("[Kernel End][HTTP]:pid:[%d]|CURRENT-GOID:[%llu]|trace_id:[%llu]---------\n", tid, get_current_goroutine(),e->trace_id);
  899. } else if (e->protocol == PROTOCOL_POSTGRES) {
  900. // __u64 trace_id = get_apm_trace_id(pid, tid);
  901. // cw_bpf_debug("[postgres sql] trace_id:%llu", trace_id);
  902. // e->trace_id = req->trace_id;
  903. e->component_sport = conn->sport;
  904. e->component_dport = conn->dport;
  905. __builtin_memcpy(&e->component_saddr, &conn->saddr, sizeof(e->component_saddr));
  906. __builtin_memcpy(&e->component_daddr, &conn->daddr, sizeof(e->component_daddr));
  907. response = is_postgres_response(payload, ret, &e->status);
  908. if (req->request_type == POSTGRES_FRAME_PARSE) {
  909. e->method = METHOD_STATEMENT_PREPARE;
  910. }
  911. } else if (e->protocol == PROTOCOL_REDIS) {
  912. cw_bpf_debug("[Response][Redis]:TGID:%d|type:%s|FD:%d\n", k.pid, "", k.fd);
  913. // __u64 trace_id = get_apm_trace_id(pid, tid);
  914. // e->trace_id = req->trace_id;
  915. cw_bpf_debug("[Redis] trace_id:%llu", req->trace_id);
  916. e->component_sport = conn->sport;
  917. e->component_dport = conn->dport;
  918. __builtin_memcpy(&e->component_saddr, &conn->saddr, sizeof(e->component_saddr));
  919. __builtin_memcpy(&e->component_daddr, &conn->daddr, sizeof(e->component_daddr));
  920. response = is_redis_response(payload, ret, &e->status, e->error_message);
  921. } else if (e->protocol == PROTOCOL_MEMCACHED) {
  922. cw_bpf_debug("[Response][MEMCACHE]:thread_id:%d\n", tid);
  923. e->component_sport = conn->sport;
  924. e->component_dport = conn->dport;
  925. __builtin_memcpy(&e->component_saddr, &conn->saddr, sizeof(e->component_saddr));
  926. __builtin_memcpy(&e->component_daddr, &conn->daddr, sizeof(e->component_daddr));
  927. response = is_memcached_response(payload, ret, &e->status, e->error_message);
  928. } else if (e->protocol == PROTOCOL_MYSQL) {
  929. cw_bpf_debug("[Response][Mysql]:thread_id:%d\n", tid);
  930. // __u64 trace_id = get_apm_trace_id(pid, tid);
  931. // cw_bpf_debug("[Mysql] trace_id:%llu", trace_id);
  932. // e->trace_id = trace_id;
  933. //response package parsing partial data (such as by header)
  934. if(ret == MYSQL_PACKAGE_HEADER_LEN) {
  935. //sava header to ctx and return
  936. char resp_packet_header[MYSQL_PACKAGE_HEADER_LEN];
  937. bpf_probe_read(resp_packet_header,MYSQL_PACKAGE_HEADER_LEN, payload);
  938. bpf_map_update_elem(&active_l7_requests_mysql_resp_header_ctx, &k, resp_packet_header, BPF_ANY);
  939. bpf_map_update_elem(&active_l7_requests, &k, req, BPF_ANY);
  940. return 0 ;
  941. }
  942. e->component_sport = conn->sport;
  943. e->component_dport = conn->dport;
  944. __builtin_memcpy(&e->component_saddr, &conn->saddr, sizeof(e->component_saddr));
  945. __builtin_memcpy(&e->component_daddr, &conn->daddr, sizeof(e->component_daddr));
  946. char* resp_packet_header = bpf_map_lookup_elem(&active_l7_requests_mysql_resp_header_ctx, &k);
  947. if(resp_packet_header) {
  948. char resp_combined_packet[5];
  949. bpf_probe_read(resp_combined_packet,MYSQL_PACKAGE_HEADER_LEN, resp_packet_header);
  950. bpf_probe_read(&resp_combined_packet[4],1, payload);
  951. response = is_mysql_response(resp_combined_packet,sizeof(resp_combined_packet), req->request_type, &e->statement_id, &e->status, e->error_message);
  952. if(response) {
  953. bpf_map_delete_elem(&active_l7_requests_mysql_resp_header_ctx, &k);
  954. }
  955. } else {
  956. response = is_mysql_response(payload, ret, req->request_type, &e->statement_id, &e->status, e->error_message);
  957. }
  958. if (req->request_type == MYSQL_COM_STMT_PREPARE) {
  959. e->method = METHOD_STATEMENT_PREPARE;
  960. }
  961. } else if (e->protocol == PROTOCOL_DM) {
  962. // cw_bpf_debug("[Response][DM] start -------->");
  963. struct l7_request_dm_ctx *dm_ctx = bpf_map_lookup_elem(&active_l7_requests_dm_ctx, &k);
  964. if (!dm_ctx) {
  965. return 0;
  966. }
  967. //获取server端的字符集类型(用来编码数据库名称,暂时不用)
  968. // if(req->request_type == DM_VERSION_CLI) {
  969. // __u8 srv_encode_buf[4];
  970. // bpf_read(payload+DM_SVR_ENCODING_OFFSET, srv_encode_buf);
  971. //
  972. // cw_bpf_debug(" srv_encode_buf:[0x%x]",srv_encode_buf[0]);
  973. // cw_bpf_debug(" srv_encode_buf:[0x%x]",srv_encode_buf[1]);
  974. // cw_bpf_debug(" srv_encode_buf:[0x%x]",srv_encode_buf[2]);
  975. // cw_bpf_debug(" srv_encode_buf:[0x%x]",srv_encode_buf[3]);
  976. // dm_ctx->srv_encoding = (__s32)(srv_encode_buf[0]& 0xff) | ((__s32)srv_encode_buf[1]&0xff) << 8 | ((__s32)srv_encode_buf[2]&0xff) << 16 | ((__s32)srv_encode_buf[3]&0xff) << 24 ;
  977. // cw_bpf_debug(" srv_encode_buf:[%d]",dm_ctx->srv_encoding);
  978. // bpf_map_update_elem(&active_l7_requests_dm_ctx, &k, dm_ctx, BPF_ANY);
  979. // return 0;
  980. // }
  981. //获取数据库名称(暂时不用)
  982. // if(req->request_type == DM_DO_LOGIN ) {
  983. //// //todo 拷贝payload 不进行处理,留给用户空间处理
  984. //// //pack_type == DM_LOGIN_ACK
  985. // if (ret == DM_HEADER_SIZE) {
  986. // bpf_map_update_elem(&active_l7_requests, &k, req, BPF_ANY);
  987. // return 0;
  988. // }
  989. //// //获取数据库名称
  990. // // bpf_read(buf+DM_PAYLOAD_OFFSET, svr_encode_buf);
  991. // __u8 tmp_1[4];
  992. // // __u32 tmp_offset = DM_PAYLOAD_OFFSET ;
  993. // __u32 tmp_offset = 0 ;
  994. // //1
  995. // bpf_read(payload+tmp_offset, tmp_1);
  996. // __s32 tmp_len = (__s32)(tmp_1[0]& 0xff) | ((__s32)tmp_1[1]&0xff) << 8 | ((__s32)tmp_1[2]&0xff) << 16 | ((__s32)tmp_1[3]&0xff) << 24 ;
  997. // tmp_offset = tmp_offset + 4 ;
  998. // tmp_offset = tmp_offset+tmp_len ;
  999. // cw_bpf_debug("===== DM_LOGIN_ACK ----1 tmp_len:%d",tmp_len);
  1000. //
  1001. // //2
  1002. // bpf_read(payload+tmp_offset,tmp_1);
  1003. // tmp_len = (__s32)(tmp_1[0]& 0xff) | ((__s32)tmp_1[1]&0xff) << 8 | ((__s32)tmp_1[2]&0xff) << 16 | ((__s32)tmp_1[3]&0xff) << 24 ;
  1004. // tmp_offset = tmp_offset + 4 ;
  1005. // tmp_offset = tmp_offset+tmp_len ;
  1006. // cw_bpf_debug("===== DM_LOGIN_ACK ----2 tmp_len:%d",tmp_len);
  1007. // //3
  1008. // bpf_read(payload+tmp_offset,tmp_1);
  1009. // tmp_len = (__s32)(tmp_1[0]& 0xff) | ((__s32)tmp_1[1]&0xff) << 8 | ((__s32)tmp_1[2]&0xff) << 16 | ((__s32)tmp_1[3]&0xff) << 24 ;
  1010. // tmp_offset = tmp_offset + 4 ;
  1011. // tmp_offset = tmp_offset+tmp_len ;
  1012. // cw_bpf_debug("===== DM_LOGIN_ACK ----3 tmp_len:%d",tmp_len);
  1013. // //4
  1014. // bpf_read(payload+tmp_offset,tmp_1);
  1015. // tmp_len = (__s32)(tmp_1[0]& 0xff) | ((__s32)tmp_1[1]&0xff) << 8 | ((__s32)tmp_1[2]&0xff) << 16 | ((__s32)tmp_1[3]&0xff) << 24 ;
  1016. // tmp_offset = tmp_offset + 4 ;
  1017. // tmp_offset = tmp_offset+tmp_len ;
  1018. // cw_bpf_debug("===== DM_LOGIN_ACK ----4 tmp_len:%d",tmp_len);
  1019. // //5
  1020. // bpf_read(payload+tmp_offset,tmp_1);
  1021. // tmp_len = (__s32)(tmp_1[0]& 0xff) | ((__s32)tmp_1[1]&0xff) << 8 | ((__s32)tmp_1[2]&0xff) << 16 | ((__s32)tmp_1[3]&0xff) << 24 ;
  1022. // tmp_offset = tmp_offset + 4 ;
  1023. // tmp_offset = tmp_offset+tmp_len ;
  1024. // cw_bpf_debug("===== DM_LOGIN_ACK ----5 tmp_len:%d",tmp_len);
  1025. // //6
  1026. // bpf_read(payload+tmp_offset,tmp_1);
  1027. // tmp_len = (__s32)(tmp_1[0]& 0xff) | ((__s32)tmp_1[1]&0xff) << 8 | ((__s32)tmp_1[2]&0xff) << 16 | ((__s32)tmp_1[3]&0xff) << 24 ;
  1028. // tmp_offset = tmp_offset + 4 ;
  1029. // tmp_offset = tmp_offset+tmp_len ;
  1030. // cw_bpf_debug("===== DM_LOGIN_ACK ----6 tmp_len:%d",tmp_len);
  1031. // //7
  1032. // bpf_read(payload+tmp_offset,tmp_1);
  1033. // tmp_len = (__s32)(tmp_1[0]& 0xff) | ((__s32)tmp_1[1]&0xff) << 8 | ((__s32)tmp_1[2]&0xff) << 16 | ((__s32)tmp_1[3]&0xff) << 24 ;
  1034. // tmp_offset = tmp_offset + 4 ;
  1035. // tmp_offset = tmp_offset+tmp_len ;
  1036. // cw_bpf_debug("===== DM_LOGIN_ACK ----7 tmp_len:%d",tmp_len);
  1037. // //8
  1038. // bpf_read(payload+tmp_offset,tmp_1);
  1039. // tmp_len = (__s32)(tmp_1[0]& 0xff) | ((__s32)tmp_1[1]&0xff) << 8 | ((__s32)tmp_1[2]&0xff) << 16 | ((__s32)tmp_1[3]&0xff) << 24 ;
  1040. // tmp_offset = tmp_offset + 4 ;
  1041. // tmp_offset = tmp_offset+tmp_len ;
  1042. // cw_bpf_debug("===== DM_LOGIN_ACK ----8 tmp_len:%d",tmp_len);
  1043. //
  1044. // //9
  1045. // tmp_offset = tmp_offset + 4 ;
  1046. // //10
  1047. // tmp_offset = tmp_offset + 4 ;
  1048. // //11
  1049. // tmp_offset = tmp_offset + 4 ;
  1050. // //12
  1051. // bpf_read(payload+tmp_offset,tmp_1);
  1052. // tmp_len = (__s32)(tmp_1[0]& 0xff) | ((__s32)tmp_1[1]&0xff) << 8 | ((__s32)tmp_1[2]&0xff) << 16 | ((__s32)tmp_1[3]&0xff) << 24 ;
  1053. // tmp_offset = tmp_offset + 4 ;
  1054. // tmp_offset = tmp_offset+tmp_len ;
  1055. // cw_bpf_debug("===== DM_LOGIN_ACK ----12 tmp_len:%d",tmp_len);
  1056. // //13
  1057. // bpf_read(payload+tmp_offset,tmp_1);
  1058. // tmp_offset = tmp_offset + 4 ;
  1059. // tmp_len = (__s32)(tmp_1[0]& 0xff) | ((__s32)tmp_1[1]&0xff) << 8 | ((__s32)tmp_1[2]&0xff) << 16 | ((__s32)tmp_1[3]&0xff) << 24 ;
  1060. // cw_bpf_debug("===== DM_LOGIN_ACK ----13 tmp_len:%d",tmp_len);
  1061. //
  1062. // cw_bpf_debug("===== DM_LOGIN_ACK dbName tmp_offset:%d",tmp_offset);
  1063. // if(tmp_len>0){
  1064. //// char dbNameBuff[7] ;
  1065. // char dbNameBuff[128] ;
  1066. // if(tmp_len > 128){
  1067. // tmp_len = 128;
  1068. // }
  1069. // bpf_read(payload+tmp_offset,dbNameBuff);
  1070. // dbNameBuff[tmp_len-1]='\0';
  1071. //// cw_bpf_debug("===== DM-resp DM_LOGIN_ACK dbName[0]:%c",dbNameBuff[0]);
  1072. //// cw_bpf_debug("===== DM-resp DM_LOGIN_ACK dbName[1]:%c",dbNameBuff[1]);
  1073. //// cw_bpf_debug("===== DM-resp DM_LOGIN_ACK dbName[2]:%c",dbNameBuff[2]);
  1074. //// cw_bpf_debug("===== DM-resp DM_LOGIN_ACK dbName[3]:%c",dbNameBuff[3]);
  1075. //// cw_bpf_debug("===== DM-resp DM_LOGIN_ACK dbName[4]:%c",dbNameBuff[4]);
  1076. //// cw_bpf_debug("===== DM-resp DM_LOGIN_ACK dbName[5]:%c",dbNameBuff[5]);
  1077. // cw_bpf_debug("===== DM_LOGIN_ACK dbName:%s",dbNameBuff);
  1078. //
  1079. // __u32 dn_name_size = (__u32)tmp_len + 1 ;
  1080. // COPY_PAYLOAD(dm_ctx->db_name,dn_name_size, dbNameBuff);
  1081. // cw_bpf_debug("===== DM_LOGIN_ACK strcpy(db_name,dbNameBuff):%s",dm_ctx->db_name);
  1082. // bpf_map_update_elem(&active_l7_requests_dm_ctx, &k, dm_ctx, BPF_ANY);
  1083. // }
  1084. // return 0;
  1085. // }
  1086. // __u64 trace_id = get_apm_trace_id(pid, tid);
  1087. // e->trace_id = req->trace_id;
  1088. e->component_sport = conn->sport;
  1089. e->component_dport = conn->dport;
  1090. __builtin_memcpy(&e->component_saddr, &conn->saddr, sizeof(e->component_saddr));
  1091. __builtin_memcpy(&e->component_daddr, &conn->daddr, sizeof(e->component_daddr));
  1092. // cw_bpf_debug("[Response][DM] trace_id:%llu", trace_id);
  1093. response = is_dm_response(payload, ret, req->request_type, &dm_ctx->status);
  1094. // cw_bpf_debug("[Response][DM] is_dm_response status ---------- %d",dm_ctx->status);
  1095. if (response) {
  1096. req->ns = dm_ctx->req_start_at_ns;
  1097. e->status = dm_ctx->status;
  1098. e->payload_size = dm_ctx->query_sql_payload_size;
  1099. COPY_PAYLOAD(e->payload, dm_ctx->query_sql_payload_size, dm_ctx->query_sql_payload);
  1100. bpf_map_delete_elem(&active_l7_requests_dm_ctx, &k);
  1101. // cw_bpf_debug("[Response][DM] is response ,delete active_l7_requests_dm_ctx -- req->request_type<0x%x> , e->payload_size:[%d]",req->request_type,e->payload_size);
  1102. }
  1103. // else {
  1104. // cw_bpf_debug("[Response][DM] not response req->request_type <0x%x>",req->request_type);
  1105. // }
  1106. // cw_bpf_debug("[Response][DM] end <---------\n");
  1107. } else if (e->protocol == PROTOCOL_MONGO) {
  1108. response = is_mongo_response(payload, ret, req->partial);
  1109. if (response == 2) { // partial
  1110. struct l7_request *r = bpf_map_lookup_elem(&l7_request_heap, &zero);
  1111. if (!r) {
  1112. return 0;
  1113. }
  1114. r->partial = 1;
  1115. r->protocol = e->protocol;
  1116. r->ns = req->ns;
  1117. r->payload_size = req->payload_size;
  1118. COPY_PAYLOAD(r->payload, req->payload_size, req->payload);
  1119. bpf_map_update_elem(&active_l7_requests, &k, r, BPF_ANY);
  1120. return 0;
  1121. }
  1122. } else if (e->protocol == PROTOCOL_KAFKA) {
  1123. response = is_kafka_response(payload, req->request_id);
  1124. } else if (e->protocol == PROTOCOL_DUBBO2) {
  1125. response = is_dubbo2_response(payload, &e->status);
  1126. }
  1127. if (!response) {
  1128. return 0;
  1129. }
  1130. if (e->trace_id == 0){
  1131. e->trace_id = get_apm_trace_id(pid,tid);
  1132. }
  1133. e->end_at = bpf_ktime_get_ns();
  1134. e->start_at = req->ns;
  1135. e->duration = e->end_at - e->start_at;
  1136. SET_TRACE_METHOD(e);
  1137. send_event(ctx, e, cid, conn);
  1138. return 0;
  1139. }
  1140. static inline __attribute__((__always_inline__))
  1141. int trace_exit_read_tp(void *ctx, __u64 id, __u32 pid, __u16 is_tls, long int ret) {
  1142. return trace_exit_read_common(ctx, id, pid, is_tls, ret, 1); // enable_tail_call = 1
  1143. }
  1144. static inline __attribute__((__always_inline__))
  1145. int trace_exit_read_up(void *ctx, __u64 id, __u32 pid, __u16 is_tls, long int ret) {
  1146. return trace_exit_read_common(ctx, id, pid, is_tls, ret, 0); // enable_tail_call = 0
  1147. }
  1148. SEC("tracepoint/syscalls/sys_enter_write")
  1149. int sys_enter_write(struct trace_event_raw_sys_enter_rw__stub* ctx) {
  1150. return trace_enter_write(ctx, ctx->fd, 0, ctx->buf, ctx->size, 0);
  1151. }
  1152. SEC("tracepoint/syscalls/sys_enter_writev")
  1153. int sys_enter_writev(struct trace_event_raw_sys_enter_rw__stub* ctx) {
  1154. return trace_enter_write(ctx, ctx->fd, 0, ctx->buf, 0, ctx->size);
  1155. }
  1156. SEC("tracepoint/syscalls/sys_enter_sendmsg")
  1157. int sys_enter_sendmsg(struct trace_event_raw_sys_enter_rw__stub* ctx) {
  1158. struct l7_user_msghdr msghdr = {};
  1159. if (bpf_probe_read(&msghdr, sizeof(msghdr), (void *)ctx->buf)) {
  1160. return 0;
  1161. }
  1162. return trace_enter_write(ctx, ctx->fd, 0, (char*)msghdr.msg_iov, 0, msghdr.msg_iovlen);
  1163. }
  1164. //struct cw_mmsghdr {
  1165. // struct user_msghdr msg_hdr;
  1166. // __u32 msg_len;
  1167. //};
  1168. SEC("tracepoint/syscalls/sys_enter_sendmmsg")
  1169. int sys_enter_sendmmsg(struct trace_event_raw_sys_enter_rw__stub* ctx) {
  1170. __u64 offset = 0;
  1171. #pragma unroll
  1172. for (int i = 0; i <= 1; i++) {
  1173. if (i >= ctx->size) {
  1174. break;
  1175. }
  1176. struct mmsghdr h = {};
  1177. if (bpf_probe_read(&h , sizeof(h), (void *)(ctx->buf + offset))) {
  1178. return 0;
  1179. }
  1180. offset += sizeof(h);
  1181. trace_dns_enter_write(ctx, ctx->fd, 0, (char*)h.msg_hdr.msg_iov, 0, h.msg_hdr.msg_iovlen);
  1182. }
  1183. return 0;
  1184. }
  1185. SEC("tracepoint/syscalls/sys_enter_sendto")
  1186. int sys_enter_sendto(struct trace_event_raw_sys_enter_rw__stub* ctx) {
  1187. return trace_enter_write(ctx, ctx->fd, 0, ctx->buf, ctx->size, 0);
  1188. }
  1189. SEC("tracepoint/syscalls/sys_enter_read")
  1190. int sys_enter_read(struct trace_event_raw_sys_enter_rw__stub* ctx) {
  1191. __u64 id = bpf_get_current_pid_tgid();
  1192. __u32 pid = id >> 32;
  1193. return trace_enter_read(id, pid, ctx->fd, ctx->buf, 0, 0);
  1194. }
  1195. SEC("tracepoint/syscalls/sys_enter_readv")
  1196. int sys_enter_readv(struct trace_event_raw_sys_enter_rw__stub* ctx) {
  1197. __u64 id = bpf_get_current_pid_tgid();
  1198. __u32 pid = id >> 32;
  1199. return trace_enter_read(id, pid, ctx->fd, ctx->buf, 0, ctx->size);
  1200. }
  1201. SEC("tracepoint/syscalls/sys_enter_recvmsg")
  1202. int sys_enter_recvmsg(struct trace_event_raw_sys_enter_rw__stub* ctx) {
  1203. __u64 id = bpf_get_current_pid_tgid();
  1204. struct l7_user_msghdr msghdr = {};
  1205. if (bpf_probe_read(&msghdr, sizeof(msghdr), (void *)ctx->buf)) {
  1206. return 0;
  1207. }
  1208. __u32 pid = id >> 32;
  1209. return trace_enter_read(id, pid, ctx->fd, (char *) msghdr.msg_iov, 0, msghdr.msg_iovlen);
  1210. }
  1211. SEC("tracepoint/syscalls/sys_enter_recvfrom")
  1212. int sys_enter_recvfrom(struct trace_event_raw_sys_enter_rw__stub* ctx) {
  1213. __u64 id = bpf_get_current_pid_tgid();
  1214. __u32 pid = id >> 32;
  1215. return trace_enter_read(id, pid, ctx->fd, ctx->buf, 0, 0);
  1216. }
  1217. SEC("tracepoint/syscalls/sys_exit_read")
  1218. int sys_exit_read(struct trace_event_raw_sys_exit_rw__stub* ctx) {
  1219. __u64 pid_tgid = bpf_get_current_pid_tgid();
  1220. __u32 pid = pid_tgid >> 32;
  1221. return trace_exit_read_tp(ctx, pid_tgid, pid, 0, ctx->ret);
  1222. }
  1223. SEC("tracepoint/syscalls/sys_exit_readv")
  1224. int sys_exit_readv(struct trace_event_raw_sys_exit_rw__stub* ctx) {
  1225. __u64 pid_tgid = bpf_get_current_pid_tgid();
  1226. __u32 pid = pid_tgid >> 32;
  1227. return trace_exit_read_tp(ctx, pid_tgid, pid, 0, ctx->ret);
  1228. }
  1229. SEC("tracepoint/syscalls/sys_exit_recvmsg")
  1230. int sys_exit_recvmsg(struct trace_event_raw_sys_exit_rw__stub* ctx) {
  1231. __u64 pid_tgid = bpf_get_current_pid_tgid();
  1232. __u32 pid = pid_tgid >> 32;
  1233. return trace_exit_read_tp(ctx, pid_tgid, pid, 0, ctx->ret);
  1234. }
  1235. SEC("tracepoint/syscalls/sys_exit_recvfrom")
  1236. int sys_exit_recvfrom(struct trace_event_raw_sys_exit_rw__stub* ctx) {
  1237. __u64 pid_tgid = bpf_get_current_pid_tgid();
  1238. __u32 pid = pid_tgid >> 32;
  1239. return trace_exit_read_tp(ctx, pid_tgid, pid, 0, ctx->ret);
  1240. }
  1241. //
  1242. //SEC("tracepoint/syscalls/sys_exit_recvfrom")
  1243. //int sys_exit_recvfrom222(struct trace_event_raw_sys_exit_rw__stub* ctx) {
  1244. // __u64 pid_tgid = bpf_get_current_pid_tgid();
  1245. // __u32 pid = pid_tgid >> 32;
  1246. // if (pid == filterPid) {
  1247. // cw_bpf_debug("sys_exit_recvfrom222");
  1248. // }
  1249. // return 0;
  1250. //}
  1251. //bpf_prog_tp__l7_http_trace_id
  1252. PROGTP(l7_http_trace_id)(void * ctx){
  1253. int zero1 = 0;
  1254. struct l7_request *req = bpf_map_lookup_elem(&l7_request_heap, &zero1);
  1255. if (!req) {
  1256. return 0;
  1257. }
  1258. // ---------- 在http请求入口生成 横向串联的trace_id start ----------
  1259. __u32 key = 0;
  1260. __u32 offset = has_cw_header(req->payload);
  1261. struct apm_span_context *cw_parent_span_context = bpf_map_lookup_elem(&apm_span_context_heap3, &key);
  1262. if (cw_parent_span_context == NULL) {
  1263. return -1;
  1264. }
  1265. __builtin_memset(cw_parent_span_context, 0, sizeof(struct apm_span_context));
  1266. if (offset > 0) {
  1267. cw_string_to_span_context(&req->payload[offset], cw_parent_span_context);
  1268. } else {
  1269. generate_random_bytes(cw_parent_span_context->trace_id, TRACE_ID_SIZE);
  1270. }
  1271. // 保存 trace_id 到psc
  1272. cw_save_parent_tracking_span(cw_parent_span_context);
  1273. cw_bpf_debug("[Trace Start in l7][HTTP] trace_id:[%llu]\n", cw_parent_span_context->trace_id);
  1274. // ---------- 在http请求入口生成 横向串联的trace_id end ----------
  1275. return 0;
  1276. }