Why this lab exists
Every time your Java code calls new Socket("api.stripe.com", 443) or Spring’s RestClient fires an HTTP request, the operating system performs a 3-way handshake, assigns sequence numbers, manages a receive window, and runs a congestion control algorithm — before a single byte of your application data is sent. You have never written any of that code. You have probably never thought about it.
This lab makes all of it visible. We implement TCP in userspace — not as a library sitting on top of kernel TCP, but as a real protocol stack that handles raw IP packets. We use a Linux TUN device to intercept packets before they reach the kernel, parse the IP and TCP headers ourselves, and respond with correctly formed segments. By the end, connect(), send(), and recv() are not magic — they are a state machine you have built yourself.
Why do production systems implement userspace networking?
Four distinct reasons motivate this in real codebases:
-
DPDK and high-performance networking. The Data Plane Development Kit moves the NIC driver into userspace entirely, eliminating kernel crossing overhead. Trading systems at Jane Street, Citadel, and Virtu run userspace TCP to achieve sub-microsecond latency that kernel TCP cannot provide due to scheduling and context-switch costs.
-
QUIC and HTTP/3. Google’s QUIC protocol implements its own congestion control and reliability on top of UDP. Every
curl --http3call is running a congestion control algorithm written in userspace (Chromium’s CUBIC, quiche’s BBR). The reason: TCP is implemented in the kernel and cannot be easily modified; UDP is a blank slate that gives application developers full control. -
Network testing tools. Scapy, the Python packet library, lets you craft arbitrary TCP packets — invalid sequence numbers, malformed checksums, forged source IPs — to test how your server handles misbehaving clients. Scapy implements its own IP/TCP stack that bypasses the kernel’s checks.
-
Understanding before abstraction. Every senior networking engineer I have met who truly understands TCP failures — RST storms, TIME_WAIT exhaustion, SYN floods, retransmission spirals — implemented TCP themselves first. The state machine is not complicated; it is just hidden.
This lab exists so that when your production system has a TCP issue, you know exactly which part of the state machine failed.
What we are building
A userspace TCP/IP stack in C, built across four stages using a Linux TUN device.
We build:
open_tun()— opens/dev/net/tun, creates a virtual NIC as a file descriptor- IP header parser — version/IHL/protocol field extraction, IPv4 only
- TCP header parser — flags, sequence numbers, data offset, checksum verification
- 3-way handshake state machine — CLOSED, LISTEN, SYN_RCVD, ESTABLISHED
- Random ISN generation and checksum computation (IP header + TCP pseudo-header)
- Reliable delivery — sequence tracking, ACK generation, receive window advertisement
- Retransmission timer — fire if no ACK within 200ms, simple fixed RTO
- FIN/FIN-ACK close sequence — CLOSE_WAIT, LAST_ACK, TIME_WAIT with 2-second cleanup
- Congestion control — slow start, congestion avoidance (AIMD), fast retransmit (3 dup ACKs)
- Throughput measurement — MB/s printed every second to stderr
We explicitly skip (and why):
- IPv6 — same concepts, different header; add after IPv4 is solid
- CUBIC and BBR — require RTT estimation and time-series math; each is a lab of its own
- TLS — sits above TCP; the TLS Handshake Protocol lab covers this
- QUIC — builds on UDP, not TCP; covered separately
- IP fragmentation reassembly — requires a fragment buffer and timer; uncommon on modern paths with PMTUD
- TCP options (SACK, window scaling, timestamps) — protocol-correct but adds 200+ lines of option parsing
Mental model
The TCP state machine
TCP is a finite state machine. Every connection moves through well-defined states in response to incoming segments and application calls. The states you care about:
+----------+
| CLOSED |
+----------+
|
passive open
|
v
+----------+
| LISTEN | (server waiting for connections)
+----------+
|
SYN received
|
v
+----------+
| SYN_RCVD | (sent SYN-ACK, waiting for ACK)
+----------+
|
ACK received
|
v
+----------+
|ESTABLISHED| (data flows here)
+----------+
/ \
app close FIN received
/ \
v v
+----------+ +----------+
|FIN_WAIT_1| |CLOSE_WAIT|
+----------+ +----------+
| |
FIN received app close
| |
v v
+----------+ +----------+
|FIN_WAIT_2| | LAST_ACK |
+----------+ +----------+
| |
FIN received ACK received
| |
v v
+----------+ +----------+
|TIME_WAIT | | CLOSED |
+----------+ +----------+
|
2MSL timer
|
v
+----------+
| CLOSED |
+----------+
The 3-way handshake lives in the top half: CLOSED → LISTEN → SYN_RCVD → ESTABLISHED. The bottom half handles graceful close. TIME_WAIT is the most commonly misunderstood state: a connection lingers there for 2 minutes after closing to ensure any delayed duplicate packets from the old connection cannot corrupt a new connection on the same 4-tuple.
The sliding window
TCP’s flow control uses a sliding window. Three variables govern the sender’s behavior:
snd_una— the oldest byte sent but not yet acknowledged (left edge of the window)snd_nxt— the next byte to send (right end of in-flight data)rcv_wnd— the receiver’s advertised available buffer space
The sender may have at most rcv_wnd bytes in flight (snd_nxt - snd_una <= rcv_wnd). When the receiver ACKs data, snd_una advances, opening space in the window. When the receiver’s buffer fills, it advertises rcv_wnd = 0 — a zero-window probe keeps the connection alive.
snd_una snd_nxt
| |
sent+ACKed | sent+in-flight| can send (window) | cannot send yet
+---------------+---------------------+
<------ rcv_wnd --------------------->
Congestion control layered on top
Flow control prevents overwhelming the receiver. Congestion control prevents overwhelming the network. They operate simultaneously, and the actual send limit is min(cwnd, rcv_wnd).
cwnd (congestion window) starts at 1 MSS (1460 bytes). In slow start it doubles every RTT (one MSS added per ACK). When it hits ssthresh (slow start threshold), it switches to additive increase: one MSS added per RTT. On loss it drops: ssthresh = cwnd/2, cwnd = ssthresh (Reno fast retransmit) or cwnd = 1 MSS (timeout).
The genius of TCP is that these three variables — snd_una, snd_nxt, cwnd — cooperate without any coordination between sender and receiver beyond the ACK stream.
Prerequisites
- C basics: structs, pointers,
malloc/free,sizeof,memcpy - Linux networking concepts: what an IP address and port number are
- Understanding of file descriptors (
open,read,write,close) - Familiarity with big-endian vs little-endian byte order
- The HTTP server lab (covers TCP concepts from the application side)
You do not need to know socket programming — this lab works one layer below it.
Stage-by-stage build
Stage 0 TUN device setup + IP/TCP header parsing
- Open /dev/net/tun with ioctl TUNSETIFF and IFF_TUN | IFF_NO_PI flags
- Configure tun0 at 10.0.0.1/24 with ip addr and ip link set tun0 up
- Read IP packets in a loop: each read() returns exactly one complete IP packet
- Parse ip_hdr fields: version_ihl, protocol, saddr, daddr, tot_len
- Parse tcp_hdr fields: source, dest, seq, ack_seq, doff_res, flags, window
- Print SYN/ACK/FIN/PSH flags and hexdump first 16 payload bytes
The key insight of v0: a TUN device is a virtual network interface exposed as a file descriptor. The kernel routes IP packets destined for the TUN interface’s address to that fd. Reading from the fd yields one complete IP packet. Writing to the fd injects a packet into the kernel’s receive path.
This is the primitive on which the entire userspace TCP stack is built. No socket(). No bind(). No accept(). Just open(), read(), and write() on a file.
Opening the TUN device:
int open_tun(const char *ifname)
{
int fd = open("/dev/net/tun", O_RDWR);
if (fd < 0) { perror("open /dev/net/tun"); return -1; }
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI; /* layer-3, no packet-info header */
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
if (ioctl(fd, TUNSETIFF, &ifr) < 0) {
perror("ioctl TUNSETIFF"); close(fd); return -1;
}
return fd;
}IFF_TUN means “layer 3” — we see raw IP packets with no Ethernet frame wrapper. IFF_NO_PI strips the 4-byte “packet info” prefix that the kernel would otherwise prepend to each read. Without IFF_NO_PI, the first 4 bytes of every read are metadata, not IP header — a confusing debugging experience.
IPv4 header layout (20 bytes):
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL | DSCP |ECN| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+All multi-byte fields are big-endian (network byte order). ntohs(ip->tot_len) converts to host order. ntohl(ip->saddr) gives the source IP as a 32-bit integer you can compare with 10.0.0.2 = 0x0a000002.
The version_ihl byte packs two fields: (version_ihl >> 4) & 0xf = IP version (4), (version_ihl & 0xf) * 4 = header length in bytes (20 when no options).
TCP flags byte:
#define TCP_FLAG_FIN 0x01
#define TCP_FLAG_SYN 0x02
#define TCP_FLAG_RST 0x04
#define TCP_FLAG_PSH 0x08
#define TCP_FLAG_ACK 0x10A SYN-ACK has flags = 0x12 (SYN | ACK). Test with if (tcp->flags & TCP_FLAG_SYN). The doff_res byte packs the data offset: ((tcp->doff_res >> 4) & 0xf) * 4 = TCP header length in bytes (20 when no options). The payload starts at buf + ihl + data_offset.
v0 key lesson: Everything below TCP is just structured bytes. Once you can parse an IP header and print the flags, you understand the entire physical representation of a TCP segment. All the sophistication of reliable delivery, congestion control, and flow control is built on top of these 40 bytes of header.
Stage 1 3-way handshake state machine (CLOSED to ESTABLISHED)
- Allocate a tcp_conn slot on SYN, generate a random ISN, send SYN-ACK
- On ACK matching our ISN+1: transition state to ESTABLISHED
- Implement send_packet(): build IP header, TCP header, compute both checksums, write to tun_fd
- Implement checksum(): 16-bit ones-complement sum over IP header (20 bytes)
- Implement tcp_checksum(): pseudo-header (12 bytes) + TCP header + payload
- Test: nc 10.0.0.2 8080 -w 2 and verify ESTABLISHED is printed
The 3-way handshake is the most misunderstood part of TCP. Engineers know the acronyms — SYN, SYN-ACK, ACK — but not why each step exists.
Why three steps?
The handshake solves a synchronization problem: both sides must agree on starting sequence numbers before they can exchange data reliably. The sequence number space is 32 bits (0 to 4,294,967,295). Each side picks an ISN (Initial Sequence Number) independently. The handshake exchanges both ISNs:
- SYN: “I want to connect; my ISN is X.”
- SYN-ACK: “OK; my ISN is Y. I acknowledge your ISN (ack = X+1).”
- ACK: “I acknowledge your ISN (ack = Y+1). We are synchronized.”
After step 3, both sides know: “all bytes I send start at my ISN+1” and “all bytes the remote sends start at their ISN+1.” Sequence numbers are then incremented for every byte of data — they track bytes, not packets.
Why random ISNs?
If ISNs were predictable (e.g., always starting at 0, or incrementing by 1 per connection), an off-path attacker could forge TCP segments with valid sequence numbers without ever seeing the traffic. The attacker could inject data, reset connections, or hijack sessions. RFC 1948 (1996) mandated randomized ISNs. The Linux kernel computes ISNs using MD5 of the 4-tuple plus a secret key plus a monotonic timer — cryptographically unpredictable.
We use rand(), which is good enough for a lab but trivially predictable in production.
The checksum:
TCP’s checksum uses a “pseudo-header” — a 12-byte construct that is not transmitted but is included in the checksum calculation. It covers source IP, destination IP, protocol (6), and TCP segment length. This ties the checksum to the specific IP addresses, so a packet cannot be silently misdelivered to the wrong host and still pass the TCP checksum.
uint16_t checksum(const void *data, int len)
{
const uint16_t *ptr = (const uint16_t *)data;
uint32_t sum = 0;
while (len > 1) { sum += *ptr++; len -= 2; }
if (len == 1) sum += *(const uint8_t *)ptr; /* odd byte: zero-pad */
while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16); /* fold carry */
return (uint16_t)(~sum); /* ones complement */
}The IP header checksum covers only the IP header (20 bytes). The TCP checksum covers the pseudo-header (12 bytes) + TCP header (20 bytes) + payload. If you send a packet with an incorrect checksum, the OS will silently drop it — no error, no log, just silence. This makes checksum bugs extremely hard to debug.
send_packet() structure:
void send_packet(int tun_fd, struct tcp_conn *conn, uint8_t flags,
const uint8_t *payload, int payload_len)
{
/* Allocate: IP header + TCP header + payload */
uint8_t *pkt = malloc(IP_HDR_LEN + TCP_HDR_LEN + payload_len);
/* Fill IP header fields in network byte order */
struct ip_hdr *ip = (struct ip_hdr *)pkt;
ip->version_ihl = 0x45; /* version=4, IHL=5 (20 bytes) */
ip->tot_len = htons(total);
ip->saddr = htonl(conn->src_ip);
ip->daddr = htonl(conn->dst_ip);
ip->check = checksum(ip, IP_HDR_LEN); /* computed after all fields set */
/* Fill TCP header, compute pseudo-header checksum */
struct tcp_hdr *tcp = (struct tcp_hdr *)(pkt + IP_HDR_LEN);
tcp->seq = htonl(conn->snd_nxt);
tcp->ack_seq = htonl(conn->rcv_nxt);
tcp->flags = flags;
tcp->check = tcp_checksum(ip, tcp, payload, payload_len);
write(tun_fd, pkt, total); /* inject into kernel's receive path */
free(pkt);
}v1 key lesson: The handshake is a state machine, not magic. The transition from SYN_RCVD to ESTABLISHED happens when we receive an ACK with ack_seq == snd_nxt (our ISN + 1). If that ACK never arrives — because our SYN-ACK had a wrong checksum, or the route was missing — the connection hangs in SYN_RCVD forever (or until the SYN-ACK retransmission timeout fires, which we add in v2).
Stage 2 Reliable delivery: seq/ack, receive window, retransmission, FIN
- On data received: advance rcv_nxt by data_len, send ACK, echo data back
- On ACK received: advance snd_una to ack_seq, clear send buffer
- Retransmit timer: poll every 50ms, fire send_packet if now - last_send > 200ms
- FIN: send ACK + our FIN, transition ESTABLISHED to CLOSE_WAIT to LAST_ACK
- TIME_WAIT: free conn slot after 2000ms via check_time_wait()
- Use O_NONBLOCK on tun_fd so the timer loop does not block on read()
Reliable delivery is the hardest part of TCP. Three mechanisms cooperate: sequence numbers track progress, ACKs confirm delivery, retransmission recovers lost segments.
Sequence numbers track bytes:
If the remote sends a segment with seq=1000 containing 500 bytes, we set rcv_nxt = 1000 + 500 = 1500. The next ACK we send has ack_seq = 1500 — telling the remote “I have received all bytes up through 1499, send from 1500 next.” If the remote then sends seq=1500 with another 300 bytes, rcv_nxt = 1800. Sequence numbers increase monotonically, wrapping around the 32-bit space.
This “byte numbering” rather than “segment numbering” is what makes TCP reliable even when segments are split or merged by the network. A 1500-byte segment can be fragmented into three 500-byte IP packets; TCP reassembles them using sequence numbers.
The receive window:
Every ACK we send advertises our available buffer space:
tcp->window = htons(RECV_WINDOW); /* 4096 bytes in our toy stack */The sender uses this to limit in-flight data: snd_nxt - snd_una <= rcv_wnd. If the receiver is slow and its buffer fills, it sends window = 0. The sender stops transmitting. When the buffer drains, the receiver sends a “window update” ACK with a non-zero window. Production stacks use 64KB (or more with window scaling); our 4KB is deliberately small to make window behavior visible in experiments.
Retransmission timer:
We make the tun_fd non-blocking (O_NONBLOCK) so our main loop can poll:
for (;;) {
long now_ms = /* clock_gettime */;
if (now_ms - last_timer_check >= 50) {
check_retransmits(tun_fd); /* fire if any conn exceeds 200ms */
check_time_wait(); /* free TIME_WAIT slots after 2000ms */
last_timer_check = now_ms;
}
ssize_t n = read(tun_fd, buf, sizeof(buf));
if (n < 0 && errno == EAGAIN) { usleep(1000); continue; }
/* ... process packet ... */
}RFC 6298 describes computing RTO from smoothed RTT (SRTT) and variance. Linux uses a min RTO of 200ms. We hardcode RETRANSMIT_MS = 200 to keep the code readable. On retransmit, we resend the bytes from send_buf[] that have not yet been acknowledged (snd_una < snd_nxt).
FIN and TIME_WAIT:
The close sequence has two important properties:
-
FIN consumes a sequence number. After sending FIN,
snd_nxtincrements by 1. The remote must ACK this sequence number to confirm the FIN was received. -
TIME_WAIT prevents phantom connections. If you close a connection and immediately open a new one on the same 4-tuple (same src/dst IP and port pair), a delayed duplicate packet from the old connection could arrive and be misinterpreted by the new connection. TIME_WAIT ensures the old sequence number space expires before reuse. Our 2-second timer is shorter than the RFC’s 4-minute 2MSL; production systems suffer from TIME_WAIT exhaustion when connection churn is high.
v2 key lesson: Reliable delivery is not one mechanism — it is the coordination of three: sequence tracking (where are we?), ACKs (what was received?), and timers (what was lost?). Remove any one of them and you get a different protocol: UDP lacks all three, QUIC implements all three in userspace, TCP implements all three in the kernel.
Stage 3 Slow start + congestion avoidance + fast retransmit (TCP Reno)
- Add cwnd and ssthresh to tcp_conn; initialize cwnd=MSS, ssthresh=65535
- on_ack(): if cwnd < ssthresh add MSS (slow start); else add MSS*MSS/cwnd (CA)
- on_duplicate_ack(): increment dup_ack_count; at 3 do fast retransmit
- Fast retransmit: ssthresh=cwnd/2, cwnd=ssthresh, retransmit missing segment
- on_timeout(): ssthresh=cwnd/2, cwnd=MSS, retransmit (back to slow start)
- Print throughput in MB/s every second; cap echoed data at min(copy, cwnd)
Congestion control is TCP’s self-regulating mechanism. Without it, every sender would transmit at maximum rate simultaneously and the network would collapse — a phenomenon called “congestion collapse” that actually happened on the early internet in 1986, dropping throughput by a factor of 1,000. Van Jacobson’s 1988 paper “Congestion Avoidance and Control” described slow start, congestion avoidance, and fast retransmit — the algorithms TCP still uses today (as “TCP Reno”).
Slow start:
The network’s capacity is unknown at connection start. Rather than probing aggressively, TCP starts with cwnd = 1 MSS and doubles each RTT:
- RTT 1: send 1 MSS, receive 1 ACK →
cwnd += MSS = 2 MSS - RTT 2: send 2 MSS, receive 2 ACKs →
cwnd += 2*MSS = 4 MSS - RTT 3: send 4 MSS →
cwnd = 8 MSS
This is exponential growth — which is why “slow start” is a misnomer. It starts slow but ramps quickly. Growth stops when cwnd >= ssthresh.
Congestion avoidance (AIMD):
Above ssthresh, TCP shifts to additive increase: one MSS added per RTT (not per ACK):
conn->cwnd += (MSS * MSS) / conn->cwnd; /* per ACK */Over one RTT of N ACKs this adds approximately MSS total — linear growth. “Additive increase, multiplicative decrease” (AIMD) is the formal name. The additive increase probes for available bandwidth; the multiplicative decrease on loss backs off sharply.
Fast retransmit:
When the receiver gets an out-of-order segment, it immediately ACKs the last in-order byte — a “duplicate ACK.” Three consecutive duplicate ACKs are strong evidence that a segment was lost (rather than merely reordered). TCP Reno triggers fast retransmit immediately:
static void on_duplicate_ack(int tun_fd, struct tcp_conn *conn)
{
conn->dup_ack_count++;
if (conn->dup_ack_count < 3) return;
/* Multiplicative decrease */
conn->ssthresh = conn->cwnd / 2;
if (conn->ssthresh < 2 * MSS) conn->ssthresh = 2 * MSS;
conn->cwnd = conn->ssthresh; /* Reno: enter CA, not slow start */
conn->dup_ack_count = 0;
/* Retransmit missing segment immediately */
send_packet(tun_fd, conn, TCP_FLAG_ACK | TCP_FLAG_PSH,
conn->send_buf, conn->send_len);
}The key difference from timeout: fast retransmit sets cwnd = ssthresh (not 1 MSS), skipping slow start and entering congestion avoidance directly. This is “fast recovery” — the connection recovers faster because the 3 dup ACKs prove the network can still carry data.
Measuring throughput:
static void throughput_tick(int bytes)
{
bytes_sent_window += bytes;
long elapsed_ms = ms_elapsed(&window_start);
if (elapsed_ms >= 1000) {
double mb_per_sec = (double)bytes_sent_window / elapsed_ms / 1000.0;
fprintf(stderr, "[v3] throughput: %.1f MB/s\n", mb_per_sec);
bytes_sent_window = 0;
clock_gettime(CLOCK_MONOTONIC, &window_start);
}
}We measured 390 MB/s with congestion control enabled vs 420 MB/s for v2’s unconstrained echo. The 7% gap comes from cwnd capping the send window when the algorithm conservatively reduces it after detecting congestion.
v3 key lesson: The number “3 duplicate ACKs” is not arbitrary. One duplicate ACK could be a reordering artifact. Two is suspicious. Three almost certainly means loss. The threshold was chosen empirically — at the time of RFC 5681’s writing, network reordering almost never produced more than 2 duplicate ACKs, so 3 was a reliable signal. Modern networks with ECMP routing can produce more reordering; TCP SACK (Selective Acknowledgement) was designed partly to handle this.
Run it yourself
labs/tcp-stack ↗
Linux (TUN/TAP is Linux-only)gccroot / sudo for /dev/net/tunmake - 1 Clone and build all four stages
git clone https://github.com/pushkarkumar-dev/system-design-labs cd system-design-labs/labs/tcp-stack make all - 2 Run v0: TUN device + packet parser (watch raw IP/TCP headers)
sudo ./v0_ipFrom another terminal: ping 10.0.0.2 or send traffic to 10.0.0.2:8080
- 3 Run v1: 3-way handshake (in one terminal, nc in another)
sudo ./v1_handshakeFrom another terminal: nc 10.0.0.2 8080 -w 2
- 4 Run v2: reliable echo server
sudo ./v2_reliableFrom another terminal: echo hello | nc 10.0.0.2 8080 -w 2
- 5 Run v3: echo server with congestion control (throughput printed to stderr)
sudo ./v3_congestion 2>&1 | tee /tmp/cc-log.txt - 6 Run the integration test (Linux only)
cd tests && sudo bash test_handshake.sh - 7 Run the Java integration (separate terminal, v2 echo must be running)
cd java-integration && mvn spring-boot:run curl http://localhost:8081/demo curl http://localhost:8081/compare
Benchmarks
Linux 6.5, Intel i7-12700K | Metric | Result | Notes |
|---|---|---|
| 3-way handshake latency | 1.2 ms | vs kernel TCP 0.08ms — 15x gap from two extra TUN kernel crossings per handshake |
| Throughput v2 echo | 420 MB/s | vs kernel TCP 9.4 GB/s — every packet crosses kernel boundary twice via TUN |
| Throughput v3 with congestion control | 390 MB/s | cwnd-based rate control reduces throughput 7% vs unconstrained v2 |
| Connections per second | 8200 conn/s | linear scan of MAX_CONNS=64 slots; hash table would give 2-3x improvement |
Stress tests
At 65 simultaneous connections (MAX_CONNS + 1): new_conn() scans all 64 slots, finds none free, prints “connection table full,” and returns NULL. The incoming SYN is silently dropped — no RST, no error to the client. The client’s SYN retransmit will fire after its own RTO (typically 1-3 seconds), then fail again. Production kernels handle this with the listen backlog queue and TCP SYN cookies.
At 10K connections per second: Even before hitting MAX_CONNS, the linear O(n) scan in find_conn() degrades. At 64 connections each lookup scans 64 entries — fine. At 10K connections per second with a higher MAX_CONNS, the lookup cost becomes significant. A hash table keyed on the 4-tuple (src_ip, dst_ip, src_port, dst_port) reduces lookup to O(1).
At 100K connections lifetime (TIME_WAIT exhaustion): If the server sees high-churn short-lived connections, TIME_WAIT entries accumulate. With MAX_CONNS=64 and a 2-second TIME_WAIT, we can process at most 32 connections per second sustainably (64 slots / 2 seconds). Anything faster exhausts the table. Real kernels use SO_REUSEADDR and TCP TIME_WAIT reuse to handle this at scale.
At 1MB/s packet loss rate: The retransmit timer fires every 200ms for every unacknowledged segment. Each retransmit re-copies the send buffer and writes to the TUN fd. At high packet loss rates, retransmits can cascade — each retransmit’s acknowledgement (or lack thereof) triggers more retransmits. Production stacks use exponential backoff (doubling RTO on each consecutive timeout) to prevent this cascade from saturating the link.
What the toy misses
- No zero-copy path The kernel uses sendfile() and DMA scatter-gather to move data from disk to NIC without copying through the CPU. Every TUN read() copies a packet from kernel space to our process buffer, and every write() copies it back. At 420 MB/s this is measurable; at 9.4 GB/s kernel TCP speeds it would be a hard ceiling.
- No TIME_WAIT reuse Real stacks hold connections in TIME_WAIT for 2MSL (up to 4 minutes defined by RFC 793; Linux defaults to 60 seconds) to handle delayed duplicates. Our MAX_CONNS=64 table exhausts after 32 connections per second at our 2-second TIME_WAIT. Linux's SO_REUSEADDR and tcp_tw_reuse sysctl allow safe TIME_WAIT reuse when the new connection's ISN is provably newer.
- No CUBIC or BBR congestion control CUBIC (Linux default since 2.6.19) uses a cubic function of time since last congestion event to probe more aggressively on high-BDP paths. BBR (Bottleneck Bandwidth and RTT, Google, 2016) models the bottleneck bandwidth and RTT independently, achieving near-line-rate throughput on lossy links where CUBIC backs off unnecessarily. Both are significantly more complex than our Reno implementation.
- No IP fragmentation reassembly If a packet larger than the path MTU is sent without the DF (Don't Fragment) bit, intermediate routers fragment it. Each fragment arrives as a separate IP packet with the same ID and non-zero fragment offset. Our stack processes each read() as a complete IP packet and drops fragments. Real stacks maintain a reassembly buffer with a timer to handle out-of-order fragments.
- No TCP options SACK (Selective ACK, RFC 2018) lets the receiver tell the sender exactly which bytes were received, enabling hole-filling retransmission. Window scaling (RFC 7323) extends the window beyond 64KB for high-BDP links. Timestamps enable RTT measurement and PAWS (Protect Against Wrapped Sequence numbers). TCP Fast Open sends data in the SYN to eliminate one RTT on repeat connections. Each option adds parsing complexity and state.
- No security SYN cookies (RFC 4987) let the server encode connection state in the ISN so it can handle SYN floods without allocating a conn slot per SYN. RFC 5961 added sequence number window checks to prevent blind reset injection. TLS sits above TCP and is a separate protocol requiring certificate validation, key exchange, and record-layer encryption — none of which exists in our stack.
Wiring it into a Java/Spring service
Wiring it into a Java/Spring service
org.springframework.boot:spring-boot-starter-web:3.3.0 labs/tcp-stack/java-integration Clone and run: cd labs/tcp-stack/java-integration && mvn spring-boot:run TcpClient: Java NIO connects to our userspace stack
The Java integration uses java.nio.channels.SocketChannel — Java’s non-blocking I/O API — to connect to our C echo server. NIO is chosen deliberately over java.net.Socket because it exposes the connection lifecycle more explicitly: connect(), finishConnect(), read(), write() map closely to the underlying SYN/ACK/data/FIN sequence.
public RoundTripResult roundTrip(String message) throws Exception {
long connectStart = System.nanoTime();
try (SocketChannel channel = SocketChannel.open()) {
channel.configureBlocking(true);
// connect() sends SYN and blocks until the 3-way handshake completes.
// With our TUN stack: ~1.2ms (two kernel crossings for SYN-ACK exchange).
// With kernel loopback TCP: ~0.08ms (stays in kernel, no user-space copies).
channel.connect(new InetSocketAddress(host, port));
long connectDone = System.nanoTime();
// write() copies bytes to the kernel send buffer.
// The kernel segments at MSS=1460, wraps in TCP/IP headers.
// Our TUN stack receives via read(tun_fd), parses header, echoes.
ByteBuffer sendBuf = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
while (sendBuf.hasRemaining()) {
channel.write(sendBuf);
}
ByteBuffer recvBuf = ByteBuffer.allocate(4096);
int bytesRead = channel.read(recvBuf);
// Total round-trip: connect + write + read = handshake + data latency
return new RoundTripResult(
message,
new String(recvBuf.array(), 0, bytesRead, StandardCharsets.UTF_8).trim(),
Duration.ofNanos(connectDone - connectStart),
Duration.ofNanos(System.nanoTime() - connectDone),
Duration.ofNanos(System.nanoTime() - connectStart)
);
}
}TcpStackComparison: the kernel vs userspace path
The comparison component exposes GET /compare — a structured JSON explanation of what happens at each layer for each Java NIO call. This is the pedagogical core of the integration:
GET /compare
{
"operation_comparison": [
{
"operation": "SocketChannel.connect(host, port)",
"kernel_tcp": "Kernel sends SYN, waits for SYN-ACK, sends ACK. All three
packets stay inside kernel. Loopback: ~0.08ms.",
"our_tun_stack": "SYN arrives via read(tun_fd) — one kernel-user copy.
We send SYN-ACK via write(tun_fd) — one user-kernel copy.
ACK arrives via read(tun_fd) again. Total: 2 extra crossings = ~1.2ms."
},
...
]
}Why is our handshake 15x slower?
The 15x latency gap (1.2ms vs 0.08ms) comes entirely from two extra kernel-user boundary crossings:
- SYN packet arrives from the remote → kernel routes to
read(tun_fd)→ our process reads it. This is oneread()syscall plus a memory copy of the entire packet. - We call
write(tun_fd, synack_pkt, total)to send SYN-ACK → kernel copies from our buffer back into the network path. Anotherwrite()syscall plus memory copy.
The kernel’s own TCP stack avoids both crossings — it processes the SYN and sends the SYN-ACK entirely within kernel mode without crossing the user-kernel boundary.
DPDK solves this by moving the NIC driver into userspace. Instead of read(tun_fd), DPDK gives you a pointer directly into DMA-mapped memory where the NIC wrote the packet. Zero copies, no syscalls. Production userspace stacks using DPDK achieve handshake latencies of 10-20 microseconds — closer to the kernel than to our 1.2ms.
Run it
# Terminal 1: start the echo server
cd labs/tcp-stack
make v2
sudo ./v2_reliable
# Terminal 2: run the Spring Boot integration
cd labs/tcp-stack/java-integration
mvn spring-boot:run
# Terminal 3: test
curl http://localhost:8081/demo
curl http://localhost:8081/compare | python3 -m json.toolExpected demo output:
{
"result": "sent=hello from Java NIO echoed=hello from Java NIO connect=1ms data=0ms total=1ms",
"connect_latency_ms": 1,
"data_latency_ms": 0,
"total_latency_ms": 1,
"note": "connect_latency is the 3-way handshake cost through our TUN stack..."
} What surprised me
Where to go deeper
RFC 793 (1981) — the original TCP specification. Still readable. The state machine diagram on page 23 is what we implemented. The prose around TIME_WAIT is the clearest explanation you will find.
RFC 5681 — “TCP Congestion Control” (2009). Defines slow start, congestion avoidance, fast retransmit, and fast recovery precisely. Our v3 implements sections 3.1 and 3.2 of this document.
“Congestion Avoidance and Control” — Van Jacobson, SIGCOMM 1988. The paper that saved the internet. Describes the 1986 congestion collapse event, diagnoses the cause (no sender-side rate limiting), and introduces slow start and congestion avoidance. Freely available as a PDF.
saminiir/level-ip — a full userspace TCP implementation in C on GitHub. Implements SACK, window scaling, and IP fragmentation reassembly — everything we skip. Well-commented and well-structured. The natural next step after completing this lab.
Linux kernel net/ipv4/tcp_input.c — the receive-side TCP implementation in the Linux kernel. Warning: ~6,000 lines. But once you have built your own state machine, you will recognize the same patterns: tcp_rcv_synsent_state_process, tcp_data_queue, tcp_ack. The kernel version is this lab plus 25 years of edge cases.
“Computer Networks: A Systems Approach” (Peterson & Davie) — Chapter 5 covers TCP in depth, including the mathematical treatment of congestion control and why TCP is provably stable (Lyapunov functions). Free online at https://book.systemsapproach.org/.
Related labs and questions
http-server — HTTP is built directly on top of TCP. Everything in this lab is what happens before your first @GetMapping handler fires. The HTTP server lab builds upward from TCP sockets; this lab builds downward to the TCP implementation itself.
dns-resolver — DNS is usually UDP, but falls back to TCP for responses over 512 bytes. Understanding why TCP fallback is expensive (a full handshake per large response) requires knowing what the handshake costs — which is now concrete.
websocket-gateway — WebSockets upgrade an existing HTTP connection and then use TCP framing directly. The WebSocket ping/pong mechanism is an application-layer keepalive that mirrors TCP’s keepalive probe mechanism. Comparing the two clarifies what belongs in the transport layer vs the application layer.
rpc-framework — gRPC runs over HTTP/2, which runs over TLS, which runs over TCP. The entire stack is visible now: our C state machine at the bottom, TLS above it, HTTP/2 multiplexing above that, Protocol Buffers at the top. Building the TCP layer makes the gRPC timeout behavior and connection pooling decisions concrete.
Questions to investigate:
- Why does a Linux server in TIME_WAIT refuse a new connection from the same client port? What sysctl changes this behavior?
- What happens to in-flight data when a RST segment arrives? Does the sender know its data was lost?
- Why did Google choose UDP (not TCP) as the base for QUIC? What specific TCP properties made QUIC necessary?
- How does TCP handle a segment with seq=100 when rcv_nxt=200? What should the receiver send back?