Skip to content

Commit bfa05d2

Browse files
Rtoaxekyooo
authored andcommitted
python: tcp.py: add state2str() and apply to tools
There are two tools/{tcpretrans.py,tcpstates.py,tcpdrop.py} display the TCP state, and those code already exist in bcc/tcp.py, thus, this patch remove the duplicate code and add bcc.tcp.state2str() function. Signed-off-by: Rong Tao <[email protected]>
1 parent 7676954 commit bfa05d2

File tree

4 files changed

+27
-61
lines changed

4 files changed

+27
-61
lines changed

src/python/bcc/tcp.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
# limitations under the License.
1414

1515
# from include/net/tcp_states.h:
16-
tcpstate = {}
17-
tcpstate[1] = 'ESTABLISHED'
18-
tcpstate[2] = 'SYN_SENT'
19-
tcpstate[3] = 'SYN_RECV'
20-
tcpstate[4] = 'FIN_WAIT1'
21-
tcpstate[5] = 'FIN_WAIT2'
22-
tcpstate[6] = 'TIME_WAIT'
23-
tcpstate[7] = 'CLOSE'
24-
tcpstate[8] = 'CLOSE_WAIT'
25-
tcpstate[9] = 'LAST_ACK'
26-
tcpstate[10] = 'LISTEN'
27-
tcpstate[11] = 'CLOSING'
28-
tcpstate[12] = 'NEW_SYN_RECV'
16+
_tcpstate = {}
17+
_tcpstate[1] = 'ESTABLISHED'
18+
_tcpstate[2] = 'SYN_SENT'
19+
_tcpstate[3] = 'SYN_RECV'
20+
_tcpstate[4] = 'FIN_WAIT1'
21+
_tcpstate[5] = 'FIN_WAIT2'
22+
_tcpstate[6] = 'TIME_WAIT'
23+
_tcpstate[7] = 'CLOSE'
24+
_tcpstate[8] = 'CLOSE_WAIT'
25+
_tcpstate[9] = 'LAST_ACK'
26+
_tcpstate[10] = 'LISTEN'
27+
_tcpstate[11] = 'CLOSING'
28+
_tcpstate[12] = 'NEW_SYN_RECV'
2929

3030
# from include/net/tcp.h:
3131
TCPHDR_FIN = 0x01
@@ -56,3 +56,6 @@ def flags2str(flags):
5656
if flags & TCPHDR_CWR:
5757
arr.append("CWR")
5858
return "|".join(arr)
59+
60+
def state2str(state):
61+
return _tcpstate.get(state, str(state))

tools/tcpdrop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@
342342
def print_ipv4_event(cpu, data, size):
343343
event = b["ipv4_events"].event(data)
344344
reason_str = drop_reasons.get(event.drop_reason, "UNKNOWN")
345-
state_flag_str = "%s (%s)" % (tcp.tcpstate[event.state], tcp.flags2str(event.tcpflags))
345+
state_flag_str = "%s (%s)" % (tcp.state2str(event.state), tcp.flags2str(event.tcpflags))
346346
print("%-8s %-7d %-2d %-20s > %-20s %-20s %s (%d)" % (
347347
strftime("%H:%M:%S"), event.pid, event.ip,
348348
"%s:%d" % (inet_ntop(AF_INET, pack('I', event.saddr)), event.sport),
@@ -356,7 +356,7 @@ def print_ipv4_event(cpu, data, size):
356356
def print_ipv6_event(cpu, data, size):
357357
event = b["ipv6_events"].event(data)
358358
reason_str = drop_reasons.get(event.drop_reason, "UNKNOWN")
359-
state_flag_str = "%s (%s)" % (tcp.tcpstate[event.state], tcp.flags2str(event.tcpflags))
359+
state_flag_str = "%s (%s)" % (tcp.state2str(event.state), tcp.flags2str(event.tcpflags))
360360
print("%-8s %-7d %-2d %-20s > %-20s %-20s %s (%d)" % (
361361
strftime("%H:%M:%S"), event.pid, event.ip,
362362
"%s:%d" % (inet_ntop(AF_INET6, event.saddr), event.sport),

tools/tcpretrans.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# 03-Nov-2017 Matthias Tafelmeier Extended this.
1717

1818
from __future__ import print_function
19-
from bcc import BPF
19+
from bcc import BPF, tcp
2020
import argparse
2121
from time import strftime
2222
from socket import inet_ntop, AF_INET, AF_INET6
@@ -337,21 +337,6 @@
337337
type[1] = 'R'
338338
type[2] = 'L'
339339

340-
# from include/net/tcp_states.h:
341-
tcpstate = {}
342-
tcpstate[1] = 'ESTABLISHED'
343-
tcpstate[2] = 'SYN_SENT'
344-
tcpstate[3] = 'SYN_RECV'
345-
tcpstate[4] = 'FIN_WAIT1'
346-
tcpstate[5] = 'FIN_WAIT2'
347-
tcpstate[6] = 'TIME_WAIT'
348-
tcpstate[7] = 'CLOSE'
349-
tcpstate[8] = 'CLOSE_WAIT'
350-
tcpstate[9] = 'LAST_ACK'
351-
tcpstate[10] = 'LISTEN'
352-
tcpstate[11] = 'CLOSING'
353-
tcpstate[12] = 'NEW_SYN_RECV'
354-
355340
# process event
356341
def print_ipv4_event(cpu, data, size):
357342
event = b["ipv4_events"].event(data)
@@ -362,9 +347,9 @@ def print_ipv4_event(cpu, data, size):
362347
"%s:%s" % (inet_ntop(AF_INET, pack('I', event.daddr)), event.dport)),
363348
end='')
364349
if args.sequence:
365-
print(" %-12s %s" % (tcpstate[event.state], event.seq))
350+
print(" %-12s %s" % (tcp.state2str(event.state), event.seq))
366351
else:
367-
print(" %s" % (tcpstate[event.state]))
352+
print(" %s" % (tcp.state2str(event.state)))
368353

369354
def print_ipv6_event(cpu, data, size):
370355
event = b["ipv6_events"].event(data)
@@ -375,9 +360,9 @@ def print_ipv6_event(cpu, data, size):
375360
"%s:%d" % (inet_ntop(AF_INET6, event.daddr), event.dport)),
376361
end='')
377362
if args.sequence:
378-
print(" %-12s %s" % (tcpstate[event.state], event.seq))
363+
print(" %-12s %s" % (tcp.state2str(event.state), event.seq))
379364
else:
380-
print(" %s" % (tcpstate[event.state]))
365+
print(" %s" % (tcp.state2str(event.state)))
381366

