libbpf-tools: add option to include 'LPORT' in tcpconnlat
authorchendotjs <chendotjs@gmail.com>
Tue, 30 Nov 2021 03:56:03 +0000 (03:56 +0000)
committerchendotjs <chendotjs@gmail.com>
Tue, 30 Nov 2021 03:57:12 +0000 (03:57 +0000)
Signed-off-by: chendotjs <chendotjs@gmail.com>
libbpf-tools/tcpconnlat.bpf.c
libbpf-tools/tcpconnlat.c
libbpf-tools/tcpconnlat.h

index 7e0940d42fcdb99f80433714c20a6bafc3985842..56d374144b0fd09219b9125620937bbed2c5aaa1 100644 (file)
@@ -36,7 +36,7 @@ static __always_inline int trace_connect(struct sock *sk)
        u32 tgid = bpf_get_current_pid_tgid() >> 32;
        struct piddata piddata = {};
 
-       if (targ_tgid  && targ_tgid != tgid)
+       if (targ_tgid && targ_tgid != tgid)
                return 0;
 
        bpf_get_current_comm(&piddata.comm, sizeof(piddata.comm));
@@ -85,6 +85,7 @@ int BPF_PROG(tcp_rcv_state_process, struct sock *sk)
                        sizeof(event.comm));
        event.ts_us = ts / 1000;
        event.tgid = piddatap->tgid;
+       event.lport = sk->__sk_common.skc_num;
        event.dport = sk->__sk_common.skc_dport;
        event.af = sk->__sk_common.skc_family;
        if (event.af == AF_INET) {
index 3e06447f1068cb9c3d04905c7a96158b3b429881..870fa436b1ac32e43a2e3d3fc69f2705fd5b4092 100644 (file)
@@ -24,6 +24,7 @@ static struct env {
        __u64 min_us;
        pid_t pid;
        bool timestamp;
+       bool lport;
        bool verbose;
 } env;
 
@@ -33,18 +34,20 @@ const char *argp_program_bug_address =
 const char argp_program_doc[] =
 "\nTrace TCP connects and show connection latency.\n"
 "\n"
-"USAGE: tcpconnlat [--help] [-t] [-p PID]\n"
+"USAGE: tcpconnlat [--help] [-t] [-p PID] [-L]\n"
 "\n"
 "EXAMPLES:\n"
-"    tcpconnlat              # summarize on-CPU time as a histogram"
-"    tcpconnlat 1            # trace connection latency slower than 1 ms"
-"    tcpconnlat 0.1          # trace connection latency slower than 100 us"
-"    tcpconnlat -t           # 1s summaries, milliseconds, and timestamps"
-"    tcpconnlat -p 185       # trace PID 185 only";
+"    tcpconnlat              # summarize on-CPU time as a histogram\n"
+"    tcpconnlat 1            # trace connection latency slower than 1 ms\n"
+"    tcpconnlat 0.1          # trace connection latency slower than 100 us\n"
+"    tcpconnlat -t           # 1s summaries, milliseconds, and timestamps\n"
+"    tcpconnlat -p 185       # trace PID 185 only\n"
+"    tcpconnlat -L           # include LPORT while printing outputs\n";
 
 static const struct argp_option opts[] = {
        { "timestamp", 't', NULL, 0, "Include timestamp on output" },
        { "pid", 'p', "PID", 0, "Trace this PID only" },
+       { "lport", 'L', NULL, 0, "Include LPORT on output" },
        { "verbose", 'v', NULL, 0, "Verbose debug output" },
        { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
        {},
@@ -72,6 +75,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
        case 't':
                env.timestamp = true;
                break;
+       case 'L':
+               env.lport = true;
+               break;
        case ARGP_KEY_ARG:
                if (pos_args++) {
                        fprintf(stderr,
@@ -131,10 +137,17 @@ void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
                return;
        }
 
-       printf("%-6d %-12.12s %-2d %-16s %-16s %-5d %.2f\n", e->tgid, e->comm,
-               e->af == AF_INET ? 4 : 6, inet_ntop(e->af, &s, src, sizeof(src)),
-               inet_ntop(e->af, &d, dst, sizeof(dst)), ntohs(e->dport),
-               e->delta_us / 1000.0);
+       if (env.lport) {
+               printf("%-6d %-12.12s %-2d %-16s %-6d %-16s %-5d %.2f\n", e->tgid, e->comm,
+                       e->af == AF_INET ? 4 : 6, inet_ntop(e->af, &s, src, sizeof(src)), e->lport,
+                       inet_ntop(e->af, &d, dst, sizeof(dst)), ntohs(e->dport),
+                       e->delta_us / 1000.0);
+       } else {
+               printf("%-6d %-12.12s %-2d %-16s %-16s %-5d %.2f\n", e->tgid, e->comm,
+                       e->af == AF_INET ? 4 : 6, inet_ntop(e->af, &s, src, sizeof(src)),
+                       inet_ntop(e->af, &d, dst, sizeof(dst)), ntohs(e->dport),
+                       e->delta_us / 1000.0);
+       }
 }
 
 void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
@@ -199,10 +212,17 @@ int main(int argc, char **argv)
                goto cleanup;
        }
 
+       /* print header */
        if (env.timestamp)
                printf("%-9s ", ("TIME(s)"));
-       printf("%-6s %-12s %-2s %-16s %-16s %-5s %s\n",
-               "PID", "COMM", "IP", "SADDR", "DADDR", "DPORT", "LAT(ms)");
+       if (env.lport) {
+               printf("%-6s %-12s %-2s %-16s %-6s %-16s %-5s %s\n",
+                       "PID", "COMM", "IP", "SADDR", "LPORT", "DADDR", "DPORT", "LAT(ms)");
+       } else {
+               printf("%-6s %-12s %-2s %-16s %-16s %-5s %s\n",
+                       "PID", "COMM", "IP", "SADDR", "DADDR", "DPORT", "LAT(ms)");
+       }
+
 
        if (signal(SIGINT, sig_int) == SIG_ERR) {
                fprintf(stderr, "can't set signal handler: %s\n", strerror(errno));
index 208a71d129769c4b550ef917799bc88481a7eb9f..9dbddfc66928022d630eca8e1e30f2ccca06d5ab 100644 (file)
@@ -18,6 +18,7 @@ struct event {
        __u64 ts_us;
        __u32 tgid;
        int af;
+       __u16 lport;
        __u16 dport;
 };