|
|
@@ -249,215 +249,56 @@ __u64 read_iovec(char *l7_iovec, __u64 iovlen, __u64 ret, char *buf) {
|
|
|
}
|
|
|
|
|
|
static inline __attribute__((__always_inline__))
|
|
|
-int rock_trace_enter_write(void *ctx, char *buf, __u64 size, __u64 iovlen) {
|
|
|
- __u64 id = bpf_get_current_pid_tgid();
|
|
|
- __u32 zero = 0;
|
|
|
- __u32 pid, tid;
|
|
|
- __u32 http_status;
|
|
|
- struct trace_event_raw_sys_enter_rw__stub* tctx = (struct trace_event_raw_sys_enter_rw__stub*)ctx;
|
|
|
- __u64 fd = tctx->fd;
|
|
|
-
|
|
|
- pid = id >> 32;
|
|
|
- tid = (__u32)id;
|
|
|
- if (load_filter_pid() != 0 && pid != load_filter_pid()) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- char* payload = buf;
|
|
|
- if (iovlen) {
|
|
|
- payload = bpf_map_lookup_elem(&iovec_buf_heap, &zero);
|
|
|
- if (!payload) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- size = read_iovec(buf, iovlen, 0, payload);
|
|
|
- }
|
|
|
- if (!size) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- struct l7_request *req = bpf_map_lookup_elem(&l7_request_heap, &zero);
|
|
|
- if (!req) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- req->protocol = PROTOCOL_UNKNOWN;
|
|
|
- req->partial = 0;
|
|
|
- req->request_id = 0;
|
|
|
- req->ns = 0;
|
|
|
- req->payload_size = size;
|
|
|
- struct l7_request_key k = {};
|
|
|
- k.pid = id >> 32;
|
|
|
- k.fd = fd;
|
|
|
- k.is_tls = 0;
|
|
|
- k.stream_id = -1;
|
|
|
-
|
|
|
-// cw_bpf_debug("enter-payload:%s|type:%s|FD:%d\n",payload,"type",k.fd);
|
|
|
-
|
|
|
- if (is_http_response(payload, &http_status))
|
|
|
- {
|
|
|
-// __u64 trace_id = get_apm_trace_id(pid, fd);
|
|
|
- __u64 trace_id = get_fd_trace_id(pid, fd);
|
|
|
- cw_bpf_debug("[Trace End in l7][Response][HTTP] pid:%d,fd:%d,trace_id:%llu", tid, fd, trace_id);
|
|
|
- // 清除trace信息
|
|
|
- cw_clear_trace(pid, tid, fd);
|
|
|
- // 发送事件到用户空间 start
|
|
|
- struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
|
|
|
- if (!e) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- struct l7_request *req = bpf_map_lookup_elem(&active_l7_requests, &k);
|
|
|
- if (!req)
|
|
|
- {
|
|
|
-// cw_bpf_debug("[Response][HTTP]:no req-----------");
|
|
|
-// cw_bpf_debug("[Response][HTTP]:pid:%d|tid:%d",k.pid,k.fd);
|
|
|
-// cw_bpf_debug("[Response][HTTP]:is_tls:%d|tid:%d",k.is_tls,k.stream_id);
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- e->duration = bpf_ktime_get_ns() - req->ns;
|
|
|
-// cw_bpf_debug("[Response][HTTP]:duration->ns:%d\n",e->duration);
|
|
|
- e->protocol = PROTOCOL_TRACE;
|
|
|
- e->status = http_status;
|
|
|
- e->pid = k.pid;
|
|
|
- e->fd = k.fd;
|
|
|
- // e->connection_timestamp = get_connection_timestamp(k.pid, k.fd);
|
|
|
- e->trace_start = 0;
|
|
|
- e->trace_end = 1;
|
|
|
- e->trace_id = trace_id;
|
|
|
- e->payload_size = size;
|
|
|
- COPY_PAYLOAD(e->payload, size, payload);
|
|
|
- // psc
|
|
|
- struct apm_span_context *cw_psc = cw_get_parent_tracking_span();
|
|
|
- if(cw_psc){
|
|
|
- cw_copy_byte_arrays(cw_psc->trace_id, e->trace_id_from, APM_TRACE_ID_SIZE);
|
|
|
- cw_copy_byte_arrays(cw_psc->assumed_app_id, e->called_id, APM_ASSUMED_APP_ID_SIZE);
|
|
|
- cw_copy_byte_arrays(cw_psc->instance_id, e->instance_id_from, APM_INSTANCE_ID_SIZE);
|
|
|
- cw_copy_byte_arrays(cw_psc->app_id, e->app_id_from, APM_APP_ID_SIZE);
|
|
|
- cw_copy_byte_arrays(cw_psc->span_id, e->span_id_from, APM_SPAN_ID_SIZE);
|
|
|
-// for (int i = 0; i < APM_TRACE_ID_SIZE; i++) {
|
|
|
-// bpf_printk("trace_enter_write - trace_id = %02x", e->trace_id_from[i]);
|
|
|
-// }
|
|
|
- }
|
|
|
- bpf_map_delete_elem(&active_l7_requests, &k);
|
|
|
- bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, e, sizeof(*e));
|
|
|
- // 发送事件到用户空间 end
|
|
|
-// __u64 k_version = load_filter_pid();
|
|
|
-// cw_bpf_debug("filter_pid:%d", k_version);
|
|
|
-//
|
|
|
-// struct test_t *ttt = bpf_map_lookup_elem(&test_heap, &zero);
|
|
|
-// if (!ttt) {
|
|
|
-// return 0;
|
|
|
-// }
|
|
|
-
|
|
|
-// struct member_fields_offset *off = bpf_map_lookup_elem(&__members_offset, &zero);
|
|
|
-// if (!off) {
|
|
|
-// return 0;
|
|
|
-// }
|
|
|
-
|
|
|
-// cw_bpf_debug("off->task__files_offset:%x", off->task__files_offset);
|
|
|
-// cw_bpf_debug("e->test_id1111:%d", ttt->test_id);
|
|
|
- cw_bpf_debug("HTTP_END");
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- if (is_http_request(payload)) {
|
|
|
- cw_bpf_debug("");
|
|
|
- cw_bpf_debug("-----[Kernel Enter][HTTP]:pid:[%d]|CURRENT-GOID:[%llu]|FD:[%d]", tid, get_current_goroutine(), k.fd);
|
|
|
-
|
|
|
- req->protocol = PROTOCOL_HTTP;
|
|
|
-// struct apm_trace_key_t trace_key = {0};
|
|
|
-// trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
|
|
|
-// cw_bpf_debug("[Enter] [HTTP]:goid:%llu",trace_key.goid);
|
|
|
-
|
|
|
-
|
|
|
- } else if (is_postgres_query(payload, size, &req->request_type)) {
|
|
|
- if (req->request_type == POSTGRES_FRAME_CLOSE) {
|
|
|
- struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
|
|
|
- if (!e) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- e->protocol = PROTOCOL_POSTGRES;
|
|
|
- e->method = METHOD_STATEMENT_CLOSE;
|
|
|
- e->payload_size = size;
|
|
|
- COPY_PAYLOAD(e->payload, size, payload);
|
|
|
- send_event(ctx, e, k.pid, k.fd);
|
|
|
- return 0;
|
|
|
- }
|
|
|
- req->protocol = PROTOCOL_POSTGRES;
|
|
|
- } else if (is_redis_query(payload, size)) {
|
|
|
- cw_bpf_debug("[Enter][Redis]:TGID:%d|type:%s|FD:%d\n",k.pid,"type",k.fd);
|
|
|
- req->protocol = PROTOCOL_REDIS;
|
|
|
- } else if (is_memcached_query(payload, size)) {
|
|
|
- req->protocol = PROTOCOL_MEMCACHED;
|
|
|
- } else if (is_mysql_query(payload, size, &req->request_type)) {
|
|
|
- cw_bpf_debug("[Enter][Mysql]:thread_id:%d\n",tid);
|
|
|
- if (req->request_type == MYSQL_COM_STMT_CLOSE) {
|
|
|
- struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
|
|
|
- if (!e) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- e->protocol = PROTOCOL_MYSQL;
|
|
|
- e->method = METHOD_STATEMENT_CLOSE;
|
|
|
- e->payload_size = size;
|
|
|
- COPY_PAYLOAD(e->payload, size, payload);
|
|
|
- cw_bpf_debug("[Enter][Mysql][Send]:thread_id:%d\n",tid);
|
|
|
- send_event(ctx, e, k.pid, k.fd);
|
|
|
- return 0;
|
|
|
- }
|
|
|
- req->protocol = PROTOCOL_MYSQL;
|
|
|
- } else if (is_mongo_query(payload, size)) {
|
|
|
- req->protocol = PROTOCOL_MONGO;
|
|
|
- } else if (is_rabbitmq_produce(payload, size)) {
|
|
|
- struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
|
|
|
- if (!e) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- e->protocol = PROTOCOL_RABBITMQ;
|
|
|
- e->method = METHOD_PRODUCE;
|
|
|
- send_event(ctx, e, k.pid, k.fd);
|
|
|
- return 0;
|
|
|
- } else if (nats_method(payload, size) == METHOD_PRODUCE) {
|
|
|
- struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
|
|
|
- if (!e) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- e->protocol = PROTOCOL_NATS;
|
|
|
- e->method = METHOD_PRODUCE;
|
|
|
- send_event(ctx, e, k.pid, k.fd);
|
|
|
- return 0;
|
|
|
- } else if (is_cassandra_request(payload, size, &k.stream_id)) {
|
|
|
- req->protocol = PROTOCOL_CASSANDRA;
|
|
|
- } else if (is_kafka_request(payload, size, &req->request_id)) {
|
|
|
- req->protocol = PROTOCOL_KAFKA;
|
|
|
- struct l7_request *prev_req = bpf_map_lookup_elem(&active_l7_requests, &k);
|
|
|
- if (prev_req && prev_req->protocol == PROTOCOL_KAFKA) {
|
|
|
- req->ns = prev_req->ns;
|
|
|
- }
|
|
|
- } else if (looks_like_http2_frame(payload, size, METHOD_HTTP2_CLIENT_FRAMES)) {
|
|
|
- struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
|
|
|
- if (!e) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- e->protocol = PROTOCOL_HTTP2;
|
|
|
- e->method = METHOD_HTTP2_CLIENT_FRAMES;
|
|
|
- e->duration = bpf_ktime_get_ns();
|
|
|
- e->payload_size = size;
|
|
|
- COPY_PAYLOAD(e->payload, size, payload);
|
|
|
- send_event(ctx, e, k.pid, k.fd);
|
|
|
- return 0;
|
|
|
- } else if (is_dubbo2_request(payload, size)) {
|
|
|
- req->protocol = PROTOCOL_DUBBO2;
|
|
|
- } else if (is_dns_request(payload, size, &k.stream_id)) {
|
|
|
- req->protocol = PROTOCOL_DNS;
|
|
|
- }
|
|
|
+int trace_dns_enter_write(void *ctx, __u64 fd, __u16 is_tls, char *buf, __u64 size, __u64 iovlen) {
|
|
|
+ __u64 id = bpf_get_current_pid_tgid();
|
|
|
+ __u32 zero = 0;
|
|
|
+ __u32 pid, tid;
|
|
|
+ __u32 http_status ;
|
|
|
+
|
|
|
+ pid = id >> 32;
|
|
|
+ tid = (__u32)id;
|
|
|
+ if (load_filter_pid() != 0 && pid != load_filter_pid()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ char* payload = buf;
|
|
|
+ if (iovlen) {
|
|
|
+ payload = bpf_map_lookup_elem(&iovec_buf_heap, &zero);
|
|
|
+ if (!payload) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ size = read_iovec(buf, iovlen, 0, payload);
|
|
|
+ }
|
|
|
+ if (!size) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
|
|
|
- if (req->protocol == PROTOCOL_UNKNOWN) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- if (req->ns == 0) {
|
|
|
- req->ns = bpf_ktime_get_ns();
|
|
|
- }
|
|
|
- COPY_PAYLOAD(req->payload, size, payload);
|
|
|
- bpf_map_update_elem(&active_l7_requests, &k, req, BPF_NOEXIST);
|
|
|
- return 0;
|
|
|
+ struct l7_request *req = bpf_map_lookup_elem(&l7_request_heap, &zero);
|
|
|
+ if (!req) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ req->protocol = PROTOCOL_UNKNOWN;
|
|
|
+ req->partial = 0;
|
|
|
+ req->request_id = 0;
|
|
|
+ req->ns = 0;
|
|
|
+ req->payload_size = size;
|
|
|
+ struct l7_request_key k = {};
|
|
|
+ k.pid = id >> 32;
|
|
|
+ k.fd = fd;
|
|
|
+ k.is_tls = is_tls;
|
|
|
+ k.stream_id = -1;
|
|
|
+
|
|
|
+ if (is_dns_request(payload, size, &k.stream_id)) {
|
|
|
+ req->protocol = PROTOCOL_DNS;
|
|
|
+ }
|
|
|
+ if (req->protocol == PROTOCOL_UNKNOWN) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (req->ns == 0) {
|
|
|
+ req->ns = bpf_ktime_get_ns();
|
|
|
+ }
|
|
|
+ COPY_PAYLOAD(req->payload, size, payload);
|
|
|
+ bpf_map_update_elem(&active_l7_requests, &k, req, BPF_NOEXIST);
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
static inline __attribute__((__always_inline__))
|
|
|
@@ -509,19 +350,29 @@ int trace_enter_write(void *ctx, __u64 fd, __u16 is_tls, char *buf, __u64 size,
|
|
|
cw_bpf_debug("[Trace End in l7][Response][HTTP] pid:%d,fd:%d,trace_id:%llu", tid, fd, trace_id);
|
|
|
cw_bpf_debug("[uprobeThread/pidpidpidpid][Trace End in l7][HTTP]pid:[%d]--[%lld]", tid, bpf_ktime_get_ns());
|
|
|
cw_bpf_debug("[Trace End in l7][Response][HTTP] event_count:%d", event_count);
|
|
|
- // 清除trace信息
|
|
|
- cw_clear_trace(pid, tid, fd);
|
|
|
+
|
|
|
// 发送事件到用户空间 start
|
|
|
struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
|
|
|
if (!e) {
|
|
|
+ cw_clear_trace(pid, tid, fd);
|
|
|
return 0;
|
|
|
}
|
|
|
+ // parent sc
|
|
|
+ struct apm_span_context *cw_psc = cw_get_parent_tracking_span();
|
|
|
+ if(cw_psc){
|
|
|
+ cw_copy_byte_arrays(cw_psc->trace_id, e->trace_id_from, APM_TRACE_ID_SIZE);
|
|
|
+ cw_copy_byte_arrays(cw_psc->assumed_app_id, e->called_id, APM_ASSUMED_APP_ID_SIZE);
|
|
|
+ cw_copy_byte_arrays(cw_psc->instance_id, e->instance_id_from, APM_INSTANCE_ID_SIZE);
|
|
|
+ cw_copy_byte_arrays(cw_psc->app_id, e->app_id_from, APM_APP_ID_SIZE);
|
|
|
+ cw_copy_byte_arrays(cw_psc->span_id, e->span_id_from, APM_SPAN_ID_SIZE);
|
|
|
+// for (int i = 0; i < APM_TRACE_ID_SIZE; i++) {
|
|
|
+// bpf_printk("trace_enter_write - span_id_from = %02x", e->span_id_from[i]);
|
|
|
+// }
|
|
|
+ }
|
|
|
struct l7_request *req = bpf_map_lookup_elem(&active_l7_requests, &k);
|
|
|
if (!req)
|
|
|
{
|
|
|
-// cw_bpf_debug("[Response][HTTP]:no req-----------");
|
|
|
-// cw_bpf_debug("[Response][HTTP]:pid:%d|tid:%d",k.pid,k.fd);
|
|
|
-// cw_bpf_debug("[Response][HTTP]:is_tls:%d|tid:%d",k.is_tls,k.stream_id);
|
|
|
+ cw_clear_trace(pid, tid, fd);
|
|
|
return 0;
|
|
|
}
|
|
|
e->start_at = req->ns;
|
|
|
@@ -540,21 +391,11 @@ int trace_enter_write(void *ctx, __u64 fd, __u16 is_tls, char *buf, __u64 size,
|
|
|
e->payload_size = size;
|
|
|
e->event_count = event_count;
|
|
|
COPY_PAYLOAD(e->payload, size, payload);
|
|
|
- // psc
|
|
|
- struct apm_span_context *cw_psc = cw_get_parent_tracking_span();
|
|
|
- if(cw_psc){
|
|
|
- cw_copy_byte_arrays(cw_psc->trace_id, e->trace_id_from, APM_TRACE_ID_SIZE);
|
|
|
- cw_copy_byte_arrays(cw_psc->assumed_app_id, e->called_id, APM_ASSUMED_APP_ID_SIZE);
|
|
|
- cw_copy_byte_arrays(cw_psc->instance_id, e->instance_id_from, APM_INSTANCE_ID_SIZE);
|
|
|
- cw_copy_byte_arrays(cw_psc->app_id, e->app_id_from, APM_APP_ID_SIZE);
|
|
|
- cw_copy_byte_arrays(cw_psc->span_id, e->span_id_from, APM_SPAN_ID_SIZE);
|
|
|
-// for (int i = 0; i < APM_TRACE_ID_SIZE; i++) {
|
|
|
-// bpf_printk("trace_enter_write - trace_id = %02x", e->trace_id_from[i]);
|
|
|
-// }
|
|
|
- }
|
|
|
bpf_map_delete_elem(&active_l7_requests, &k);
|
|
|
// 清除事件计数
|
|
|
bpf_map_delete_elem(&trace_event_count_heap, &trace_id);
|
|
|
+ // 清除trace信息
|
|
|
+ cw_clear_trace(pid, tid, fd);
|
|
|
bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, e, sizeof(*e));
|
|
|
// 发送事件到用户空间 end
|
|
|
// __u64 k_version = load_filter_pid();
|
|
|
@@ -835,12 +676,15 @@ int trace_exit_read(void *ctx, __u64 id, __u32 pid, __u16 is_tls, long int ret)
|
|
|
int response = 0;
|
|
|
if (!req) {
|
|
|
if (is_dns_response(payload, ret, &k.stream_id, &e->status)) {
|
|
|
+ bpf_printk("dns");
|
|
|
req = bpf_map_lookup_elem(&active_l7_requests, &k);
|
|
|
if (!req) {
|
|
|
return 0;
|
|
|
}
|
|
|
e->protocol = PROTOCOL_DNS;
|
|
|
- e->duration = bpf_ktime_get_ns() - req->ns;
|
|
|
+ e->start_at = req->ns;
|
|
|
+ e->end_at = bpf_ktime_get_ns();
|
|
|
+ e->duration = e->end_at - req->ns;
|
|
|
e->payload_size = ret;
|
|
|
COPY_PAYLOAD(e->payload, ret, payload);
|
|
|
send_event(ctx, e, k.pid, k.fd);
|
|
|
@@ -984,23 +828,23 @@ int sys_enter_sendmsg(struct trace_event_raw_sys_enter_rw__stub* ctx) {
|
|
|
// __u32 msg_len;
|
|
|
//};
|
|
|
|
|
|
-// SEC("tracepoint/syscalls/sys_enter_sendmmsg")
|
|
|
-// int sys_enter_sendmmsg(struct trace_event_raw_sys_enter_rw__stub* ctx) {
|
|
|
-// __u64 offset = 0;
|
|
|
-// #pragma unroll
|
|
|
-// for (int i = 0; i <= 1; i++) {
|
|
|
-// if (i >= ctx->size) {
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// struct mmsghdr h = {};
|
|
|
-// if (bpf_probe_read(&h , sizeof(h), (void *)(ctx->buf + offset))) {
|
|
|
-// return 0;
|
|
|
-// }
|
|
|
-// offset += sizeof(h);
|
|
|
-// rock_trace_enter_write(ctx, (char*)h.msg_hdr.msg_iov, 0, h.msg_hdr.msg_iovlen);
|
|
|
-// }
|
|
|
-// return 0;
|
|
|
-// }
|
|
|
+SEC("tracepoint/syscalls/sys_enter_sendmmsg")
|
|
|
+int sys_enter_sendmmsg(struct trace_event_raw_sys_enter_rw__stub* ctx) {
|
|
|
+ __u64 offset = 0;
|
|
|
+#pragma unroll
|
|
|
+ for (int i = 0; i <= 1; i++) {
|
|
|
+ if (i >= ctx->size) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ struct mmsghdr h = {};
|
|
|
+ if (bpf_probe_read(&h , sizeof(h), (void *)(ctx->buf + offset))) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ offset += sizeof(h);
|
|
|
+ trace_dns_enter_write(ctx, ctx->fd, 0, (char*)h.msg_hdr.msg_iov, 0, h.msg_hdr.msg_iovlen);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
SEC("tracepoint/syscalls/sys_enter_sendto")
|
|
|
int sys_enter_sendto(struct trace_event_raw_sys_enter_rw__stub* ctx) {
|