TCP-RT is a TCP monitoring tool that uses kernel instrumentation to identify requests and responses in TCP connections and capture related metrics, such as request receipt time and service processing time. TCP-RT is applicable to scenarios where only one concurrent request and response exist on a single connection, such as HTTP/1.1, MySQL, or Redis services. It requires Alibaba Cloud Linux 2 (kernel version 4.19.91-21.al7 or later) or Alibaba Cloud Linux 3. Alibaba Cloud Linux 3 (kernel version 5.10.134-17 or later) provides a new version of TCP-RT that adds support for HTTP/TLS protocols while retaining all features of the original version.
To distinguish between the versions, this document uses tcp_rt.ko to refer to the older version and tcprt for the newer version. The core differences are as follows:
tcp_rt.ko: It is used as a kernel module.
tcprt: It is used with
systemctland a configuration file and provides additional features.
Use tcprt whenever possible.
How it works
Local mode (server perspective)
When a TCP service handles one concurrent request and response, the timeline is shown in the following figure.
The Nth request sent by the client to the server is labeled ReqN and consists of two packets: ReqN-1 and ReqN-2. The server records the arrival time of the first packet as T0 and the arrival time of the second packet as T1. After the server receives the full request, it processes the request and sends two response packets: RspN-1 and RspN-2. The server records the time at which it sends the first response packet as T2. After the client receives the response, it sends an ACK back to the server. The server records the time at which it receives the final ACK as T3.
Based on these timestamps, TCP-RT calculates the following metrics:
upload_time: The time when the user initiated the upload request.
process_time: The server processing time.
This is the interval between the time when the server receives the last request packet and the time when the server starts to send the first response packet.
download_time: The data download time.
This is the interval between the time when the server starts to send the response packets and the time when the server receives the final ACK from the client. This metric is important for large data responses.
Peer mode (client perspective)
When a TCP service handles one concurrent request and response, the timeline is shown in the following figure.
The Nth request (ReqN) sent from the client to the server consists of two data packets: ReqN-1 and ReqN-2. The server records the time it receives the first packet as T0 and the time it receives the second packet as T1. After receiving and processing the request, the server sends two response packets, RspN-1 and RspN-2, to the client. The server records the time it sends the first response packet as T2. After receiving the response packets, the client sends an ACK to the server, and the server records the time it receives the final ACK as T3.
Based on these timestamps, TCP-RT calculates the following metrics:
upload_time: The time when the user makes an upload request.
process_time: The server processing time.
This is the interval between the time when the client sends the last request packet and the time when the client receives the first response packet.
download_time: The data download time.
This is the interval between the time when the client receives the first response packet and the time when the client receives the last response packet.
Output details
TCP-RT collects TCP service metrics in kernel mode and outputs the metrics in the format described in the following table.
File type | Output method | Output timing |
Log file | Output via
|
|
Stats file | Data is aggregated by port (server port or client port) and output to the file | Output occurs periodically. The default interval is one minute. |
Parsing modes
TCP-RT supports multiple parsing modes that you can configure. For more information, see Using TCP-RT.
default: The default mode.
Parses generic TCP streams that follow a request/response pattern, such as HTTP, Redis, or MySQL.
http: The enhanced HTTP parsing mode.
This mode is built based on the default mode and reports round-trip time (RTT) and congestion window metrics. It also detects the HTTP “Expect: 100-continue” process and logs the 100-continue request and its response as an
R.Crecord. For more information, see Output format.Enhanced HTTPS
This mode is built based on the http mode and adds support for Transport Layer Security (TLS) message parsing.
TLS handshake parsing: Detects the TLS 1.2 and TLS 1.3 handshake phases and marks them in the output logs. For more information, see Output format.
For TLS 1.2 (four-way handshake), two
R.Hrecords are generated.For TLS 1.3 (three-way handshake), one
R.Hrecord is generated, including the handshake duration.
“Close Notify” Alert message parsing: The message is logged as an
R.Arecord. For more information, see Output format.
The parsing mode for tcp_rt.ko is not configurable and supports only the default mode.
Output format
Definitions of TASK and TCP lifecycle.
A TASK represents a complete request and response cycle.
A TCP lifecycle contains multiple TASKs.
Log file format
In log files, each column in a record corresponds to a piece of information. The following figure shows a sample log entry.
The columns are described from left to right as follows:
The version number. The value can be V6 or V7.
V6: The default version.
V7: This version appears for R-type records that are generated based on specific configurations with extra fields. This includes the following records:
R logs in http or https parsing modes use V7 because they include trailing fields.
Extended R log formats, such as R.C, R.Z, R.A, R.H, and R.F, use V7. For more information, see the following table.
The record type identifier. The value can be R, E, W, N, or P.
R: This record is generated when a request reaches the local server and a request and response cycle is complete within a TCP service.
E: This record is generated when a connection is closed.
W: This record is generated when a connection is closed during response transmission.
N: This record is generated when a connection is closed during request reception.
P: This record is generated when the local server sends a request to a remote server and a request and response cycle is complete within a TCP service.
The seconds part of the TASK start time.
The microseconds part of the TASK start time.
The remote IP address of the TCP connection.
The remote port of the TCP connection.
The local IP address of the TCP connection.
The local port of the TCP connection.
Additional fields are included based on the record type. For more information, see the following table.
Scenario ID | Field description |
R | This record indicates normal start and end of a TASK. A single TCP connection can have multiple R records.
|
R.C | Note Supported only by tcprt (V7). May appear in http/https parsing modes. Format matches R records. Indicates the TASK was triggered by an “Expect: 100-continue” request (no application data included). |
R.Z | Note Supported only by tcprt (V7). May appear in https parsing mode. Format matches R records. Indicates the TASK belongs to a TLS 1.3 0-RTT handshake. |
R.A | Note Supported only by tcprt (V7). May appear in https parsing mode. Format matches R records. Indicates the TASK contains a |
R.H | Note Supported only by tcprt (V7). May appear in https parsing mode. R records may include trailing fields. Indicates the TASK belongs to a TLS handshake phase. For TLS 1.3 (three-way handshake), the log reports handshake duration in microseconds. Trailing field format: |
R.F | Note Supported only by tcprt (V7). Appears when Indicates the TASK triggers completion time statistics by byte thresholds. Based on byte thresholds defined in the configuration file, the response data stream is divided into steps, and data volume plus completion time for each step are appended to the record (units: bytes and us). Trailing field format: |
P | This record indicates normal start and end of a TASK. A single TCP service can have multiple P records. P records were added in V6 to capture outbound client requests. They appear only if
|
E | This record indicates TCP connection closure. Each TCP connection has exactly one E record. Connections with
|
N | This record appears when a TCP connection closes during request segment reception. A connection may have zero or one N record.
|
W | This record appears when a TCP connection closes during response segment transmission. A connection may have zero or one W record.
|
Extended log types use the
R.xformat, wherexcan be a single character or a combination of characters. For example,R.AFindicates that bothAandFapply.R.AandR.Clogs use the response data volume as a criterion and may have minor false positives.For the metrics in the preceding table that indicate the amount of data sent by a TASK, if the TASK is the last one in a connection, one is subtracted from the
snd_nxtvalue during normal processing to account for the connection closure (FIN). However, if the connection is closed abnormally, for example, if a TASK receives an abnormal termination message (RST) after the TASK is complete and the connection terminates in response to thereset, the actual amount of data sent is one byte greater than the displayed value. Typically, parameter values related to the amount of data sent by a task are correct, and the margin of error is only one byte.
The three time phases are shown in How it works:
R records report the total time (T0 to T3), upload delay (upload_time, T0 to T1), and service delay (process_time, T1 to T2). The download time (download_time) can be calculated using the following formula:
download_time = total_time - upload_time - process_time
Stats file format
The columns are described from left to right as follows:
The timestamp when the record was output.
A reserved field. The value is always
all.The port number.
The average total duration of a TASK that is obtained from R records.
The average service delay of a TASK that is obtained from R records.
The packet loss rate in per mille (‰).
The average RTT in microseconds (us).
The per mille (‰) of TASKs that are closed during request transmission.
The average amount of data sent per TASK.
The average upload delay of a TASK.
The average amount of data received per TASK.
The number of TASKs that are included in the statistics.
tcprt versus tcp_rt.ko version differences
Comparison item | tcprt | tcp_rt.ko |
Log file path | Same | |
Log format | tcprt extends the log format while maintaining backward compatibility. | |
Feature management |
|
|
Feature configuration | Configuration file:
| Module parameters:
|
System requirements | Alibaba Cloud Linux 3 (kernel version |
|
Features |
| Supports only default parsing mode. |
Feature usage
Choose one of the following methods based on your environment:
tcprt
For Alibaba Cloud Linux 3 (kernel version 5.10.134-17 or later).
tcp_rt.ko
For Alibaba Cloud Linux 2 (kernel version 4.19.91-21.al7 or later).
Alibaba Cloud Linux 3.
Because both share the same log file path, you cannot run them at the same time. You can use only one at a time.
tcprt
Installation and configuration.
Install tcprt.
sudo yum install -y tcprtConfigure tcprt.
Open the
/etc/tcprt-bpf/tcprt.yamlfile.sudo vim /etc/tcprt/tcprt.yamlPress
ito enter the edit mode and configure parameters as required.The configuration file has three sections: global, per-port, and init. The sections are described in the following tables.
Global
Parameter
Description
Values
Default
peer
Default tcprt port matching mode.
false: Match local ports.
true: Match remote ports.
true/false
Empty
stats
Enable stats log output.
enable/disable
disable
stats_interval
Stats output interval. Unit: s.
Integer
60
first_frame_bytes
Byte thresholds for completion time statistics. Unit: byte.
List of up to 4 integers
Empty
per port.
This section is a list that is empty by default. Each element defines a port configuration.
Parameter
Description
Values
Default
port
Port number or range.
An integer representing the port number.
Two comma-separated integers: closed interval port range.
None
peer
Matching mode for this port (or range).
true/false
Global peering configuration
mode
Protocol parsing mode. default matches tcp_rt.ko behavior.
default/http/https
default
first_frame
Enable completion time statistics by byte thresholds.
enable/disable
disable
Initialize.
Parameter
Description
Values
Default
log_buf_num
Number of sub-buffers for log files.
Integer
8
log_buf_size
Size of each log sub-buffer. Unit: byte.
Maximum log file size is
log_buf_num * log_buf_size.Integer
262144
stats_buf_num
Number of sub-buffers for stats files.
Integer
8
stats_buf_size
Size of each stats sub-buffer. Unit: byte.
Maximum stats file size is
stats_buf_num * stats_buf_size.Integer
16384
After you edit the file, press the
Esckey, enter:wq, and then press theEnterkey to save the file.Start the service to automatically load the configuration.
sudo systemctl start tcprt(Optional) You can enable automatic startup at boot.
sudo systemctl enable tcprt
Uninstall tcprt.
sudo yum remove tcprtType
yand press the Enter key to confirm the uninstallation.
tcprt configuration example
The peer parameter is set to false to match local ports by default. Two port configurations are added:
Port 443 (exact match): HTTPS parsing is enabled to match the local port. The first_frame feature (completion time by byte thresholds) is enabled with thresholds of 100, 2000, and 20000 bytes.
Port range 56789 to 56800: The default TCP parsing is used to match remote ports.
The stats feature is disabled.
The default buffer sizes are used for logs and stats.
global:
peer: false
stats: disable
stats_interval: 60
first_frame_bytes: [100, 2000, 20000, 0]
per-port:
-
port: 443
mode: https
first_frame: enable
-
port: 56789,56800
mode: default
peer: true
init:
log_buf_num: 8
log_buf_size: 262144
stats_buf_num: 8
stats_buf_size: 16384Byte completion times are tracked using ACK packets. In practice, one ACK packet may acknowledge a large amount of data, such as tens of kilobytes. Therefore, the logged data volumes may exceed the configured byte thresholds.
tcp_rt.ko
Load and configure the module.
You can use one of the following methods.
Configure parameters during module loading.
Load the tcp_rt module and configure parameters.
For example, load the tcp_rt module and configure lports to 80.
sudo modprobe tcp_rt lports=80Verify the configuration.
sudo cat /sys/module/tcp_rt/parameters/lports
Load the module and then configure parameters.
Load the module.
sudo modprobe tcp_rtAfter the module is loaded, configure parameters in the /sys/module/tcp_rt/parameters/ directory.
Example: Monitor local port 80.
sudo sh -c 'echo 80 > /sys/module/tcp_rt/parameters/lports'Verify the configuration.
sudo cat /sys/module/tcp_rt/parameters/lportsThe parameters and commands are described in the following table.
Parameter
Description
Default
Command
stats
Enable stats output:
0: No.
1: Yes.
0
echo 0 > statsstats_interval
Stats output interval. Unit: s.
60
echo 60 > stats_intervallports
Local server ports to monitor (up to 6).
None
echo 80,800,8080 > lportspports
Remote ports of TCP connections.
None
echo 80,800,8080 > pportslports_range
Local server port ranges. Two numbers per range. Example command configures 80–100 and 1000–2000.
None
echo 80,100,1000,2000 >lports_rangepports_range
Remote port ranges. Two numbers per range. Example command configures 80–100 and 1000–2000.
None
echo 80,100,1000,2000 >pports_rangelog_buf_num
Maximum log file size is
log_buf_num * 256 k. Configurable only at module load.8
modprobe tcp_rt log_buf_num=10stats_buf_num
Maximum stats file size is
stats_buf_num * 16 k. Configurable only at module load.8
modprobe tcp_rt stats_buf_num=10
Unload the module.
Run the following command to deactivate the
tcp-rtmodule. This way, no new connections use TCP-RT.sudo echo 1 > /sys/kernel/debug/tcp-rt/deactivateRun the following command to verify that no connections are using the
tcp-rtmodule.lsmodIf the value of
Used byfor thetcp-rtmodule is0, no connections are usingtcp-rt.If no connections are using the
tcp-rtmodule, run the following command to uninstall thetcp-rtmodule.sudo rmmod tcp_rt
FAQ
Log file size differs from configuration
Commands such as ls -l may show file sizes that are larger than the configured size. This is normal. Log files are stored in the /sys virtual filesystem. Therefore, the ls -l command does not reflect the actual disk usage because these files do not consume disk space. The displayed size represents the cumulative log volume and increases over time. To check the actual log size, you can redirect the logs to a regular file and inspect the file.
The following figure shows an example of the default settings (2 MB). If you use the cat command to redirect the log content to a file, the actual size of the file is approximately 2 MB, as expected.
