Skip to content

Commit 456cb95

Browse files
suresh2514yonghong-song
authored andcommitted
tools: add option to include 'LPORT' in tcpconnlat, update man pages
1 parent d80afb2 commit 456cb95

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

man/man8/tcpconnlat.8

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.SH NAME
33
tcpconnlat \- Trace TCP active connection latency. Uses Linux eBPF/bcc.
44
.SH SYNOPSIS
5-
.B tcpconnlat [\-h] [\-t] [\-p PID] [-v] [min_ms]
5+
.B tcpconnlat [\-h] [\-t] [\-p PID] [\-L] [-v] [min_ms]
66
.SH DESCRIPTION
77
This tool traces active TCP connections
88
(eg, via a connect() syscall), and shows the latency (time) for the connection
@@ -31,6 +31,9 @@ Include a timestamp column.
3131
\-p PID
3232
Trace this process ID only (filtered in-kernel).
3333
.TP
34+
\-L
35+
Include a LPORT column.
36+
.TP
3437
\-v
3538
Print the resulting BPF program, for debugging purposes.
3639
.TP
@@ -50,6 +53,10 @@ Trace PID 181 only:
5053
#
5154
.B tcpconnlat \-p 181
5255
.TP
56+
Trace connects, and include LPORT:
57+
#
58+
.B tcpconnlat \-L
59+
.TP
5360
Trace connects with latency longer than 10 ms:
5461
#
5562
.B tcpconnlat 10
@@ -77,6 +84,9 @@ Source IP address.
7784
DADDR
7885
Destination IP address.
7986
.TP
87+
LPORT
88+
Source port
89+
.TP
8090
DPORT
8191
Destination port
8292
.TP

tools/tcpconnlat.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# Licensed under the Apache License, Version 2.0 (the "License")
1414
#
1515
# 19-Feb-2016 Brendan Gregg Created this.
16+
# 15-Mar-2021 Suresh Kumar Added LPORT option
1617

