DTRACE_TCP(4)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1, nixpkgs unstable

✨ NAME ✨

okay so bestie, welcome to dtrace_tcp(4) β€” a DTrace provider for tracing events related to the tcp(4) protocol. (Straight out of the Kernel Interfaces Manual, iykyk 🫢)


πŸ¦‹ SYNOPSIS πŸ¦‹

the probe signatures are giving very copy-me-exactly energy:

tcp:::accept-established(pktinfo_t *, csinfo_t *, ipinfo_t *,
    tcpsinfo_t *, tcpinfo_t *);

tcp:::accept-refused(pktinfo_t *, csinfo_t *, ipinfo_t *, tcpsinfo_t *,
    tcpinfo_t *);

tcp:::connect-established(pktinfo_t *, csinfo_t *, ipinfo_t *,
    tcpsinfo_t *, tcpinfo_t *);

tcp:::connect-refused(pktinfo_t *, csinfo_t *, ipinfo_t *, tcpsinfo_t *,
    tcpinfo_t *);

tcp:::connect-request(pktinfo_t *, csinfo_t *, ipinfo_t *, tcpsinfo_t *,
    tcpinfo_t *);

tcp:::receive(pktinfo_t *, csinfo_t *, ipinfo_t *, tcpsinfo_t *,
    tcpinfo_t *);

tcp:::send(pktinfo_t *, csinfo_t *, ipinfo_t *, tcpsinfo_t *,
    tcpinfo_t *);

tcp:::state-change(void *, csinfo_t *, void *, tcpsinfo_t *, void *,
    tcplsinfo_t *);

tcp:::siftr(siftrinfo_t *);

πŸ’… DESCRIPTION πŸ’…

The DTrace tcp provider is your backstage pass to trace events in the tcp(4) protocol implementation. It's lowkey similar to the dtrace_ip(4) and dtrace_udp(4) providers, but bestie, it also contains probes corresponding to protocol events at a level higher than packet reception and transmission. Iconic behavior, honestly. πŸ’–

All tcp probes except for tcp:::state-change() and tcp:::siftr() have the same number and type of arguments. The last three arguments are used to describe a TCP segment: the ipinfo_t argument exposes the version-agnostic fields of the IP header, while the tcpinfo_t argument exposes the TCP header, and the tcpsinfo_t argument describes details of the corresponding TCP connection state, if any. Their fields are described in the ARGUMENTS section. ✨

  • tcp:::accept-established() β€” fires when a remotely-initiated active TCP open succeeds. At this point the new connection is in the ESTABLISHED state, and the probe arguments expose the headers associated with the final ACK of the three-way handshake. πŸŽ€
  • tcp:::accept-refused() β€” fires when a SYN arrives on a port without a listening socket. The probe arguments expose the headers associated with the RST to be transmitted to the remote host in response to the SYN segment. πŸ’€

The tcp:::connect-established(), tcp:::connect-refused(), and tcp:::connect-request() probes are similar to the accept probes, except that they correspond to locally-initiated TCP connections. Periodt.

  • tcp:::connect-established() β€” fires when the SYN-ACK segment of a three-way handshake is received from the remote host and a final ACK is prepared for transmission. This occurs immediately after the local connection state transitions from SYN-SENT to ESTABLISHED. The probe arguments describe the headers associated with the received SYN-ACK segment. πŸ”₯
  • tcp:::connect-refused() β€” fires when the local host receives a RST segment in response to a SYN segment, indicating that the remote host refused to open a connection. The probe arguments describe the IP and TCP headers associated with the received RST segment. πŸ™ˆ
  • tcp:::connect-request() β€” fires as the kernel prepares to transmit the initial SYN segment of a three-way handshake. 🌈

The tcp:::send() and tcp:::receive() probes fire when the host sends or receives a TCP packet, respectively. As with the dtrace_udp(4) provider, tcp probes fire only for packets sent by or to the local host; forwarded packets are handled in the IP layer and are only visible to the dtrace_ip(4) provider. No cap. πŸ’―

The tcp:::state-change() probe fires upon local TCP connection state transitions. Its first, third and fifth arguments are currently always NULL. Its last argument describes the from-state in the transition, and the to-state can be obtained from args[3]->tcps_state. πŸ¦‹

The tcp:::siftr() probe fires when a TCP segment is sent or received by the host. For a detailed description see siftr(4). The siftrinfo_t argument provides the information about the TCP connection. 🌸

πŸ’… ARGUMENTS πŸ’…

πŸŽ€ pktinfo_t πŸŽ€

