Skip to content

Commit

Permalink
trace.py: show human-readable TCP state transitions
Browse files Browse the repository at this point in the history
This commit slighly improves the --tcpdump option to present
the tcp_state tracepoint look less cryptic like so:

0xffff8000015c5040 netisr           0         1.172073258 tcp_state            tp=0xffffa00001e9bc00, CLOSED -> SYN_RECEIVED
0xffff8000015c5040 netisr           0         1.172079566 tcp_state            tp=0xffffa00001e9bc00, SYN_RECEIVED -> ESTABLISHED
0xffff800002064040 >/tests/tst-tcp  0         1.273263812 tcp_state            tp=0xffffa00001e9bc00, ESTABLISHED -> FIN_WAIT_1
0xffff8000015c5040 netisr           0         1.273368587 tcp_state            tp=0xffffa00001e9b800, ESTABLISHED -> CLOSE_WAIT
0xffff8000015c5040 netisr           0         1.273374815 tcp_state            tp=0xffffa00001e9bc00, FIN_WAIT_1 -> FIN_WAIT_2
0xffff80000205e040 /tests/tst-tcp-  0         1.273412034 tcp_state            tp=0xffffa00001e9b800, CLOSE_WAIT -> LAST_ACK
0xffff8000023fa040 >/tests/tst-tcp  0         1.280858240 tcp_state            tp=0xffffa00001e9b800, CLOSED -> CLOSED

Signed-off-by: Waldemar Kozaczuk <[email protected]>
  • Loading branch information
wkozaczuk committed Jul 30, 2022
1 parent 2f8bcdd commit 46b79c2
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions scripts/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ def get_backtrace_formatter(args):

def list_trace(args):
def data_formatter(sample):
if args.tcpdump and is_net_packet_sample(sample):
return format_packet_sample(sample)
if args.tcpdump:
if is_net_packet_sample(sample):
return format_packet_sample(sample)
if is_tcp_state_sample(sample):
return format_tcp_state_sample(sample)
return sample.format_data(sample)

def name_filter(sample):
Expand Down Expand Up @@ -368,9 +371,41 @@ def format_packet_sample(sample):
proc.wait()
return packet_line

# See http://tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF-2.htm
tcp_states = {
0: 'CLOSED', # closed
1: 'LISTEN', # listening for connection
2: 'SYN_SENT', # active, have sent syn
3: 'SYN_RECEIVED', # have sent and received syn
# states < ESTABLISHED are those where connections not established
4: 'ESTABLISHED', # established
5: 'CLOSE_WAIT', # received fin, waiting for close
# states > CLOSE_WAIT are those where user has closed
6: 'FIN_WAIT_1', # have closed, sent fin
7: 'CLOSING', # closed exchanged FIN; awaiting FIN ACK
8: 'LAST_ACK', # had fin and close; awaiting FIN ACK
# states > CLOSE_WAIT and < FIN_WAIT_2 await ACK of FIN
9: 'FIN_WAIT_2', # have closed, fin is acked
10: 'TIME_WAIT'
}

def format_tcp_state_sample(sample):
sample_str = sample.format_data(sample)
states = re.findall(".*(\d+) -> (\d+).*", sample_str)
if states == []:
return sample_str
else:
[(state_from, state_to)] = states
state_from_str = tcp_states.get(int(state_from))
state_to_str = tcp_states.get(int(state_to))
return "%s%s -> %s" % (sample_str[:-6], state_from_str or 'Unknown', state_to_str or 'Unknown')

def is_net_packet_sample(sample):
return sample.name.startswith('net_packet_')

def is_tcp_state_sample(sample):
return sample.name == "tcp_state"

def is_input_net_packet_sample(sample):
return sample.name == "net_packet_in"

Expand Down

0 comments on commit 46b79c2

Please sign in to comment.