TCP uses sequence numbers, acknowledgments and retransmission to provide an ordered byte stream. Congestion control separately adjusts sending behavior. For diagnosis, inspect cwnd, rwnd, RTT, retransmissions and application reads before deciding whether to tune a setting.
This article explains sequence handling, CUBIC window curves and BBR’s bandwidth/delay model, then checks a deterministic slow-start example. The original net/ipv4/tcp_input.c shorthand and perf commands remain, with their version and measurement scope stated below.
1. Sequence Space and Kernel State Machines
The 3-way handshake (SYN, SYN-ACK, ACK) adds connection-setup latency, which can matter for short transfers and high-RTT paths. net.ipv4.tcp_syncookies provides a backlog-overflow defense; it does not replace capacity planning or complete denial-of-service protection.
Linux manages TCP state using structures including struct tcp_sock. An ACK can enter tcp_ack() for validation, SACK and congestion-control work. The unversioned shorthand below omits paths and does not match the full v6.12 implementation; it is not directly compilable source for that version.
/* Excerpt from linux/net/ipv4/tcp_input.c */
static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
{
struct tcp_sock *tp = tcp_sk(sk);
u32 prior_snd_una = tp->snd_una;
u32 ack_seq = TCP_SKB_CB(skb)->ack_seq;
/* If the ACK is out of bounds, drop it */
if (before(ack_seq, prior_snd_una))
goto old_ack;
/* Process SACK blocks to detect out-of-order delivery */
if (tcp_is_sack(tp) && tcp_check_sack_reneging(sk, flag))
tcp_retransmit_timer(sk);
/* Update the congestion control state machine */
tcp_cong_control(sk, ack_seq, prior_snd_una, flag);
return 1;
}
A receiver can buffer out-of-order data and return duplicate ACKs/SACK. Classic RFC 5681 fast retransmit uses three duplicate ACKs with its stated conditions; modern SACK/RACK recovery also uses other signals. The diagram below draws only two duplicate ACKs and omits the third, so those two alone do not illustrate the claimed three-ACK trigger.
sequenceDiagram
participant Client (tcp_output)
participant Server (tcp_input)
Note over Client,Server: RTT = 20ms, MSS = 1460
Client->>Server: DATA, seq=1001, len=1460
Server->>Client: ACK, ack=2461
Client-xServer: DATA, seq=2461, len=1460 (Loss!)
Client->>Server: DATA, seq=3921, len=1460
Server->>Client: DUP ACK, ack=2461 (SACK 3921-5381)
Client->>Server: DATA, seq=5381, len=1460
Server->>Client: DUP ACK, ack=2461 (SACK 3921-6841)
Note over Client: Fast Retransmit Triggered (3 DUP ACKs)
Client->>Server: DATA, seq=2461, len=1460 (Retransmission)
Server->>Client: ACK, ack=6841 (Cumulative ACK advances!)
2. The Mathematics of Congestion Control
When packet loss occurs, TCP reacts by modulating its sending window. Standard algorithms like NewReno use an Additive Increase, Multiplicative Decrease (AIMD) mathematical model.
The CUBIC Algorithm
CUBIC is widely used; the actual default depends on the kernel build and system configuration. It computes a target window \( W \) from time \( t \) since a congestion event. Actual sending still depends on ACK timing, RTT, the fairness branch, receive windows and application supply.
$$ W_{cubic}(t) = C(t-K)^3 + W_{max} $$
Here \( K = \sqrt[3]{ \frac{W_{max}(1-\beta)}{C} } \), and \( \beta \) is the retained window fraction, recommended as 0.7 in RFC 9438. At the modeled congestion event the window falls to about 0.7Wmax. The expression \( W = W \times \beta \) describes a window adjustment, not an immediate fixed 30% reduction in achieved throughput for every lost packet.
The BBR Algorithm (Bottleneck Bandwidth and RTT)
BBR estimates BtlBw and RTprop from observed delivery rates and minimum RTT, using BDP as a reference scale for in-flight data. This has the dimensional relation of Little’s Law, \( L = \lambda W \), but the estimates are not exact physical-link parameters or a universal optimum.
$$ BDP = BtlBw \times RTprop $$
In Linux v6.12 BBR (v1), PROBE_BW cycles through gains including \( 1.25 \), \( 0.75 \) and unity to probe capacity and drain queues. Progression depends on time, in-flight data and loss conditions, rather than an immediate switch whenever RTT rises. BBR still needs retransmission and recovery; it is not immune to random wireless loss. Other versions require their own gain and loss-handling checks.