Okay so the pktinfo_t argument is currently unimplemented, bestie β€” it's literally just here for compatibility with other implementations of this provider. Its fields are:

  • uinptr_t pkt_addr β€” always set to 0. That's it. That's the field. πŸ’€

πŸ¦‹ csinfo_t πŸ¦‹

Same vibe: the csinfo_t argument is currently unimplemented and is included for compatibility with other implementations of this provider. Its fields are:

  • uintptr_t cs_addr β€” always set to 0.
  • uint64_t cs_cid β€” a pointer to the struct inpcb for this packet, or NULL.
  • pid_t cs_pid β€” always set to 0.

🌈 ipinfo_t 🌈

The ipinfo_t type is a version-agnostic representation of fields from an IP header. Its fields are described in the dtrace_ip(4) manual page. Go read that one for the full breakdown, iykyk. ✨

πŸ’– tcpsinfo_t πŸ’–

The tcpsinfo_t type gives you a stable representation of TCP connection state. Some tcp probes β€” like tcp:::accept-refused() β€” fire in a context where there is no TCP connection, and in that case this argument is NULL. Its fields are:

  • uintptr_t tcps_addr β€” the address of the corresponding TCP control block. Currently this is a pointer to a struct tcpcb.
  • int tcps_local β€” a boolean indicating whether the connection is local to the host. Currently unimplemented and always set to -1.
  • int tcps_active β€” a boolean indicating whether the connection was initiated by the local host. Currently unimplemented and always set to -1.
  • uint16_t tcps_lport β€” local TCP port.
  • uint16_t tcps_rport β€” remote TCP port.
  • string tcps_laddr β€” local address.
  • string tcps_raddr β€” remote address.
  • int32_t tcps_state β€” current TCP state. The valid TCP state values are given by the constants prefixed with TCPS_ in /usr/lib/dtrace/tcp.d.
  • uint32_t tcps_iss β€” initial send sequence number.
  • uint32_t tcps_suna β€” initial sequence number of sent but unacknowledged data.
  • uint32_t tcps_snxt β€” next sequence number for send.
  • uint32_t tcps_rack β€” sequence number of received and acknowledged data.
  • uint32_t tcps_rnxt β€” next expected sequence number for receive.
  • u_long tcps_swnd β€” TCP send window size.
  • int32_t tcps_snd_ws β€” window scaling factor for the TCP send window.
  • u_long tcps_rwnd β€” TCP receive window size.
  • int32_t tcps_rcv_ws β€” window scaling factor for the TCP receive window.
  • u_long tcps_cwnd β€” TCP congestion window size.
  • u_long tcps_cwnd_ssthresh β€” congestion window threshold at which slow start ends and congestion avoidance begins.
  • uint32_t tcps_sack_fack β€” last sequence number selectively acknowledged by the receiver.
  • uint32_t tcps_sack_snxt β€” next selectively acknowledge sequence number at which to begin retransmitting.
  • uint32_t tcps_rto β€” round-trip timeout, in milliseconds.
  • uint32_t tcps_mss β€” maximum segment size.
  • int tcps_retransmit β€” a boolean indicating that the local sender is retransmitting data.
  • int tcps_srtt β€” smoothed round-trip time.

🧚 tcpinfo_t 🧚

The tcpinfo_t type exposes the fields in a TCP segment header in host order. Its fields are:

  • uint16_t tcp_sport β€” source TCP port.
  • uint16_t tcp_dport β€” destination TCP port.
  • uint32_t tcp_seq β€” sequence number.
  • uint32_t tcp_ack β€” acknowledgement number.
  • uint8_t tcp_offset β€” data offset, in bytes.
  • uint8_t tcp_flags β€” TCP flags.
  • uint16_t tcp_window β€” TCP window size.
  • uint16_t tcp_checksum β€” checksum.
  • uint16_t tcp_urgent β€” urgent data pointer.
  • struct tcphdr *tcp_hdr β€” a pointer to the raw TCP header.

🌸 tcplsinfo_t 🌸

The tcplsinfo_t type is used by the tcp:::state-change() probe to provide the from-state of a transition. Its fields are:

  • int32_t tcps_state β€” a TCP state. The valid TCP state values are given by the constants prefixed with TCPS_ in /usr/lib/dtrace/tcp.d.

πŸ‘‘ siftrinfo_t πŸ‘‘

