Skip to content

Commit 4174719

Browse files
committed
nettrace: show socket state in sock mode
Signed-off-by: Menglong Dong <imagedong@tencent.com>
1 parent 98545ea commit 4174719

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

shared/bpf/skb_parse.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#ifndef _H_BPF_SKB_UTILS
88
#define _H_BPF_SKB_UTILS
99

10+
#undef bpf_core_type_exists
11+
#undef bpf_core_field_exists
12+
#undef bpf_core_enum_value_exists
1013
#include <bpf/bpf_core_read.h>
1114

1215
#include "skb_macro.h"
@@ -405,11 +408,12 @@ static try_inline int __probe_parse_sk(parse_ctx_t *ctx)
405408
ske->l4.tcp.dport))
406409
goto err;
407410

408-
ske->rqlen = _C(&(sk->sk_receive_queue), qlen);
409-
ske->wqlen = _C(&(sk->sk_write_queue), qlen);
411+
ske->rqlen = _C(sk, sk_receive_queue.qlen);
412+
ske->wqlen = _C(sk, sk_write_queue.qlen);
410413

411414
ske->proto_l3 = l3_proto;
412415
ske->proto_l4 = l4_proto;
416+
ske->state = _C(skc, skc_state);
413417

414418
icsk = (void *)sk;
415419
if (bpf_core_helper_exist(jiffies64))

shared/bpf/skb_shared.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ typedef struct {
117117
u16 proto_l3;
118118
u8 proto_l4;
119119
u8 timer_pending;
120+
u8 state;
120121
} sock_t;
121122

122123
#define TCP_FLAGS_ACK (1 << 4)

shared/pkt_utils.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define _LINUX_IN_H
88
#include <netinet/in.h>
99
#include <arpa/inet.h>
10+
#include <netinet/tcp.h>
1011

1112
#include <sys_utils.h>
1213

@@ -158,6 +159,20 @@ static const char *timer_name[] = {
158159
[ICSK_TIME_LOSS_PROBE] = "loss_probe",
159160
[ICSK_TIME_REO_TIMEOUT] = "reo_timeout",
160161
};
162+
static const char *state_name[] = {
163+
[0] = "UNKNOW",
164+
[TCP_ESTABLISHED] = "ESTABLISHED",
165+
[TCP_SYN_SENT] = "SYN_SENT",
166+
[TCP_SYN_RECV] = "SYN_RECV",
167+
[TCP_FIN_WAIT1] = "FIN_WAIT1",
168+
[TCP_FIN_WAIT2] = "FIN_WAIT2",
169+
[TCP_TIME_WAIT] = "TIME_WAIT",
170+
[TCP_CLOSE] = "CLOSE",
171+
[TCP_CLOSE_WAIT] = "CLOSE_WAIT",
172+
[TCP_LAST_ACK] = "LAST_ACK",
173+
[TCP_LISTEN] = "LISTEN",
174+
[TCP_CLOSING] = "CLOSING",
175+
};
161176

162177
int ts_print_sock(char *buf, sock_t *ske, char *minfo, bool date_format)
163178
{
@@ -222,7 +237,8 @@ int ts_print_sock(char *buf, sock_t *ske, char *minfo, bool date_format)
222237

223238
switch (l4) {
224239
case IPPROTO_TCP:
225-
BUF_FMT(" info:(%u %u)", ske->l4.tcp.packets_out,
240+
BUF_FMT(" %s info:(%u %u)", state_name[ske->state],
241+
ske->l4.tcp.packets_out,
226242
ske->l4.tcp.retrans_out);
227243
case IPPROTO_UDP:
228244
hz = kernel_hz();

0 commit comments

Comments
 (0)