Bug 1934935

Summary: RFE: 'tcpconnlat': add LPORT while listing socket information
Product: Red Hat Enterprise Linux 8 Reporter: suresh kumar <surkumar>
Component: bccAssignee: Jerome Marchand <jmarchan>
Status: CLOSED ERRATA QA Contact: Zhiqian Guan <zhguan>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 8.4CC: jmarchan, jolsa, rdossant, skozina, zhguan
Target Milestone: rcKeywords: FutureFeature, Triaged
Target Release: ---   
Hardware: All   
OS: All   
Whiteboard:
Fixed In Version: bcc-0.19.0-2.el8 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2021-11-09 18:13:24 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description suresh kumar 2021-03-04 04:37:36 UTC
Description of problem:
'tcpconnlat' is listing only local IP address while listing socket information. If we include 'LPORT' along with IP address, it will help in troubleshooting while cross checking  'tcpconnect' output with wireshark or netstat/ss outputs etc.


Version-Release number of selected component (if applicable):
bcc-tools-0.16.0-2.el8.x86_64


How reproducible:


Steps to Reproduce:
1.run 'tcpconnlat'  and wait for output
2.
3.

Actual results:
1 warning generated.
PID    COMM         IP SADDR            DADDR            DPORT LAT(ms)
5186   ssh          4  192.168.122.205  192.168.122.150  22    0.18
5194   ssh          6  fe80::1          fe80::2          22    0.37


Expected results:
Include 'LPORT'


Additional info:

Comment 2 suresh kumar 2021-03-08 01:49:10 UTC
submitted below patch to upstream  at https://github.com/iovisor/bcc/pull/3298/commits/ffa330f602b1b8ba69e888869bb0e0e296d17da1

+++
commit ffa330f602b1b8ba69e888869bb0e0e296d17da1 (HEAD -> tcpconnlat.py, origin/tcpconnlat.py)
Author: suresh2514 <suresh2514>
Date:   Mon Mar 8 07:09:32 2021 +0530

    tools: add LPORT option for tcpconnlat output

diff --git a/tools/tcpconnlat.py b/tools/tcpconnlat.py
index e28a43a7..1a96312d 100755
--- a/tools/tcpconnlat.py
+++ b/tools/tcpconnlat.py
@@ -13,6 +13,7 @@
 # Licensed under the Apache License, Version 2.0 (the "License")
 #
 # 19-Feb-2016   Brendan Gregg   Created this.
+# 05-Mar-2021   Suresh Kumar    Added LPORT option
 
 from __future__ import print_function
 from bcc import BPF
@@ -85,6 +86,7 @@ struct ipv4_data_t {
     u32 saddr;
     u32 daddr;
     u64 ip;
+    u16 lport;
     u16 dport;
     u64 delta_us;
     char task[TASK_COMM_LEN];
@@ -97,6 +99,7 @@ struct ipv6_data_t {
     unsigned __int128 saddr;
     unsigned __int128 daddr;
     u64 ip;
+    u16 lport;
     u16 dport;
     u64 delta_us;
     char task[TASK_COMM_LEN];
@@ -142,8 +145,9 @@ int trace_tcp_rcv_state_process(struct pt_regs *ctx, struct sock *skp)
 #endif
 
     // pull in details
-    u16 family = 0, dport = 0;
+    u16 family = 0, lport = 0, dport = 0;
     family = skp->__sk_common.skc_family;
+    lport = skp->__sk_common.skc_num;
     dport = skp->__sk_common.skc_dport;

     // emit to appropriate data path
@@ -152,6 +156,8 @@ int trace_tcp_rcv_state_process(struct pt_regs *ctx, struct sock *skp)
         data4.ts_us = now / 1000;
         data4.saddr = skp->__sk_common.skc_rcv_saddr;
         data4.daddr = skp->__sk_common.skc_daddr;
+        u16 lport = skp->__sk_common.skc_num;
+        data4.lport = lport;
         data4.dport = ntohs(dport);
         data4.delta_us = delta_us;
         __builtin_memcpy(&data4.task, infop->task, sizeof(data4.task));
@@ -164,6 +170,7 @@ int trace_tcp_rcv_state_process(struct pt_regs *ctx, struct sock *skp)
             skp->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32);
         bpf_probe_read_kernel(&data6.daddr, sizeof(data6.daddr),
             skp->__sk_common.skc_v6_daddr.in6_u.u6_addr32);
+        data6.lport = lport;
         data6.dport = ntohs(dport);
         data6.delta_us = delta_us;
         __builtin_memcpy(&data6.task, infop->task, sizeof(data6.task));
@@ -208,9 +215,9 @@ def print_ipv4_event(cpu, data, size):
         if start_ts == 0:
             start_ts = event.ts_us
         print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="")