The siftrinfo_t type is used by the tcp:::siftr() probe to provide the state of the TCP connection. Its fields are:

  • uint8_t direction β€” direction of packet that triggered the log message. Either "0" for in, or "1" for out.
  • uint8_t ipver β€” the version of the IP protocol being used. Either "1" for IPv4, or "2" for IPv6.
  • uint16_t lport β€” the TCP port that the local host is communicating via.
  • uint16_t rport β€” the TCP port that the remote host is communicating via.
  • string laddr β€” the IPv4 or IPv6 address of the local host.
  • string raddr β€” the IPv4 or IPv6 address of the remote host.
  • uint32_t snd_cwnd β€” the current congestion window (CWND) for the flow, in bytes.
  • uint32_t snd_wnd β€” the current sending window for the flow, in bytes. The post scaled value is reported, except during the initial handshake (first few packets), during which time the unscaled value is reported.
  • uint32_t rcv_wnd β€” the current receive window for the flow, in bytes. The post scaled value is always reported.
  • uint32_t t_flags2 β€” the current value of the t_flags2 for the flow.
  • uint32_t snd_ssthresh β€” the slow start threshold (SSTHRESH) for the flow, in bytes.
  • int conn_state β€” a TCP state. The valid TCP state values are given by the constants prefixed with TCPS_ in /usr/lib/dtrace/tcp.d.
  • uint32_t mss β€” the maximum segment size (MSS) for the flow, in bytes.
  • uint32_t srtt β€” the current smoothed RTT (SRTT) for the flow in microseconds.
  • u_char sack_enabled β€” SACK enabled indicator. 1 if SACK enabled, 0 otherwise.
  • u_char snd_scale β€” the current window scaling factor for the sending window.
  • u_char rcv_scale β€” the current window scaling factor for the receiving window.
  • u_int t_flags β€” the current value of the t_flags for the flow.
  • uint32_t rto β€” the current retransmission timeout (RTO) for the flow in microseconds. Divide by HZ to get the timeout length in seconds.
  • u_int snd_buf_hiwater β€” the current size of the socket send buffer in bytes.
  • u_int snd_buf_cc β€” the current number of bytes in the socket send buffer.
  • u_int rcv_buf_hiwater β€” the current size of the socket receive buffer in bytes.
  • u_int rcv_buf_cc β€” the current number of bytes in the socket receive buffer.
  • u_int sent_inflight_bytes β€” the current number of unacknowledged bytes in-flight. Bytes acknowledged via SACK are not excluded from this count.
  • int t_segqlen β€” the current number of segments in the reassembly queue.
  • u_int flowid β€” flowid for the connection. A caveat: zero 0 either represents a valid flowid or a default value when the flowid is not being set.
  • u_int flowtype β€” flow type for the connection. Flowtype defines which protocol fields are hashed to produce the flowid. A complete listing is available in /usr/include/sys/mbuf.h under M_HASHTYPE_*.

πŸ“ FILES πŸ“

  • /usr/lib/dtrace/tcp.d β€” DTrace type and translator definitions for all the probes of the tcp provider except the siftr probe.
  • /usr/lib/dtrace/siftr.d β€” DTrace type and translator definitions for the siftr probe of the tcp provider.

✨ EXAMPLES ✨

Okay bestie, this is the fun part β€” actual scripts you can run fr fr. πŸ’…


🌸 Example 1: logging TCP segments in real time

This one prints every TCP segment as it flies by, in real time. It's giving airport departure board energy. πŸ›«

#pragma D option quiet
#pragma D option switchrate=10hz

dtrace:::BEGIN
{
        printf(" %3s %15s:%-5s      %15s:%-5s %6s  %s\n", "CPU",
            "LADDR", "LPORT", "RADDR", "RPORT", "BYTES", "FLAGS");
}

tcp:::send
{
        this->length = args[2]->ip_plength - args[4]->tcp_offset;
        printf(" %3d %16s:%-5d -> %16s:%-5d %6d  (", cpu, args[2]->ip_saddr,
            args[4]->tcp_sport, args[2]->ip_daddr, args[4]->tcp_dport,
            this->length);
        printf("%s", args[4]->tcp_flags & TH_FIN ? "FIN|" : "");
        printf("%s", args[4]->tcp_flags & TH_SYN ? "SYN|" : "");
        printf("%s", args[4]->tcp_flags & TH_RST ? "RST|" : "");
        printf("%s", args[4]->tcp_flags & TH_PUSH ? "PUSH|" : "");
        printf("%s", args[4]->tcp_flags & TH_ACK ? "ACK|" : "");
        printf("%s", args[4]->tcp_flags & TH_URG ? "URG|" : "");
        printf("%s", args[4]->tcp_flags == 0 ? "null " : "");
        printf("\b)\n");
}

