Home » TCP/IP Deep Dive: TCP Handshake, Flow Control และ Congestion Control
TCP/IP Deep Dive: TCP Handshake, Flow Control และ Congestion Control
TCP/IP Deep Dive: TCP Handshake, Flow Control และ Congestion Control
TCP (Transmission Control Protocol) เป็น transport layer protocol ที่ให้ reliable, ordered delivery ของ data TCP ใช้ 3-way handshake สร้าง connection, flow control (sliding window) ป้องกัน receiver overwhelm และ congestion control (slow start, congestion avoidance) ป้องกัน network congestion การเข้าใจ TCP internals สำคัญมากสำหรับ troubleshooting performance
TCP performance problems เป็น สาเหตุอันดับ 1 ของ application slowness ที่ network engineers ต้องแก้ เช่น slow file transfers, web page load times สูง หรือ database timeouts การเข้าใจ TCP window size, retransmissions และ congestion control ช่วยระบุ bottleneck ว่าอยู่ที่ network, server หรือ application
TCP Header
| Field |
Size |
Purpose |
| Source Port |
16 bits |
Sender’s port number |
| Destination Port |
16 bits |
Receiver’s port number |
| Sequence Number |
32 bits |
Byte position ของ first data byte ใน segment |
| Acknowledgment Number |
32 bits |
Next expected byte (cumulative ACK) |
| Window Size |
16 bits |
Receiver’s available buffer (flow control) |
| Flags |
6 bits |
SYN, ACK, FIN, RST, PSH, URG |
| Checksum |
16 bits |
Error detection |
TCP 3-Way Handshake
| Step |
Client |
Server |
Flags |
| 1 |
SYN (seq=x) → |
|
SYN |
| 2 |
|
← SYN-ACK (seq=y, ack=x+1) |
SYN+ACK |
| 3 |
ACK (ack=y+1) → |
|
ACK |
| 4 |
Connection ESTABLISHED — data transfer begins |
– |
TCP Connection Termination (4-Way)
| Step |
Initiator |
Responder |
Flags |
| 1 |
FIN (seq=x) → |
|
FIN+ACK |
| 2 |
|
← ACK (ack=x+1) |
ACK |
| 3 |
|
← FIN (seq=y) |
FIN+ACK |
| 4 |
ACK (ack=y+1) → |
|
ACK |
| 5 |
TIME_WAIT (2×MSL) |
CLOSED |
– |
TCP States
| State |
ความหมาย |
Side |
| LISTEN |
Server waiting for SYN |
Server |
| SYN_SENT |
SYN sent, waiting for SYN-ACK |
Client |
| SYN_RECEIVED |
SYN-ACK sent, waiting for ACK |
Server |
| ESTABLISHED |
Connection active, data transfer |
Both |
| FIN_WAIT_1 |
FIN sent, waiting for ACK |
Initiator |
| FIN_WAIT_2 |
FIN ACKed, waiting for peer’s FIN |
Initiator |
| CLOSE_WAIT |
FIN received, waiting for app to close |
Responder |
| TIME_WAIT |
Waiting 2×MSL before fully closing |
Initiator |
| CLOSED |
Connection fully closed |
Both |
Flow Control (Sliding Window)
| Concept |
รายละเอียด |
| Window Size |
จำนวน bytes ที่ receiver ยอมรับได้โดยไม่ต้องรอ ACK |
| Receive Window (rwnd) |
Receiver advertise buffer space ใน TCP header |
| Window Scaling |
TCP option ที่ขยาย window size > 64KB (shift count, up to 1GB) |
| Zero Window |
Receiver buffer เต็ม → advertise window=0 → sender หยุดส่ง |
| Window Update |
Receiver ประมวลผล data → advertise window > 0 → sender ส่งต่อ |
| Silly Window Syndrome |
Receiver advertise tiny windows → inefficient small segments |
Congestion Control
| Phase |
วิธีทำงาน |
cwnd Growth |
| Slow Start |
เริ่ม cwnd=1 MSS → double ทุก RTT (exponential) |
1 → 2 → 4 → 8 → 16… |
| Congestion Avoidance |
เมื่อ cwnd ≥ ssthresh → เพิ่ม 1 MSS per RTT (linear) |
16 → 17 → 18 → 19… |
| Fast Retransmit |
3 duplicate ACKs → retransmit lost segment ทันที (ไม่รอ timeout) |
– |
| Fast Recovery |
หลัง fast retransmit: ssthresh = cwnd/2, cwnd = ssthresh + 3 |
Halve + 3 (ไม่กลับ slow start) |
| Timeout |
RTO expired → ssthresh = cwnd/2, cwnd = 1 MSS (กลับ slow start) |
Reset to 1 |
Modern Congestion Control Algorithms
| Algorithm |
วิธีทำงาน |
เหมาะกับ |
| Reno |
AIMD (Additive Increase Multiplicative Decrease) |
Traditional (legacy) |
| Cubic |
Cubic function สำหรับ window growth (aggressive recovery) |
Linux default, general purpose |
| BBR (Google) |
Model-based: estimate bandwidth + RTT (not loss-based) |
High bandwidth, long RTT links |
| BBR v2 |
Improved fairness + reduced bufferbloat |
Google services, CDNs |
| DCTCP |
ECN-based (react to congestion marks, not loss) |
Data center (low latency) |
TCP Performance Factors
| Factor |
ผลกระทบ |
วิธีแก้ |
| High RTT |
Slow start ใช้เวลานาน, throughput ต่ำ |
Window scaling, BBR, CDN (reduce RTT) |
| Packet Loss |
Retransmissions, cwnd reduction |
Fix network issues, BBR (loss-tolerant) |
| Small Window |
Sender ส่งได้น้อย (bottleneck) |
Window scaling, tune buffer sizes |
| Bufferbloat |
Excessive queuing → high latency |
AQM (CoDel, fq_codel), ECN |
| BDP (Bandwidth-Delay Product) |
Max throughput = window / RTT |
Window ≥ BDP สำหรับ full utilization |
BDP Calculation
| Parameter |
Value |
| Bandwidth |
100 Mbps = 12.5 MB/s |
| RTT |
50 ms = 0.05 s |
| BDP |
12.5 MB/s × 0.05 s = 625 KB |
| Required Window |
≥ 625 KB สำหรับ full 100 Mbps utilization |
| Default Window (64KB) |
Throughput = 64 KB / 0.05 s = 1.28 MB/s = 10.24 Mbps (only 10%!) |
TCP Tuning
| Parameter |
Linux sysctl |
Recommended |
| TCP Window Scaling |
net.ipv4.tcp_window_scaling |
1 (enabled, default) |
| Receive Buffer Max |
net.core.rmem_max |
16777216 (16MB) |
| Send Buffer Max |
net.core.wmem_max |
16777216 (16MB) |
| TCP Buffer Auto-tuning |
net.ipv4.tcp_rmem / tcp_wmem |
“4096 87380 16777216” |
| Congestion Algorithm |
net.ipv4.tcp_congestion_control |
bbr (or cubic) |
| ECN |
net.ipv4.tcp_ecn |
1 (enable for DC) |
ทิ้งท้าย: TCP = Reliable + Flow + Congestion Control
TCP Deep Dive 3-way handshake: SYN → SYN-ACK → ACK Flow control: receiver window (rwnd) ป้องกัน receiver overwhelm Congestion control: slow start → congestion avoidance → fast retransmit/recovery BDP = bandwidth × RTT → window ≥ BDP สำหรับ full throughput Modern: BBR (model-based), CUBIC (loss-based), DCTCP (ECN-based)
อ่านเพิ่มเติมเกี่ยวกับ Network Traffic Analysis Wireshark และ QoS Classification Marking ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com