-    print("%-6d %-12.12s %-2d %-16s %-16s %-5d %.2f" % (event.pid,
+    print("%-6d %-12.12s %-2d %-16s %-6d %-16s %-6d %.2f" % (event.pid,
         event.task.decode('utf-8', 'replace'), event.ip,
-        inet_ntop(AF_INET, pack("I", event.saddr)),
+        inet_ntop(AF_INET, pack("I", event.saddr)), event.lport, 
         inet_ntop(AF_INET, pack("I", event.daddr)), event.dport,
         float(event.delta_us) / 1000))
 
@@ -221,16 +228,16 @@ def print_ipv6_event(cpu, data, size):
         if start_ts == 0:
             start_ts = event.ts_us
         print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="")
-    print("%-6d %-12.12s %-2d %-16s %-16s %-5d %.2f" % (event.pid,
+    print("%-6d %-12.12s %-2d %-16s %-6d %-16s %-6d %.2f" % (event.pid,
         event.task.decode('utf-8', 'replace'), event.ip,
-        inet_ntop(AF_INET6, event.saddr), inet_ntop(AF_INET6, event.daddr),
-        event.dport, float(event.delta_us) / 1000))
+        inet_ntop(AF_INET6, event.saddr), event.lport,
+        inet_ntop(AF_INET6, event.daddr), event.dport, float(event.delta_us) / 1000))
 
 # header
 if args.timestamp:
     print("%-9s" % ("TIME(s)"), end="")
-print("%-6s %-12s %-2s %-16s %-16s %-5s %s" % ("PID", "COMM", "IP", "SADDR",
-    "DADDR", "DPORT", "LAT(ms)"))
+print("%-6s %-12s %-2s %-16s %-6s %-16s %-6s %s" % ("PID", "COMM", "IP", "SADDR",
+    "LPORT", "DADDR", "DPORT", "LAT(ms)"))
 
 # read events
 b["ipv4_events"].open_perf_buffer(print_ipv4_event)

+++

Comment 3 suresh kumar 2021-03-15 03:45:02 UTC
Submitted upstream commit: https://github.com/iovisor/bcc/pull/3316/commits/552693d60905f53b1ffdb01905fca4fa9adb9422

Added '-L' option to list LPORT
+++
./tcpconnlat -L
PID    COMM         IP SADDR            LPORT  DADDR            DPORT LAT(ms)
2734   cups-browsed 4  192.168.8.95     53564  192.168.5.17     631   40.00
2734   cups-browsed 4  192.168.8.95     53566  192.168.5.17     631   39.69
2734   cups-browsed 4  192.168.8.95     53572  192.168.5.17     631   40.05
3467   python3      6  ::1              39340  ::1              3000  0.02
3467   python3      4  127.0.0.1        60846  127.0.0.1        3000  0.04
+++

Comment 4 suresh kumar 2021-03-17 09:27:55 UTC
commit merged to upstream (456cb957cea4ffafa217a1f8a4b90983bce09799)

Comment 5 Zhiqian Guan 2021-04-19 03:22:39 UTC
Hi Jerome,
Do you think this could be added to RHEL8.5? if so, please set a DTM, thanks:)