tcp:::receive
{
        this->length = args[2]->ip_plength - args[4]->tcp_offset;
        printf(" %3d %16s:%-5d <- %16s:%-5d %6d  (", cpu,
            args[2]->ip_daddr, args[4]->tcp_dport, args[2]->ip_saddr,
            args[4]->tcp_sport, this->length);
        printf("%s", args[4]->tcp_flags & TH_FIN ? "FIN|" : "");
        printf("%s", args[4]->tcp_flags & TH_SYN ? "SYN|" : "");
        printf("%s", args[4]->tcp_flags & TH_RST ? "RST|" : "");
        printf("%s", args[4]->tcp_flags & TH_PUSH ? "PUSH|" : "");
        printf("%s", args[4]->tcp_flags & TH_ACK ? "ACK|" : "");
        printf("%s", args[4]->tcp_flags & TH_URG ? "URG|" : "");
        printf("%s", args[4]->tcp_flags == 0 ? "null " : "");
        printf("\b)\n");
}

The -> in the output is a packet going out on send, the <- means it came in on receive β€” it's giving directional realness. πŸŽ€


πŸ¦‹ Example 2: logging TCP connection state changes

This one's for when you want to watch connections mutate between states like a little caterpillar becoming a butterfly. πŸ›βž‘οΈπŸ¦‹ The DELTA(us) column shows microseconds since the last change on that same connection, and the very first change on a connection shows a literal - instead of a number, bc there's nothing to compare to yet, iykyk.

#pragma D option quiet
#pragma D option switchrate=25hz

int last[int];

dtrace:::BEGIN
{
        printf("   %12s %-20s    %-20s %s\n",
            "DELTA(us)", "OLD", "NEW", "TIMESTAMP");
}

tcp:::state-change
{
        this->elapsed = (timestamp - last[args[1]->cs_cid]) / 1000;
        printf("   %12d %-20s -> %-20s %d\n", this->elapsed,
            tcp_state_string[args[5]->tcps_state],
            tcp_state_string[args[3]->tcps_state], timestamp);
        last[args[1]->cs_cid] = timestamp;
}

tcp:::state-change
/last[args[1]->cs_cid] == 0/
{
        printf("   %12s %-20s -> %-20s %d\n", "-",
            tcp_state_string[args[5]->tcps_state],
            tcp_state_string[args[3]->tcps_state], timestamp);
        last[args[1]->cs_cid] = timestamp;
}

Notice the two tcp:::state-change clauses β€” the second one is predicate-guarded on last[args[1]->cs_cid] == 0, so it's the "first time we're seeing this connection" clause. The way that works is iconic, actually. πŸ‘‘


🧚 Example 3: CWND and SSTHRESH via the siftr probe

Wanna peek at congestion window (CWND) and slow-start threshold (SSTHRESH) every time a packet gets sent or received? Yeah you do. The siftr probe is your girl. πŸ’–

#pragma D option quiet
#pragma D option switchrate=10hz

dtrace:::BEGIN
{
        printf(" %3s %16s:%-5s %16s:%-5s %10s %10s\n",
            "DIR", "LADDR", "LPORT", "RADDR", "RPORT", "CWND", "SSTHRESH");
}

tcp:::siftr
{
        printf(" %3s %16s:%-5d %16s:%-5d %10u %10u\n",
            siftr_dir_string[args[0]->direction],
            args[0]->laddr, args[0]->lport, args[0]->raddr, args[0]->rport,
            args[0]->snd_cwnd, args[0]->snd_ssthresh);
}

πŸ’« COMPATIBILITY πŸ’«

This provider is compatible with the tcp provider in Solaris. No cap β€” same vibes across platforms. 🫢


🌈 SEE ALSO 🌈

Go read these next, bestie, they're all part of the friend group:


πŸ“œ HISTORY πŸ“œ

The tcp provider first appeared in FreeBSD 10.0. A whole era. ⭐


πŸ‘‘ AUTHORS πŸ‘‘

This manual page was written by Mark Johnston <markj@FreeBSD.org>. Absolute main character energy for writing all this out by hand. Respect. πŸŽ€


πŸ™ˆ BUGS πŸ™ˆ

Heads up bestie, the tcps_local and tcps_active fields of tcpsinfo_t are not filled in by the translator. So if you're squinting at those and getting nothing β€” it's not you, it's the translator. πŸ’€

That's the whole page, bestie β€” you're officially a dtrace TCP provider knower now. Slay. πŸ’…βœ¨

This page is from Debian, dated July 2, 2023 (DTRACE_TCP(4)) β€” go forth and trace responsibly. πŸŒΈπŸ¦‹

πŸ“¦ where this page comes from