Skip to content

Commit 6952a17

Browse files
parse deallocation of stmt
1 parent b2968db commit 6952a17

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

aggregator/data.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,17 @@ func (a *Aggregator) parseMySQLCommand(d *l7_req.L7Event) (string, error) {
14111411
return fmt.Sprintf("EXECUTE %d *values*", d.MySqlPrepStmtId), nil
14121412
}
14131413
sqlCommand = query
1414+
} else if d.Method == l7_req.MYSQL_STMT_CLOSE { // deallocated stmt
1415+
a.mySqlStmtsMu.Lock()
1416+
// extract statementId from payload
1417+
stmtId := binary.LittleEndian.Uint32(r)
1418+
stmtKey := fmt.Sprintf("%d-%d-%d", d.Pid, d.Fd, stmtId)
1419+
_, ok := a.mySqlStmts[stmtKey]
1420+
if ok {
1421+
delete(a.mySqlStmts, stmtKey)
1422+
}
1423+
a.mySqlStmtsMu.Unlock()
1424+
return fmt.Sprintf("CLOSE STMT %d ", stmtId), nil
14141425
}
14151426
return sqlCommand, nil
14161427
}

ebpf/c/bpf_bpfeb.o

24.9 KB
Binary file not shown.

ebpf/c/bpf_bpfel.o

26 KB
Binary file not shown.

ebpf/c/l7.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,42 @@ int process_enter_of_syscalls_write_sendto(void* ctx, __u64 fd, __u8 is_tls, cha
286286
args.write_start_ns = timestamp;
287287
bpf_map_update_elem(&active_writes, &id, &args, BPF_ANY);
288288
}else if (is_mysql_query(buf,count,&req->request_type)){
289+
if (req->request_type == MYSQL_COM_STMT_CLOSE) { // stmtID will be extracted on userspace
290+
struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
291+
if (!e) {
292+
return 0;
293+
}
294+
e->protocol = PROTOCOL_MYSQL;
295+
e->method = METHOD_MYSQL_STMT_CLOSE;
296+
bpf_probe_read(e->payload, MAX_PAYLOAD_SIZE, buf);
297+
if(count > MAX_PAYLOAD_SIZE){
298+
// will not be able to copy all of it
299+
e->payload_size = MAX_PAYLOAD_SIZE;
300+
e->payload_read_complete = 0;
301+
}else{
302+
e->payload_size = count;
303+
e->payload_read_complete = 1;
304+
}
305+
306+
struct sock* sk = get_sock(fd);
307+
if (sk != NULL) {
308+
__u32 saddr = BPF_CORE_READ(sk,sk_rcv_saddr);
309+
__u16 sport = BPF_CORE_READ(sk,sk_num);
310+
__u32 daddr = BPF_CORE_READ(sk,sk_daddr);
311+
__u16 dport = BPF_CORE_READ(sk,sk_dport);
312+
313+
e->saddr = bpf_htonl(saddr);
314+
e->sport = sport;
315+
e->daddr = bpf_htonl(daddr);
316+
e->dport = bpf_htons(dport);
317+
}
318+
long r = bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, e, sizeof(*e));
319+
if (r < 0) {
320+
unsigned char log_msg[] = "failed write to l7_events -- res|fd|psize";
321+
log_to_userspace(ctx, WARN, func_name, log_msg, r, e->fd, e->payload_size);
322+
}
323+
return 0;
324+
}
289325
req->protocol = PROTOCOL_MYSQL;
290326
}else if (is_http2_frame(buf, count)){
291327
struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);

ebpf/c/mysql.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
#define MYSQL_COM_STMT_EXECUTE 0x17 // COM_STMT_EXECUTE asks the server to execute a prepared statement as identified by statement_id.
1717

1818

19+
// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_stmt_close.html
20+
#define MYSQL_COM_STMT_CLOSE 0x19 // COM_STMT_CLOSE deallocates a prepared statement.
21+
// No response packet is sent back to the client.
22+
23+
1924
#define MYSQL_RESPONSE_OK 0x00
2025
#define MYSQL_RESPONSE_EOF 0xfe
2126
#define MYSQL_RESPONSE_ERROR 0xff
@@ -24,6 +29,8 @@
2429
#define METHOD_MYSQL_TEXT_QUERY 1
2530
#define METHOD_MYSQL_PREPARE_STMT 2
2631
#define METHOD_MYSQL_EXEC_STMT 3
32+
#define METHOD_MYSQL_STMT_CLOSE 4
33+
2734

2835
#define MYSQL_STATUS_OK 1
2936
#define MYSQL_STATUS_FAILED 2
@@ -48,11 +55,10 @@ int is_mysql_query(char *buf, __u64 buf_size, __u8 *request_type) {
4855
return 1;
4956
}
5057

51-
// COM_STMT_CLOSE deallocates a prepared statement.
52-
// if (b[4] == MYSQL_COM_STMT_CLOSE) {
53-
// *request_type = MYSQL_COM_STMT_CLOSE;
54-
// return 1;
55-
// }
58+
if (b[4] == MYSQL_COM_STMT_CLOSE) {
59+
*request_type = MYSQL_COM_STMT_CLOSE;
60+
return 1;
61+
}
5662

5763
if (b[4] == MYSQL_COM_STMT_PREPARE) {
5864
*request_type = MYSQL_COM_STMT_PREPARE;

ebpf/l7_req/l7.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const (
137137
METHOD_MYSQL_TEXT_QUERY
138138
METHOD_MYSQL_PREPARE_STMT
139139
METHOD_MYSQL_EXEC_STMT
140+
METHOD_MYSQL_STMT_CLOSE
140141
)
141142

142143
// for http, user space
@@ -189,6 +190,7 @@ const (
189190
MYSQL_TEXT_QUERY = "TEXT_QUERY"
190191
MYSQL_PREPARE_STMT = "PREPARE_STMT"
191192
MYSQL_EXEC_STMT = "EXEC_STMT"
193+
MYSQL_STMT_CLOSE = "STMT_CLOSE"
192194
)
193195

194196
// Custom type for the enumeration
@@ -311,6 +313,8 @@ func (e MySQLMethodConversion) String() string {
311313
return MYSQL_PREPARE_STMT
312314
case METHOD_MYSQL_EXEC_STMT:
313315
return MYSQL_EXEC_STMT
316+
case METHOD_MYSQL_STMT_CLOSE:
317+
return MYSQL_STMT_CLOSE
314318
default:
315319
return "Unknown"
316320
}

0 commit comments

Comments
 (0)