Encapsulation
When an application sends data, each layer on the way down wraps it in its own header (and sometimes a trailer). This wrapping is called encapsulation. The receiver does the reverse, decapsulation: each layer strips its header, checks it, and hands the rest up. It is like putting a letter in an envelope, then putting that envelope in a courier bag with a different label.
The layers wrap each other
Application [ HTTP request ]
Transport [TCP hdr][ HTTP request ] = segment
Internet [IP hdr][TCP hdr][ HTTP request ] = packet
Link [Eth hdr][IP hdr][TCP hdr][ HTTP request ][Eth FCS] = frame
Physical 0110100101110... (bits on the wire)Each layer treats everything from the layer above as an opaque payload. The IP layer doesn't know or care that it's carrying TCP, and TCP doesn't care that the bytes are HTTP.
Building it in code
Here is a toy encapsulation. Each layer adds a readable header so you can see the nesting. Real headers are compact binary structures, which you'll build in later lessons.
def application(text):
return text.encode()
def transport(payload, src_port, dst_port):
header = f"[TCP {src_port}->{dst_port}]".encode()
return header + payload
def internet(segment, src_ip, dst_ip):
header = f"[IP {src_ip}->{dst_ip}]".encode()
return header + segment
def link(packet, src_mac, dst_mac):
header = f"[ETH {src_mac}->{dst_mac}]".encode()
trailer = b"[FCS]"
return header + packet + trailer
data = application("GET / HTTP/1.1")
segment = transport(data, 51514, 80)
packet = internet(segment, "192.168.1.20", "93.184.215.14")
frame = link(packet, "aa:aa", "bb:bb")
for name, unit in [("data", data), ("segment", segment), ("packet", packet), ("frame", frame)]:
print(f"{name:<8} {len(unit):>3} bytes {unit.decode()}")data 14 bytes GET / HTTP/1.1
segment 29 bytes [TCP 51514->80]GET / HTTP/1.1
packet 61 bytes [IP 192.168.1.20->93.184.215.14][TCP 51514->80]GET / HTTP/1.1
frame 84 bytes [ETH aa:aa->bb:bb][IP 192.168.1.20->93.184.215.14][TCP 51514->80]GET / HTTP/1.1[FCS]
Decapsulation
The receiving host peels the layers off from the outside in. Each layer checks that the header is meant for it before passing the payload up:
frame = b"[ETH aa:aa->bb:bb][IP 192.168.1.20->93.184.215.14][TCP 51514->80]GET / HTTP/1.1[FCS]"
def strip(unit, prefix):
assert unit.startswith(prefix), f"expected {prefix!r}"
end = unit.index(b"]") + 1
return unit[1:end - 1].decode(), unit[end:]
hdr, rest = strip(frame, b"[ETH")
rest = rest.removesuffix(b"[FCS]")
print("link layer read: ", hdr)
hdr, rest = strip(rest, b"[IP")
print("internet layer read: ", hdr)
hdr, rest = strip(rest, b"[TCP")
print("transport layer read:", hdr)
print("application got: ", rest.decode())link layer read: ETH aa:aa->bb:bb
internet layer read: IP 192.168.1.20->93.184.215.14
transport layer read: TCP 51514->80
application got: GET / HTTP/1.1
Overhead
Headers cost bandwidth. With real header sizes, here is the share of each Ethernet frame that is actual application data:
| Header | Size |
|---|---|
| Ethernet header + FCS | 14 + 4 bytes (plus 8 preamble and 12 inter-frame gap on the wire) |
| IPv4 header | 20 bytes (no options) |
| TCP header | 20 bytes (no options; usually 32 with timestamps) |
| UDP header | 8 bytes |
ETH = 14 + 4
WIRE_EXTRA = 8 + 12 # preamble + inter-frame gap
IP, TCP = 20, 20
for payload in [1, 64, 512, 1460]:
frame = ETH + IP + TCP + payload
wire = frame + WIRE_EXTRA
print(f"payload {payload:>4} B -> frame {frame:>4} B, on the wire {wire:>4} B, "
f"efficiency {payload / wire:6.1%}")payload 1 B -> frame 59 B, on the wire 79 B, efficiency 1.3%
payload 64 B -> frame 122 B, on the wire 142 B, efficiency 45.1%
payload 512 B -> frame 570 B, on the wire 590 B, efficiency 86.8%
payload 1460 B -> frame 1518 B, on the wire 1538 B, efficiency 94.9%
A 1-byte keystroke in an SSH session costs about 80 bytes on the wire. Full-size packets are about 95% efficient. That's why protocols try to send data in large chunks, and why a minimum Ethernet frame is padded to 64 bytes: a tiny message wastes most of the link.
MTU: how big can a packet be?
Every link has a Maximum Transmission Unit (MTU): the biggest packet (layer 3 and up) it can carry in one frame. For Ethernet the standard MTU is 1500 bytes. Subtract the IP and TCP headers and you get the Maximum Segment Size (MSS), the most application data per TCP segment:
for name, mtu, extra in [("Ethernet", 1500, 0), ("PPPoE (DSL)", 1492, 0),
("IPv6 over Ethernet", 1500, 20), ("VPN tunnel", 1420, 0)]:
ip_header = 20 + extra
mss = mtu - ip_header - 20
print(f"{name:<20} MTU {mtu} MSS {mss}")Ethernet MTU 1500 MSS 1460
PPPoE (DSL) MTU 1492 MSS 1452
IPv6 over Ethernet MTU 1500 MSS 1440
VPN tunnel MTU 1420 MSS 1380
Note: If a packet is bigger than the next link's MTU, IPv4 routers may fragment it (split it into pieces) or drop it and send an ICMP "fragmentation needed" message back. IPv6 routers never fragment. Many mysterious "large downloads hang but small pages work" bugs are MTU problems, typically inside VPN tunnels where extra headers shrink the available space.
Test yourself
In which order are headers added when a host sends a web request over Ethernet?
- Ethernet, IP, TCP
- TCP, IP, Ethernet
- IP, TCP, Ethernet
- TCP, Ethernet, IP
With an Ethernet MTU of 1500 bytes and 20-byte IPv4 and TCP headers, what is the MSS?
- 1500
- 1480
- 1460
- 1440
What does the IP layer do with the TCP header inside its payload?
- Rewrites the ports
- Checks the HTTP data
- Nothing: it treats the payload as opaque bytes
- Removes it