|
|
@@ -276,8 +276,9 @@ struct {
|
|
|
} apm_span_context_heap3 SEC(".maps");
|
|
|
|
|
|
#define TRACE_ID_SIZE 16
|
|
|
-static __inline __u32 has_cw_header(const char *data);
|
|
|
-static __always_inline struct apm_span_context *build_sc(struct ebpf_proc_info proc_info, char *host);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
static __always_inline void span_context_to_cw_string_stream(struct apm_span_context *ctx, char *buff, char type_from);
|
|
|
static __always_inline void cw_string_to_span_context(char *str, struct apm_span_context *ctx);
|
|
|
static __always_inline void generate_random_bytes(unsigned char *buff, __u32 size);
|
|
|
@@ -1402,13 +1403,13 @@ int sockops_cb(struct bpf_sock_ops *skops) {
|
|
|
|
|
|
// 只处理TCP连接
|
|
|
if (skops->family != AF_INET) {
|
|
|
-// return 0;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
// 只处理客户端主动建立的连接(出站连接)
|
|
|
// 这样stream_verdict就能看到客户端发送的HTTP请求
|
|
|
if (skops->op != BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) {
|
|
|
-// return 0;
|
|
|
+ return 0;
|
|
|
}
|
|
|
__u32 saddr = bpf_ntohl(skops->local_ip4);
|
|
|
__u32 daddr = bpf_ntohl(skops->remote_ip4);
|
|
|
@@ -1431,7 +1432,7 @@ int sockops_cb(struct bpf_sock_ops *skops) {
|
|
|
// 同时将socket添加到sockmap(用于sk_msg程序)
|
|
|
bpf_sock_map_update(skops, &sk_msg_map, &cookie, BPF_ANY);
|
|
|
|
|
|
-// bpf_printk("sockops: added socket to sockhash and sockmap, cookie=%u\n", cookie);
|
|
|
+ bpf_printk("sockops: added socket to sockhash and sockmap, cookie=%u\n", cookie);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -1495,14 +1496,6 @@ int http_request_handler(struct __sk_buff *skb) {
|
|
|
return 1; // 允许数据包通过
|
|
|
}
|
|
|
|
|
|
-static __always_inline int load_u8(const char *data, const char *data_end, int off, char *out)
|
|
|
-{
|
|
|
- const char *p = data + off;
|
|
|
- if (p + 1 > data_end)
|
|
|
- return -1;
|
|
|
- *out = *(const char *)p;
|
|
|
- return 0;
|
|
|
-}
|
|
|
// sk_msg program for handling socket messages
|
|
|
SEC("sk_msg")
|
|
|
int sk_msg_handler(struct sk_msg_md *msg)
|
|
|
@@ -1517,6 +1510,14 @@ int sk_msg_handler(struct sk_msg_md *msg)
|
|
|
return SK_PASS;
|
|
|
}
|
|
|
|
|
|
+ // todo 进程过滤
|
|
|
+ __u32 tgid = (__u32) (bpf_get_current_pid_tgid() >> 32);
|
|
|
+ struct ebpf_proc_info *proc_info = bpf_map_lookup_elem(&proc_info_map, &tgid);
|
|
|
+ if (!proc_info) {
|
|
|
+ return SK_PASS;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
__u32 max_buf = msg->size < MAX_L7_IOVEC_BUF_SIZE ? msg->size : MAX_L7_IOVEC_BUF_SIZE;
|
|
|
|
|
|
// 拉满整条消息查Header
|
|
|
@@ -1571,26 +1572,52 @@ int sk_msg_handler(struct sk_msg_md *msg)
|
|
|
}
|
|
|
|
|
|
// TODO real Header
|
|
|
- const char insert[] = "AA: a\r\n"; // 要插入的 7 字节
|
|
|
+// const char insert[] = "AA: a\r\n"; // 要插入的 7 字节
|
|
|
// const char insert[CW_STREAM_HEADER_LEN] = "cwtrace: 00:00:1015481350055581:5450531005555981:5610250100539899:304775019cd3218a304775019cd3218a:1001025098564810:140acc88cde8773f\r\n";
|
|
|
|
|
|
- int ins_len = sizeof(insert) - 1; // 不含 '\0'
|
|
|
+// int ins_len = sizeof(insert) - 1; // 不含 '\0'
|
|
|
|
|
|
- __u32 tgid = (__u32) (bpf_get_current_pid_tgid() >> 32);
|
|
|
- struct ebpf_proc_info *proc_info = bpf_map_lookup_elem(&proc_info_map, &tgid);
|
|
|
- if (!proc_info) {
|
|
|
- return SK_PASS;
|
|
|
- }
|
|
|
|
|
|
- struct apm_span_context *cw_sc = build_sc(*proc_info, "host");
|
|
|
+// __u32 rip_net = msg->remote_ip4; // network order
|
|
|
+// __u32 raw_port = msg->remote_port; // 32-bit field
|
|
|
+// unsigned char assumed_app_id[16];
|
|
|
+// ipport_to_8digits_from_u32_high(rip_net, raw_port, assumed_app_id);
|
|
|
+
|
|
|
+// for (int i = 0; i < APM_ASSUMED_APP_ID_SIZE; i++) {
|
|
|
+// bpf_printk("assumed_app_id-assumed_app_id[%d] = %02x", i, assumed_app_id[i]);
|
|
|
+// }
|
|
|
+
|
|
|
+ struct apm_span_context *cw_sc = build_sc_by_ipport(*proc_info, msg->remote_ip4, msg->remote_port);
|
|
|
if (cw_sc == NULL) {
|
|
|
return SK_PASS;
|
|
|
}
|
|
|
|
|
|
- span_context_to_cw_string_stream(cw_sc, map_data->header_stream, '1');
|
|
|
+
|
|
|
+ span_context_to_cw_string_stream(cw_sc, map_data->header_stream, code_type_to_code(proc_info->code_type));
|
|
|
+// for (int i = 0; i < APM_ASSUMED_APP_ID_SIZE; i++) {
|
|
|
+// bpf_printk("cw-assumed_app_id[%d] = %02x", i, cw_sc->assumed_app_id[i]);
|
|
|
+// }
|
|
|
+
|
|
|
bpf_printk("[java client] header:[%s],sizeof %d ", map_data->header_stream, sizeof(map_data->header_stream));
|
|
|
|
|
|
+ __u32 raw = msg->remote_port;
|
|
|
+ bpf_printk("raw_port=0x%x low_net=0x%x high_net=0x%x\n",
|
|
|
+ raw, (raw & 0xffff), ((raw >> 16) & 0xffff));
|
|
|
+ __u16 port_host = ( __u16 )( __builtin_bswap32(msg->remote_port) & 0xffff );
|
|
|
+
|
|
|
|
|
|
+// __u32 rip = __builtin_bswap32(msg->remote_ip4); // host order
|
|
|
+// __u8 b0 = (rip >> 24) & 0xff;
|
|
|
+// __u8 b1 = (rip >> 16) & 0xff;
|
|
|
+// __u8 b2 = (rip >> 8) & 0xff;
|
|
|
+// __u8 b3 = rip & 0xff;
|
|
|
+// __u16 rport = __builtin_bswap32(msg->remote_port);
|
|
|
+// bpf_printk("remote %u.%u.%u\n", b0, b1, b2);
|
|
|
+// bpf_printk("remote port %u:%u\n", b3, rport);
|
|
|
+// bpf_printk("remote port %u\n", rport);
|
|
|
+
|
|
|
+
|
|
|
+ __u32 ins_len = sizeof(map_data->header_stream) - 1;
|
|
|
__u32 header_offset = map_data->header_offset_idx;
|
|
|
|
|
|
if (header_offset > msg->size){
|
|
|
@@ -1629,42 +1656,14 @@ int sk_msg_handler(struct sk_msg_md *msg)
|
|
|
if (header_offset > len)
|
|
|
return SK_PASS;
|
|
|
|
|
|
-// if (n > MAX_INS) n = MAX_INS;
|
|
|
-// if (n > len - header_off) n = len - header_off; // 用 len - off 规避加法溢出
|
|
|
-
|
|
|
-
|
|
|
-// char *dst = data + header_off;
|
|
|
-
|
|
|
-
|
|
|
-//#pragma clang loop unroll(full)
|
|
|
-// for (__u32 i = 0; i < MAX_INS; i++) {
|
|
|
-// if (i >= n) break;
|
|
|
-// data[0] = insert[0];
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// if (ins_len >= len)
|
|
|
-// return SK_PASS;
|
|
|
-// if (data + 0 <= data_end)
|
|
|
-// data[0] = insert[0];
|
|
|
-// *(char *)(data + 0) = insert[0];
|
|
|
-//#pragma clang loop unroll(full)
|
|
|
-// for (int i = 0; i < ins_len; i++) {
|
|
|
-// if (data + header_off + i + 1 > data_end) break;
|
|
|
-// *(char *)(data + header_off + i) = insert[i];
|
|
|
-// bpf_printk("%d",i);
|
|
|
-//
|
|
|
-// }
|
|
|
-//
|
|
|
-// return SK_PASS;
|
|
|
-// __u32 max_buf;
|
|
|
#pragma clang loop unroll(full)
|
|
|
for (int i = 0; i < MAX_L7_IOVEC_BUF_SIZE; i++) {
|
|
|
/*test1*/
|
|
|
if (i >= header_offset) {
|
|
|
for (int j = 0; j < ins_len; j++) {
|
|
|
if (data + i + 1 > data_end) break;
|
|
|
- *(char *) (data + i) = insert[j];
|
|
|
- bpf_printk("set-%d %d", i, j);
|
|
|
+ *(char *) (data + i) = map_data->header_stream[j];
|
|
|
+// bpf_printk("set-%d %d", i, j);
|
|
|
i++;
|
|
|
}
|
|
|
break;
|
|
|
@@ -1693,17 +1692,8 @@ int sk_msg_handler(struct sk_msg_md *msg)
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-// if (data + header_off + 0 + 1 <=data_end) {
|
|
|
-// *(char *)(data + header_off + 0) = insert[0];
|
|
|
-// }
|
|
|
-// if (data + header_off + 1 + 1 <=data_end) {
|
|
|
-// *(char *)(data + header_off + 1) = insert[1];
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
-// if (data1 + i + 1 > data_end) break;
|
|
|
-// *(char *)(data1 + i) = insert[i];
|
|
|
+ // 保存span context
|
|
|
+ cw_save_current_tracking_span(cw_sc);
|
|
|
char vb[200] = {};
|
|
|
if (bpf_probe_read_kernel(vb, 200, (void *)data) == 0) {
|
|
|
bpf_printk("Modified HTTP request: %s\n", vb);
|