From: Waldemar Kozaczuk <jwkozac...@gmail.com>
Committer: Waldemar Kozaczuk <jwkozac...@gmail.com>
Branch: master

trace.py: show human-readable TCP state transitions

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 <jwkozac...@gmail.com>

---
diff --git a/scripts/trace.py b/scripts/trace.py
--- a/scripts/trace.py
+++ b/scripts/trace.py
@@ -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):
@@ -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"
 

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/000000000000678f0005e507ff47%40google.com.

Reply via email to