1718
from __future__ import print_function
1819
from bcc import BPF
@@ -38,6 +39,7 @@ def positive_float(val):
3839
./tcpconnlat 0.1 # trace connection latency slower than 100 us
3940
./tcpconnlat -t # include timestamps
4041
./tcpconnlat -p 181 # only trace PID 181
42+
./tcpconnlat -L # include LPORT while printing outputs
4143
"""
4244
parser = argparse.ArgumentParser(
4345
description="Trace TCP connects and show connection latency",
@@ -47,6 +49,8 @@ def positive_float(val):
4749
help="include timestamp on output")
4850
parser.add_argument("-p", "--pid",
4951
help="trace this PID only")
52+
parser.add_argument("-L", "--lport", action="store_true",
53+
help="include LPORT on output")
5054
parser.add_argument("duration_ms", nargs="?", default=0,
5155
type=positive_float,
5256
help="minimum duration to trace (ms)")
@@ -85,6 +89,7 @@ def positive_float(val):
8589
u32 saddr;
8690
u32 daddr;
8791
u64 ip;
92+
u16 lport;
8893
u16 dport;
8994
u64 delta_us;
9095
char task[TASK_COMM_LEN];
@@ -97,6 +102,7 @@ def positive_float(val):
97102
unsigned __int128 saddr;
98103
unsigned __int128 daddr;
99104
u64 ip;
105+
u16 lport;
100106
u16 dport;
101107
u64 delta_us;
102108
char task[TASK_COMM_LEN];
@@ -142,8 +148,9 @@ def positive_float(val):
142148
#endif
143149
144150
// pull in details
145-
u16 family = 0, dport = 0;
151+
u16 family = 0, lport = 0, dport = 0;
146152
family = skp->__sk_common.skc_family;
153+
lport = skp->__sk_common.skc_num;
147154
dport = skp->__sk_common.skc_dport;
148155
149156
// emit to appropriate data path
@@ -152,6 +159,7 @@ def positive_float(val):
152159
data4.ts_us = now / 1000;
153160
data4.saddr = skp->__sk_common.skc_rcv_saddr;
154161
data4.daddr = skp->__sk_common.skc_daddr;
162+
data4.lport = lport;
155163
data4.dport = ntohs(dport);
156164
data4.delta_us = delta_us;
157165
__builtin_memcpy(&data4.task, infop->task, sizeof(data4.task));
@@ -164,6 +172,7 @@ def positive_float(val):
164172
skp->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32);
165173
bpf_probe_read_kernel(&data6.daddr, sizeof(data6.daddr),
166174
skp->__sk_common.skc_v6_daddr.in6_u.u6_addr32);
175+
data6.lport = lport;
167176
data6.dport = ntohs(dport);
168177
data6.delta_us = delta_us;
169178
__builtin_memcpy(&data6.task, infop->task, sizeof(data6.task));
@@ -208,11 +217,18 @@ def print_ipv4_event(cpu, data, size):
208217
if start_ts == 0:
209218
start_ts = event.ts_us
210219
print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="")
211-
print("%-6d %-12.12s %-2d %-16s %-16s %-5d %.2f" % (event.pid,
212-
event.task.decode('utf-8', 'replace'), event.ip,
213-
inet_ntop(AF_INET, pack("I", event.saddr)),
214-
inet_ntop(AF_INET, pack("I", event.daddr)), event.dport,
215-
float(event.delta_us) / 1000))
220+
if args.lport:
221+
print("%-6d %-12.12s %-2d %-16s %-6d %-16s %-5d %.2f" % (event.pid,
222+
event.task.decode('utf-8', 'replace'), event.ip,
223+
inet_ntop(AF_INET, pack("I", event.saddr)), event.lport,
224+
inet_ntop(AF_INET, pack("I", event.daddr)), event.dport,
225+
float(event.delta_us) / 1000))
226+
else:
227+
print("%-6d %-12.12s %-2d %-16s %-16s %-5d %.2f" % (event.pid,
228+
event.task.decode('utf-8', 'replace'), event.ip,
229+
inet_ntop(AF_INET, pack("I", event.saddr)),
230+
inet_ntop(AF_INET, pack("I", event.daddr)), event.dport,
231+
float(event.delta_us) / 1000))
216232

217233
def print_ipv6_event(cpu, data, size):
218234
event = b["ipv6_events"].event(data)
@@ -221,16 +237,27 @@ def print_ipv6_event(cpu, data, size):
221237
if start_ts == 0:
222238
start_ts = event.ts_us
223239
print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="")
224-
print("%-6d %-12.12s %-2d %-16s %-16s %-5d %.2f" % (event.pid,
225-
event.task.decode('utf-8', 'replace'), event.ip,
226-
inet_ntop(AF_INET6, event.saddr), inet_ntop(AF_INET6, event.daddr),
227-
event.dport, float(event.delta_us) / 1000))
240+
if args.lport:
241+
print("%-6d %-12.12s %-2d %-16s %-6d %-16s %-5d %.2f" % (event.pid,
242+
event.task.decode('utf-8', 'replace'), event.ip,
243+
inet_ntop(AF_INET6, event.saddr), event.lport,
244+
inet_ntop(AF_INET6, event.daddr),
245+
event.dport, float(event.delta_us) / 1000))
246+
else:
247+
print("%-6d %-12.12s %-2d %-16s %-16s %-5d %.2f" % (event.pid,
248+
event.task.decode('utf-8', 'replace'), event.ip,
249+
inet_ntop(AF_INET6, event.saddr), inet_ntop(AF_INET6, event.daddr),
250+
event.dport, float(event.delta_us) / 1000))
228251

229252
# header
230253
if args.timestamp:
231254
print("%-9s" % ("TIME(s)"), end="")
232-
print("%-6s %-12s %-2s %-16s %-16s %-5s %s" % ("PID", "COMM", "IP", "SADDR",
233-
"DADDR", "DPORT", "LAT(ms)"))
255+
if args.lport:
256+
print("%-6s %-12s %-2s %-16s %-6s %-16s %-5s %s" % ("PID", "COMM",
257+
"IP", "SADDR", "LPORT", "DADDR", "DPORT", "LAT(ms)"))
258+
else:
259+
print("%-6s %-12s %-2s %-16s %-16s %-5s %s" % ("PID", "COMM", "IP",
260+
"SADDR", "DADDR", "DPORT", "LAT(ms)"))
234261

235262
# read events
236263
b["ipv4_events"].open_perf_buffer(print_ipv4_event)

tools/tcpconnlat_example.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,24 @@ if the response is a RST (port closed).
3434
USAGE message:
3535

3636
# ./tcpconnlat -h
37-
usage: tcpconnlat [-h] [-t] [-p PID] [min_ms]
37+
usage: tcpconnlat [-h] [-t] [-p PID] [-L] [-v] [duration_ms]
3838

3939
Trace TCP connects and show connection latency
4040

4141
positional arguments:
42-
min_ms minimum duration to trace, in ms (default 0)
42+
duration_ms minimum duration to trace (ms)
4343

4444
optional arguments:
4545
-h, --help show this help message and exit
4646
-t, --timestamp include timestamp on output
4747
-p PID, --pid PID trace this PID only
48+
-L, --lport include LPORT on output
49+
-v, --verbose print the BPF program for debugging purposes
4850

4951
examples:
5052
./tcpconnlat # trace all TCP connect()s
53+
./tcpconnlat 1 # trace connection latency slower than 1 ms
54+
./tcpconnlat 0.1 # trace connection latency slower than 100 us
5155
./tcpconnlat -t # include timestamps
5256
./tcpconnlat -p 181 # only trace PID 181
53-
./tcpconnlat 1 # only show connects longer than 1 ms
54-
./tcpconnlat 0.1 # only show connects longer than 100 us
55-
./tcpconnlat -v # Show the BPF program
57+
./tcpconnlat -L # include LPORT while printing outputs

0 commit comments

Comments
 (0)