3. Kernel Instrumentation: `perf` and Flame Graphs
Use perf and flame graphs with an event suited to the question. The retransmission tracepoint stack graph below counts event call stacks; it is not a CPU-time profile of the whole TCP stack. CPU sampling, counters and scheduler/lock evidence answer different questions.
# Profile TCP retransmissions across the entire kernel
sudo perf record -e tcp:tcp_retransmit_skb -aR -g
sudo perf script | stackcollapse-perf.pl | flamegraph.pl > tcp_retrans_flame.svg
# Monitor active connections using ss (socket statistics)
# Extract precise BBR bandwidth estimates and RTT
ss -nti | grep -A 1 'ESTAB'
# Example Output:
# cwnd:10 pacing_rate 1200Mbps delivery_rate 950Mbps bbr:(bw:950Mbps,mrtt:15.2ms,pacing_gain:1.25)
Align tcp:tcp_retransmit_skb with request latency, RTT, queue and GC timelines to examine correlation. Retransmission events alone do not distinguish loss, reordering, timeout or application stalls, and do not establish a GC cause.
4. Bufferbloat and queue configuration
Bufferbloat and the FQ-CoDel Intervention
The original narrative used a 40Gbps streaming scenario with latency rising from 30ms to 2500ms, but the available material contains no matching run logs, topology or control. These remain unverified original assertions.
ss -ntiprovides connection-level window and RTT clues; queue attribution also needs loaded/unloaded delay, bottleneck location and queue statistics.Bufferbloat is extra delay from sustained queue occupancy. Window growth, bottleneck bandwidth, queue capacity and scheduling affect it. A large cwnd alone does not identify an ISP router’s memory configuration or establish CUBIC as the sole cause.
fq_codelcombines flow isolation and CoDel at a controlled queue, dropping or ECN-marking packets under its conditions. It cannot directly remove queues at an uncontrolled upstream bottleneck. The original below-50ms outcome lacks a matching test record. The two sysctls below are configuration examples: default_qdisc does not replace every existing qdisc, and changing the default congestion algorithm does not retroactively switch all established connections. Check support and effective interface/connection state.
sysctl -w net.core.default_qdisc=fq_codel
sysctl -w net.ipv4.tcp_congestion_control=bbr
5. Automated Event Simulation
The companion Python script doubles cwnd in fixed rounds and resets it to 1 after one deterministic loss: a toy slow-start model. The C11 program on 127.0.0.1 only echoes data; it does not export kernel congestion-control state or implement BBR.
python src/tcp_reliability_cwnd.py
cc -std=c11 -Wall -Wextra -O2 src/tcp_loopback_echo.c -o /tmp/tcp_echo
/tmp/tcp_echo
tcp-cwnd-results.csv records six fixed rounds with cwnd 1,2,4,1,2,4 and a loss-retransmit marker in round 3. The original figure and animation captions are retained; this CSV and companion plot do not compare CUBIC/BBR, estimate BDP or trace kernel Fast Recovery.
6. Animated Walkthrough
7. Engineering Heuristics & Anti-Patterns
- Hardware Offloading: Choose TSO/LRO settings for the driver, forwarding/encapsulation path and measured behavior. Transmit segmentation and receive aggregation are different operations. The original up-to-40% CPU figure has no matching conditions here and is not a universal gain.
- 0-Window:
TCP Zero Windowadvertises zero receive-window space. Check application reads, buffering and memory pressure. It does not establish that the application is deadlocked or exclude simultaneous network congestion. - Socket-buffer limits: Before changing
tcp_rmemandtcp_wmem, inspect BDP, autotuning, connection count and memory budget. A limit is not immediate allocation per connection or a throughput guarantee. A transatlantic 100Gbps path is not the sole condition that can justify tuning.
8. TCP Troubleshooting Evidence Matrix
Reliability, receive windows, congestion control, queues, and application read speed all affect TCP behavior. The matrix below maps symptoms to evidence for distinguishing network congestion, application stalls, bufferbloat, and unsuitable congestion-control settings.
| Symptom | Key evidence | Inspect first | Do not mistake it for |
|---|---|---|---|
| Sawtooth throughput drops | cwnd, retransmits, and congestion algorithm from ss -nti. |
CUBIC multiplicative decrease, random loss, or queue drops. | Application-level rate limiting. |
| P99 latency suddenly spikes | RTT distribution, queue delay, and tcp_retransmit_skb events. |
Bufferbloat, upstream queuing, or wireless jitter. | Database slow queries. |
| Frequent Zero Window packets | Receive window, application recv() rate, CPU, and GC state. |
The receiving application is not draining its socket buffer. | Middlebox congestion. |
| BBR behaves inconsistently | Pacing rate, delivery rate, min RTT, and qdisc configuration. | Missing pacing, wrong queue discipline, or polluted bandwidth estimate. | BBR being universally ineffective. |
FAQ
How do BBR and CUBIC use loss signals?
Wireless paths can lose packets without congestion. CUBIC responds according to its loss-control rules; 0.7 in the model is the retained window fraction, not fixed throughput halving. BBR combines delivery-rate, RTT and version-specific loss/recovery behavior. Unchanged RTT alone does not identify every radio loss or guarantee full throughput.
Does the 3-Way Handshake guarantee delivery?
The handshake establishes connection state, exchanges each side’s initial sequence number and negotiates supported options; it is not a cryptographic random negotiation of those options. Subsequent acknowledgments, retransmission and windows support ordered delivery. Persistent failures or resource limits can still cause timeout or connection errors.
References
- Linux Kernel IP Sysctl Documentation
- BBR: Congestion-Based Congestion Control
- RFC 8229: TCP Encapsulation
TCP provides a reliable byte-stream service, with errors for persistent failures. The next article examines HTTP/2, HTTP/3 and CDN caching. HTTP/3 uses QUIC rather than TCP.
Related: loopback cannot drop packets, but it retransmitted 62 times — a real investigation that used retransmit counters and the TCP state distribution as its entry point.