Skip to content

Commit 59037cc

Browse files
committed
[Fix] Fix delete ebpf map failed issue
1 parent 651e631 commit 59037cc

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

agent/conn/conntrack.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,33 @@ func (c *Connection4) OnClose(needClearBpfMap bool) {
251251
err := connInfoMap.Delete(c.TgidFd)
252252
if err != nil {
253253
log.Debugf("clean conn_info_map failed: %v", err)
254+
} else {
255+
log.Debugf("clean conn_info_map deleted")
254256
}
255257
key, revKey := c.extractSockKeys()
256258
sockKeyConnIdMap := bpf.GetMap("SockKeyConnIdMap")
257-
err = sockKeyConnIdMap.Delete(&key)
259+
err = sockKeyConnIdMap.Delete(key)
258260
if err != nil {
259261
log.Debugf("clean sock_key_conn_id_map key failed: %v", err)
262+
} else {
263+
log.Debugf("clean sockKeyConnIdMap deleted key")
260264
}
261-
err = sockKeyConnIdMap.Delete(&revKey)
265+
err = sockKeyConnIdMap.Delete(revKey)
262266
if err != nil {
263267
log.Debugf("clean sock_key_conn_id_map revkey failed: %v", err)
268+
} else {
269+
log.Debugf("clean sockKeyConnIdMap deleted revkey")
264270
}
265271
sockXmitMap := bpf.GetMap("SockXmitMap")
266-
n, err := sockXmitMap.BatchDelete([]bpf.AgentSockKey{key, revKey}, nil)
272+
err = sockXmitMap.Delete(key)
273+
if err == nil {
274+
log.Debugf("clean sockXmitMap deleted key")
275+
} else {
276+
log.Debugf("clean sockXmitMap failed: %v", err)
277+
}
278+
err = sockXmitMap.Delete(revKey)
267279
if err == nil {
268-
log.Debugf("clean sockXmitMap deleted: %v", n)
280+
log.Debugf("clean sockXmitMap deleted revkey")
269281
} else {
270282
log.Debugf("clean sockXmitMap failed: %v", err)
271283
}
@@ -283,7 +295,7 @@ func (c *Connection4) UpdateConnectionTraceable(traceable bool) {
283295
connInfo := bpf.AgentConnInfoT{}
284296
err := connInfoMap.Lookup(c.TgidFd, &connInfo)
285297
if err == nil {
286-
connInfo.NoTrace = traceable
298+
connInfo.NoTrace = !traceable
287299
connInfoMap.Update(c.TgidFd, &connInfo, ebpf.UpdateExist)
288300
} else {
289301
log.Debugf("try to update %s conn_info_map to no_trace, but no entry in map found!", c.ToString())
@@ -294,7 +306,7 @@ func (c *Connection4) doUpdateConnIdMapProtocolToUnknwon(key bpf.AgentSockKey, m
294306
var connIds bpf.AgentConnIdS_t
295307
err := m.Lookup(&key, &connIds)
296308
if err == nil {
297-
connIds.NoTrace = traceable
309+
connIds.NoTrace = !traceable
298310
m.Update(&key, &connIds, ebpf.UpdateExist)
299311
} else {
300312
log.Debugf("try to update %s conn_id_map to no_trace, but no entry in map found! key: %v", c.ToString(), key)

agent/conn/processor.go

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func (p *Processor) run() {
151151
} else if event.ConnType == bpf.AgentConnTypeTKProtocolInfer {
152152
// 协议推断
153153
conn = p.connManager.FindConnection4Or(TgidFd, event.Ts+common.LaunchEpochTime)
154+
// previousProtocol := conn.Protocol
154155
if conn != nil && conn.Status != Closed {
155156
conn.Protocol = event.ConnInfo.Protocol
156157
} else {

bpf/pktlatency.bpf.c

+13-10
Original file line numberDiff line numberDiff line change
@@ -1031,14 +1031,16 @@ static __always_inline bool create_conn_info(void* ctx, struct conn_info_t *conn
10311031
uint16_t one = 1;
10321032
uint8_t* enable_local_port_filter = bpf_map_lookup_elem(&enabled_local_port_map, &one);
10331033
if (enable_local_port_filter != NULL) {
1034-
uint8_t* enabled_local_port = bpf_map_lookup_elem(&enabled_local_port_map, &conn_info->laddr.in4.sin_port);
1034+
u16 port = conn_info->laddr.in4.sin_port;
1035+
uint8_t* enabled_local_port = bpf_map_lookup_elem(&enabled_local_port_map, &port);
10351036
if (enabled_local_port == NULL) {
10361037
return false;
10371038
}
10381039
}
10391040
uint8_t* enable_remote_port_filter = bpf_map_lookup_elem(&enabled_remote_port_map, &one);
10401041
if (enable_remote_port_filter != NULL) {
1041-
uint8_t* enabled_remote_port = bpf_map_lookup_elem(&enabled_remote_port_map, &conn_info->raddr.in4.sin_port);
1042+
u16 port = conn_info->raddr.in4.sin_port;
1043+
uint8_t* enabled_remote_port = bpf_map_lookup_elem(&enabled_remote_port_map, &port);
10421044
if (enabled_remote_port == NULL) {
10431045
return false;
10441046
}
@@ -1047,7 +1049,8 @@ static __always_inline bool create_conn_info(void* ctx, struct conn_info_t *conn
10471049
if (conn_info->raddr.in4.sin_family == AF_INET) {
10481050
uint8_t* enable_remote_ipv4_filter = bpf_map_lookup_elem(&enabled_remote_ipv4_map, &one32);
10491051
if (enable_remote_ipv4_filter != NULL) {
1050-
uint8_t* enabled_remote_ipv4 = bpf_map_lookup_elem(&enabled_remote_ipv4_map, &conn_info->raddr.in4.sin_addr.s_addr);
1052+
u32 addr = conn_info->raddr.in4.sin_addr.s_addr;
1053+
uint8_t* enabled_remote_ipv4 = bpf_map_lookup_elem(&enabled_remote_ipv4_map, &addr);
10511054
if (enabled_remote_ipv4 == NULL || conn_info->raddr.in4.sin_addr.s_addr == 0) {
10521055
return false;
10531056
}
@@ -1238,12 +1241,12 @@ static __always_inline void process_syscall_data_vecs(void* ctx, struct data_arg
12381241
if (!conn_info) {
12391242
tcp_sk = get_socket_from_fd(args->fd);
12401243
if (tcp_sk) {
1241-
struct sock_key key;
12421244
int zero = 0;
1243-
struct conn_info_t *new_conn_info = bpf_map_lookup_elem(&conn_info_t_map, &zero);
1245+
// struct conn_info_t *new_conn_info = bpf_map_lookup_elem(&conn_info_t_map, &zero);
1246+
struct conn_info_t _new_conn_info = {};
1247+
struct conn_info_t *new_conn_info = &_new_conn_info;
12441248
if (new_conn_info) {
12451249
new_conn_info->protocol = kProtocolUnset;
1246-
parse_sock_key_sk((struct sock*)tcp_sk, &key);
12471250
bool created = create_conn_info_in_data_syscall(ctx, tcp_sk, tgid_fd, direct, bytes_count, new_conn_info);
12481251
if (created) {
12491252
conn_info = bpf_map_lookup_elem(&conn_info_map, &tgid_fd);
@@ -1317,12 +1320,12 @@ static __always_inline void process_syscall_data(void* ctx, struct data_args *ar
13171320
if (!conn_info) {
13181321
tcp_sk = get_socket_from_fd(args->fd);
13191322
if (tcp_sk) {
1320-
struct sock_key key;
13211323
int zero = 0;
1322-
struct conn_info_t *new_conn_info = bpf_map_lookup_elem(&conn_info_t_map, &zero);
1324+
// struct conn_info_t *new_conn_info = bpf_map_lookup_elem(&conn_info_t_map, &zero);
1325+
struct conn_info_t _new_conn_info = {};
1326+
struct conn_info_t *new_conn_info = &_new_conn_info;
13231327
if (new_conn_info) {
13241328
new_conn_info->protocol = kProtocolUnset;
1325-
parse_sock_key_sk((struct sock*)tcp_sk, &key);
13261329
bool created = create_conn_info_in_data_syscall(ctx, tcp_sk, tgid_fd, direct, bytes_count, new_conn_info);
13271330
if (created) {
13281331
conn_info = bpf_map_lookup_elem(&conn_info_map, &tgid_fd);
@@ -1336,8 +1339,8 @@ static __always_inline void process_syscall_data(void* ctx, struct data_args *ar
13361339
if (conn_info->protocol == kProtocolUnset || conn_info->protocol == kProtocolUnknown) {
13371340
enum traffic_protocol_t before_infer = conn_info->protocol;
13381341
// bpf_printk("[protocol infer]:start, bc:%d", bytes_count);
1339-
struct protocol_message_t protocol_message = infer_protocol(args->buf, bytes_count, conn_info);
13401342
// conn_info->protocol = protocol_message.protocol;
1343+
struct protocol_message_t protocol_message = infer_protocol(args->buf, bytes_count, conn_info);
13411344
if (before_infer != protocol_message.protocol) {
13421345
conn_info->protocol = protocol_message.protocol;
13431346
// bpf_printk("[protocol infer]: %d", conn_info->protocol);

0 commit comments

Comments
 (0)