Comment 6 Jerome Marchand 2021-04-19 11:59:26 UTC
(In reply to Zhiqian Guan from comment #5)
> Hi Jerome,
> Do you think this could be added to RHEL8.5? if so, please set a DTM,
> thanks:)

This should be fixed by the next rebase, so setting the DTM accordingly.

Comment 8 Zhiqian Guan 2021-04-20 01:50:22 UTC
(In reply to Jerome Marchand from comment #6)
> (In reply to Zhiqian Guan from comment #5)
> > Hi Jerome,
> > Do you think this could be added to RHEL8.5? if so, please set a DTM,
> > thanks:)
> 
> This should be fixed by the next rebase, so setting the DTM accordingly.

OK, thank you!

Comment 10 Zhiqian Guan 2021-05-08 08:16:04 UTC
Pre-verify with ipv4/v6, looks good.
[root@netqe3 tools]# ./tcpconnlat -L
PID    COMM         IP SADDR            LPORT  DADDR            DPORT LAT(ms)
38098  ncat         4  192.168.1.2      9904   192.168.1.1      9904  0.12
38101  ncat         6  2000::1          45504  2000::2          9904  0.14
38106  ncat         4  192.168.1.2      57136  192.168.1.1      9904  0.10
38111  ncat         6  2000::1          9904   2000::2          9904  0.12
^C[root@netqe3 tools]# rpm -q bcc
bcc-0.19.0-2.el8.x86_64
[root@netqe3 tools]# uname -r
4.18.0-305.1.el8.x86_64
[root@netqe3 tools]#

Comment 13 Zhiqian Guan 2021-05-18 08:28:28 UTC
Test by using ncat to establish IPv4/6 connection, results look fine

[root@netqe3 tools]# ./tcpconnlat -h
usage: tcpconnlat [-h] [-t] [-p PID] [-L] [-v] [duration_ms]

Trace TCP connects and show connection latency

positional arguments:
  duration_ms        minimum duration to trace (ms)

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

examples:
    ./tcpconnlat           # trace all TCP connect()s
    ./tcpconnlat 1         # trace connection latency slower than 1 ms
    ./tcpconnlat 0.1       # trace connection latency slower than 100 us
    ./tcpconnlat -t        # include timestamps
    ./tcpconnlat -p 181    # only trace PID 181
    ./tcpconnlat -L        # include LPORT while printing outputs
[root@netqe3 tools]# ./tcpconnlat -L
PID    COMM         IP SADDR            LPORT  DADDR            DPORT LAT(ms)
39300  ncat         4  192.168.1.2      42738  192.168.1.1      9904  0.14
39309  ncat         4  192.168.1.2      9900   192.168.1.1      9904  0.12
39312  ncat         4  192.168.1.2      9900   192.168.1.1      9904  0.11
39332  ncat         6  2000::2          36908  2000::1          9904  0.35
39341  ncat         6  2000::2          9900   2000::1          9904  0.12
[root@netqe3 tools]# ./tcpconnlat --lport
PID    COMM         IP SADDR            LPORT  DADDR            DPORT LAT(ms)
42403  ncat         4  192.168.1.2      9900   192.168.1.1      9904  0.11
42414  ncat         4  192.168.1.2      42790  192.168.1.1      9904  0.11
42417  ncat         6  2000::2          36960  2000::1          9904  0.13
42420  ncat         6  2000::2          9900   2000::1          9904  0.11
^C[root@netqe3 tools]# rpm -q bcc
bcc-0.19.0-2.el8.x86_64
[root@netqe3 tools]#

Comment 14 Zhiqian Guan 2021-05-18 08:36:25 UTC
Base on the test results in Comment13, set this to VERIFIED

Comment 16 errata-xmlrpc 2021-11-09 18:13:24 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (bcc bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2021:4205