Skip to content

Commit fd4679d

Browse files
authored
net_watcher:move net_watcher to MagicEyes (#904)
* add use protocol percentage * update * delete * delete * move net_watcher to MagicEyes
1 parent ffba937 commit fd4679d

20 files changed

+4890
-1585
lines changed

MagicEyes/src/backend/net/net_watcher/bpf/common.bpf.h

+670
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2023 The LMP Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
// net_watcher libbpf 丢包
17+
18+
#include "common.bpf.h"
19+
static __always_inline
20+
int __tp_kfree(struct trace_event_raw_kfree_skb *ctx)
21+
{
22+
if(!drop_reason)
23+
return 0;
24+
struct sk_buff *skb=ctx->skbaddr;
25+
if (skb == NULL) // 判断是否为空
26+
return 0;
27+
struct iphdr *ip = skb_to_iphdr(skb);
28+
struct tcphdr *tcp = skb_to_tcphdr(skb);
29+
struct packet_tuple pkt_tuple = {0};
30+
get_pkt_tuple(&pkt_tuple, ip, tcp);
31+
32+
struct reasonissue *message;
33+
message = bpf_ringbuf_reserve(&kfree_rb, sizeof(*message), 0);
34+
if(!message){
35+
return 0;
36+
}
37+
message->saddr = pkt_tuple.saddr;
38+
message->daddr = pkt_tuple.daddr;
39+
message->sport = pkt_tuple.sport;
40+
message->dport = pkt_tuple.dport;
41+
message->protocol = ctx->protocol;
42+
message->location = (long)ctx->location;
43+
message->drop_reason = ctx->reason;
44+
bpf_ringbuf_submit(message,0);
45+
if(stack_info)
46+
getstack(ctx);
47+
return 0;
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2023 The LMP Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
// net_watcher libbpf icmp
17+
18+
#include "common.bpf.h"
19+
20+
static __always_inline
21+
int __icmp_time(struct sk_buff *skb)
22+
{
23+
if(!icmp_info||skb==NULL)
24+
return 0;
25+
struct iphdr *ip = skb_to_iphdr(skb);
26+
struct ip_packet ipk = {0};
27+
get_ip_pkt_tuple(&ipk, ip);
28+
unsigned long long time= bpf_ktime_get_ns() / 1000;
29+
bpf_map_update_elem(&icmp_time, &ipk, &time, BPF_ANY);
30+
return 0;
31+
}
32+
33+
static __always_inline
34+
int __rcvend_icmp_time(struct sk_buff *skb)
35+
{
36+
if(!icmp_info)
37+
return 0;
38+
if(skb==NULL)
39+
return 0;
40+
struct iphdr *ip = skb_to_iphdr(skb);
41+
struct ip_packet ipk = {0};
42+
get_ip_pkt_tuple(&ipk, ip);
43+
unsigned long long *pre_time = bpf_map_lookup_elem(&icmp_time, &ipk);
44+
if(pre_time==NULL)
45+
return 0;
46+
47+
unsigned long long new_time= bpf_ktime_get_ns() / 1000;
48+
unsigned long long time=new_time-*pre_time;
49+
struct icmptime *message;
50+
message = bpf_ringbuf_reserve(&icmp_rb, sizeof(*message), 0);
51+
if(!message){
52+
return 0;
53+
}
54+
55+
message->saddr = ipk.saddr;
56+
message->daddr =ipk.daddr;
57+
message->icmp_tran_time =time;
58+
message->flag =0;
59+
bpf_ringbuf_submit(message,0);
60+
return 0;
61+
}
62+
63+
static __always_inline
64+
int __reply_icmp_time(struct sk_buff *skb)
65+
{
66+
if(!icmp_info)
67+
return 0;
68+
if(skb==NULL)
69+
return 0;
70+
struct iphdr *ip = skb_to_iphdr(skb);
71+
struct ip_packet ipk = {0};
72+
get_ip_pkt_tuple(&ipk, ip);
73+
unsigned long long *pre_time = bpf_map_lookup_elem(&icmp_time, &ipk);
74+
if(pre_time==NULL)
75+
return 0;
76+
unsigned long long new_time= bpf_ktime_get_ns() / 1000;
77+
unsigned long long time=new_time-*pre_time;
78+
struct icmptime *message;
79+
message = bpf_ringbuf_reserve(&icmp_rb, sizeof(*message), 0);
80+
if(!message){
81+
return 0;
82+
}
83+
84+
message->saddr = ipk.saddr;
85+
message->daddr =ipk.daddr;
86+
message->icmp_tran_time =time;
87+
message->flag =1;
88+
bpf_ringbuf_submit(message,0);
89+
return 0;
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2023 The LMP Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
// mysql
17+
18+
#include "common.bpf.h"
19+
#include "mysql_helper.bpf.h"
20+
static __always_inline int __handle_mysql_start(struct pt_regs *ctx) {
21+
// dispatch_command(THD *thd, const COM_DATA *com_data, enum
22+
enum enum_server_command command = PT_REGS_PARM3(ctx);
23+
union COM_DATA *com_data = (union COM_DATA *)PT_REGS_PARM2(ctx);
24+
pid_t pid = bpf_get_current_pid_tgid() >> 32;
25+
pid_t tid = bpf_get_current_pid_tgid();
26+
void *thd = (void *)PT_REGS_PARM1(ctx);
27+
struct query_info info;
28+
u32 size = 0;
29+
char *sql;
30+
31+
if (command != COM_QUERY) {
32+
return 0;
33+
}
34+
35+
bpf_probe_read(&info.size, sizeof(info.size), &com_data->com_query.length);
36+
bpf_probe_read_str(&sql, sizeof(sql), &com_data->com_query.query);
37+
bpf_probe_read_str(&info.msql, sizeof(info.msql), sql);
38+
// bpf_printk("sql1==%s size1==%lu", info.msql,info.size);
39+
info.start_time = bpf_ktime_get_ns() / 1000;
40+
41+
bpf_map_update_elem(&queries, &tid, &info, BPF_ANY);
42+
return 0;
43+
}
44+
45+
static __always_inline int __handle_mysql_end(struct pt_regs *ctx) {
46+
char comm[16];
47+
pid_t pid = bpf_get_current_pid_tgid() >> 32;
48+
pid_t tid = bpf_get_current_pid_tgid();
49+
struct query_info *info = bpf_map_lookup_elem(&queries, &tid);
50+
if (!info) {
51+
return 0;
52+
}
53+
54+
struct mysql_query *message =
55+
bpf_ringbuf_reserve(&mysql_rb, sizeof(*message), 0);
56+
if (!message) {
57+
return 0;
58+
}
59+
u64 *count_ptr, count = 1;
60+
count_ptr = bpf_map_lookup_elem(&sql_count, &tid);
61+
if (count_ptr) {
62+
count = *count_ptr + 1;
63+
}
64+
65+
message->count = count;
66+
bpf_map_update_elem(&sql_count, &tid, &count, BPF_ANY);
67+
message->duratime = bpf_ktime_get_ns() / 1000 - info->start_time;
68+
message->pid = pid;
69+
message->tid = tid;
70+
bpf_get_current_comm(&message->comm, sizeof(comm));
71+
message->size = info->size;
72+
bpf_probe_read_str(&message->msql, sizeof(message->msql), info->msql);
73+
// bpf_printk("C==%d D==%lu S==%lu SQL==%s",count,
74+
// message->duratime,message->size,message->msql);
75+
76+
bpf_ringbuf_submit(message, 0);
77+
return 0;
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// Copyright 2023 The LMP Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
//
17+
// net_watcher libbpf 内核<->用户 传递信息相关结构体
18+
19+
#ifndef __MYSQL_HELPER_BPF_H
20+
#define __MYSQL_HELPER_BPF_H
21+
22+
#include "net_watcher.h"
23+
#include "vmlinux.h"
24+
#include <asm-generic/errno.h>
25+
#include <bpf/bpf_core_read.h>
26+
#include <bpf/bpf_endian.h>
27+
#include <bpf/bpf_helpers.h>
28+
#include <bpf/bpf_tracing.h>
29+
#include <string.h>
30+
31+
enum enum_server_command {
32+
COM_SLEEP,
33+
COM_QUIT,
34+
COM_INIT_DB,
35+
COM_QUERY,
36+
COM_FIELD_LIST,
37+
COM_CREATE_DB,
38+
COM_DROP_DB,
39+
COM_REFRESH,
40+
COM_SHUTDOWN,
41+
COM_STATISTICS,
42+
COM_PROCESS_INFO,
43+
COM_CONNECT,
44+
COM_PROCESS_KILL,
45+
COM_DEBUG,
46+
COM_PING,
47+
COM_TIME,
48+
COM_DELAYED_INSERT,
49+
COM_CHANGE_USER,
50+
COM_BINLOG_DUMP,
51+
COM_TABLE_DUMP,
52+
COM_CONNECT_OUT,
53+
COM_REGISTER_SLAVE,
54+
COM_STMT_PREPARE,
55+
COM_STMT_EXECUTE,
56+
COM_STMT_SEND_LONG_DATA,
57+
COM_STMT_CLOSE,
58+
COM_STMT_RESET,
59+
COM_SET_OPTION,
60+
COM_STMT_FETCH,
61+
COM_DAEMON,
62+
COM_BINLOG_DUMP_GTID,
63+
COM_RESET_CONNECTION,
64+
/* don't forget to update const char *command_name[] in sql_parse.cc */
65+
/* Must be last */
66+
COM_END
67+
};
68+
69+
typedef struct st_com_init_db_data {
70+
const char *db_name;
71+
unsigned long length;
72+
} COM_INIT_DB_DATA;
73+
74+
#define MYSQL_SHUTDOWN_KILLABLE_CONNECT (unsigned char)(1 << 0)
75+
#define MYSQL_SHUTDOWN_KILLABLE_TRANS (unsigned char)(1 << 1)
76+
#define MYSQL_SHUTDOWN_KILLABLE_LOCK_TABLE (unsigned char)(1 << 2)
77+
#define MYSQL_SHUTDOWN_KILLABLE_UPDATE (unsigned char)(1 << 3)
78+
79+
#define LOCK_MODE_MASK 0xFUL
80+
#define LOCK_TYPE_MASK 0xF0UL
81+
82+
enum mysql_enum_shutdown_level {
83+
SHUTDOWN_DEFAULT = 0,
84+
SHUTDOWN_WAIT_CONNECTIONS = MYSQL_SHUTDOWN_KILLABLE_CONNECT,
85+
SHUTDOWN_WAIT_TRANSACTIONS = MYSQL_SHUTDOWN_KILLABLE_TRANS,
86+
SHUTDOWN_WAIT_UPDATES = MYSQL_SHUTDOWN_KILLABLE_UPDATE,
87+
SHUTDOWN_WAIT_ALL_BUFFERS = (MYSQL_SHUTDOWN_KILLABLE_UPDATE << 1),
88+
SHUTDOWN_WAIT_CRITICAL_BUFFERS = (MYSQL_SHUTDOWN_KILLABLE_UPDATE << 1) + 1,
89+
KILL_QUERY = 254,
90+
KILL_CONNECTION = 255
91+
};
92+
93+
typedef struct st_com_refresh_data {
94+
unsigned char options;
95+
} COM_REFRESH_DATA;
96+
97+
typedef struct st_com_shutdown_data {
98+
enum mysql_enum_shutdown_level level;
99+
} COM_SHUTDOWN_DATA;
100+
101+
typedef struct st_com_kill_data {
102+
unsigned long id;
103+
} COM_KILL_DATA;
104+
105+
typedef struct st_com_set_option_data {
106+
unsigned int opt_command;
107+
} COM_SET_OPTION_DATA;
108+
109+
typedef struct st_com_stmt_execute_data {
110+
unsigned long stmt_id;
111+
unsigned long flags;
112+
unsigned char *params;
113+
unsigned long params_length;
114+
} COM_STMT_EXECUTE_DATA;
115+
116+
typedef struct st_com_stmt_fetch_data {
117+
unsigned long stmt_id;
118+
unsigned long num_rows;
119+
} COM_STMT_FETCH_DATA;
120+
121+
typedef struct st_com_stmt_send_long_data_data {
122+
unsigned long stmt_id;
123+
unsigned int param_number;
124+
unsigned char *longdata;
125+
unsigned long length;
126+
} COM_STMT_SEND_LONG_DATA_DATA;
127+
128+
typedef struct st_com_stmt_prepare_data {
129+
const char *query;
130+
unsigned int length;
131+
} COM_STMT_PREPARE_DATA;
132+
133+
typedef struct st_stmt_close_data {
134+
unsigned int stmt_id;
135+
} COM_STMT_CLOSE_DATA;
136+
137+
typedef struct st_com_stmt_reset_data {
138+
unsigned int stmt_id;
139+
} COM_STMT_RESET_DATA;
140+
141+
typedef struct st_com_query_data {
142+
const char *query;
143+
unsigned int length;
144+
} COM_QUERY_DATA;
145+
146+
typedef struct st_com_field_list_data {
147+
unsigned char *table_name;
148+
unsigned int table_name_length;
149+
const unsigned char *query;
150+
unsigned int query_length;
151+
} COM_FIELD_LIST_DATA;
152+
153+
union COM_DATA {
154+
COM_INIT_DB_DATA com_init_db;
155+
COM_REFRESH_DATA com_refresh;
156+
COM_SHUTDOWN_DATA com_shutdown;
157+
COM_KILL_DATA com_kill;
158+
COM_SET_OPTION_DATA com_set_option;
159+
COM_STMT_EXECUTE_DATA com_stmt_execute;
160+
COM_STMT_FETCH_DATA com_stmt_fetch;
161+
COM_STMT_SEND_LONG_DATA_DATA com_stmt_send_long_data;
162+
COM_STMT_PREPARE_DATA com_stmt_prepare;
163+
COM_STMT_CLOSE_DATA com_stmt_close;
164+
COM_STMT_RESET_DATA com_stmt_reset;
165+
COM_QUERY_DATA com_query;
166+
COM_FIELD_LIST_DATA com_field_list;
167+
};
168+
169+
/* help functions end */
170+
171+
#endif

0 commit comments

Comments
 (0)