382367
def depict_cnt(counts_tab, l3prot='ipv4'):
383368
for k, v in sorted(counts_tab.items(), key=lambda counts: counts[1].value):

tools/tcpstates.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# 20-Mar-2018 Brendan Gregg Created this.
1717

1818
from __future__ import print_function
19-
from bcc import BPF
19+
from bcc import BPF, tcp
2020
import argparse
2121
from socket import inet_ntop, AF_INET, AF_INET6
2222
import sys
@@ -312,28 +312,6 @@
312312
exit(1)
313313

314314

315-
def tcpstate2str(state):
316-
# from include/net/tcp_states.h:
317-
tcpstate = {
318-
1: "ESTABLISHED",
319-
2: "SYN_SENT",
320-
3: "SYN_RECV",
321-
4: "FIN_WAIT1",
322-
5: "FIN_WAIT2",
323-
6: "TIME_WAIT",
324-
7: "CLOSE",
325-
8: "CLOSE_WAIT",
326-
9: "LAST_ACK",
327-
10: "LISTEN",
328-
11: "CLOSING",
329-
12: "NEW_SYN_RECV",
330-
}
331-
332-
if state in tcpstate:
333-
return tcpstate[state]
334-
else:
335-
return str(state)
336-
337315
def journal_fields(event, addr_family):
338316
addr_pfx = 'IPV4'
339317
if addr_family == AF_INET6:
@@ -354,8 +332,8 @@ def journal_fields(event, addr_family):
354332
'OBJECT_TCP_SOURCE_PORT': str(event.lport),
355333
'OBJECT_' + addr_pfx + '_DESTINATION_ADDRESS': inet_ntop(addr_family, event.daddr),
356334
'OBJECT_TCP_DESTINATION_PORT': str(event.dport),
357-
'OBJECT_TCP_OLD_STATE': tcpstate2str(event.oldstate),
358-
'OBJECT_TCP_NEW_STATE': tcpstate2str(event.newstate),
335+
'OBJECT_TCP_OLD_STATE': tcp.state2str(event.oldstate),
336+
'OBJECT_TCP_NEW_STATE': tcp.state2str(event.newstate),
359337
'OBJECT_TCP_SPAN_TIME': str(event.span_us)
360338
}
361339

@@ -396,7 +374,7 @@ def print_event(event, addr_family):
396374
version if args.wide or args.csv else "",
397375
inet_ntop(addr_family, event.saddr), event.lport,
398376
inet_ntop(addr_family, event.daddr), event.dport,
399-
tcpstate2str(event.oldstate), tcpstate2str(event.newstate),
377+
tcp.state2str(event.oldstate), tcp.state2str(event.newstate),
400378
float(event.span_us) / 1000))
401379
if args.journal:
402380
journal.send(**journal_fields(event, addr_family))

0 commit comments

Comments
 (0)