Sub topic 10
Data Units and Devices at Each Layer
This chapter assumes you know the seven OSI layers by name and have seen encapsulation: each layer adds its own header, and the receiver removes the headers in reverse order. Here you attach two practical labels to each layer. The first is the name of the data unit at that layer. The second is the network device that works at that layer. These labels explain what you see in packet captures and where a device fits in a network.
Protocol data units
A protocol data unit (PDU) is the chunk of data that one layer handles, including that layer's own header. Each layer has its own name for its PDU:
| Layer | Name | PDU | Address or identifier used |
|---|---|---|---|
| 7 | Application | Data (message) | URL, hostname, user name |
| 6 | Presentation | Data | Encoding or format label |
| 5 | Session | Data | Session or connection ID |
| 4 | Transport | Segment (TCP) or datagram (UDP) | Port number |
| 3 | Network | Packet | IP address |
| 2 | Data Link | Frame | MAC address |
| 1 | Physical | Bits (symbols) | None |
Layers 5 to 7 are usually grouped and called "data". In practice these layers are often implemented inside one application, so there is no separate header you can point to for each.
The names are only conventions. People say "packet" for almost anything that crosses a network, and that is usually fine. The precise names matter when you must say where a problem sits. "The frame was dropped" points at Layer 2. "The segment was retransmitted" points at TCP.
How a PDU grows
Each layer treats the PDU from the layer above as an opaque payload. It adds a header, and Layer 2 also adds a trailer. The result becomes the next layer's payload:
- The application produces data, for example an HTTP request.
- TCP adds a header (20 bytes without options). The result is a segment.
- IPv4 adds a header (20 bytes without options). The result is a packet.
- Ethernet adds a 14-byte header and a 4-byte frame check sequence (FCS) trailer. The result is a frame.
- The network card turns the frame into signals on the medium.
Computing the overhead
The following script calculates the PDU sizes for a 4000-byte application message sent over TCP, IPv4, and Ethernet with a standard 1500-byte MTU. The maximum transmission unit (MTU) is the largest payload a link will carry in one frame. The maximum segment size (MSS) is the largest amount of TCP data that fits in one packet.
import math
ETH_HEADER = 14 # destination MAC, source MAC, EtherType
ETH_FCS = 4 # frame check sequence trailer
IPV4_HEADER = 20 # no options
TCP_HEADER = 20 # no options
MTU = 1500 # largest IP packet one Ethernet frame carries
mss = MTU - IPV4_HEADER - TCP_HEADER
app_bytes = 4000
segments = math.ceil(app_bytes / mss)
print(f"MSS = {mss} bytes, segments needed = {segments}")
remaining = app_bytes
for i in range(1, segments + 1):
data = min(remaining, mss)
remaining -= data
segment = TCP_HEADER + data
packet = IPV4_HEADER + segment
frame = ETH_HEADER + packet + ETH_FCS
print(f"#{i}: data={data} segment={segment} packet={packet} frame={frame}")The output is:
MSS = 1460 bytes, segments needed = 3
#1: data=1460 segment=1480 packet=1500 frame=1518
#2: data=1460 segment=1480 packet=1500 frame=1518
#3: data=1080 segment=1100 packet=1120 frame=1138TCP, not IP, splits the message. It sizes each segment so that the resulting packet fits the MTU, which avoids IP fragmentation. The 1518-byte frame is the classic maximum Ethernet frame size, counting the header and FCS but not the preamble.
Devices and the layer they work at
A device "works at" a layer when it reads and acts on that layer's header. It also handles everything below. A switch reads the Ethernet header, so it is a Layer 2 device. It still has to handle Layer 1 signaling to receive and send bits.
A device generally does not look inside the payload of its own layer. A router reads the IP header to decide where to send a packet. It does not read the TCP header, and it does not need to.
Layer 1: repeaters, hubs, cables, and media converters
A repeater regenerates a weakened signal so it can travel farther. A hub is a multi-port repeater. It copies every incoming signal to all other ports. It has no concept of addresses. All devices on a hub share one collision domain, which is the set of devices whose transmissions can interfere with each other. Hubs are obsolete, but they are still the clearest example of Layer 1 behavior. Cables, connectors, and media converters (copper to fiber) also belong here.
Layer 2: network cards, bridges, switches, and access points
A network interface card (NIC) sends and receives frames, and it stores the MAC address. It straddles Layers 1 and 2.
A switch is a multi-port bridge. It builds a MAC address table by recording the source MAC address of each frame against the port it arrived on. Then it forwards each frame only to the port that holds the destination MAC address. If the destination is unknown, or the frame is a broadcast, the switch floods the frame out of all ports except the one it came in on. Each switch port is its own collision domain. All ports in the same VLAN still share one broadcast domain, which is the set of devices that receive a Layer 2 broadcast.
A Wi-Fi access point bridges wireless frames to wired Ethernet frames. It also works at Layers 1 and 2.
Layer 3: routers and Layer 3 switches
A router connects different IP networks. It reads the destination IP address, looks up the longest matching prefix in its routing table, and forwards the packet to the next hop. Each router interface is a separate broadcast domain. This is why routers contain broadcast traffic.
A Layer 3 switch is a switch that can also route between VLANs in hardware. It is common in campus and data center networks.
Layers 4 to 7: firewalls, load balancers, and proxies
Devices above Layer 3 look at ports and application content.
- A stateful firewall tracks TCP connections and UDP flows. It filters by IP address, port, and connection state, which is Layer 3 and 4 behavior.
- A next-generation firewall or web application firewall inspects application content, such as HTTP paths or TLS server names. That is Layer 7.
- A Layer 4 load balancer picks a backend by IP address and port without reading the payload.
- A Layer 7 load balancer or reverse proxy reads the HTTP request and can route by path, header, or cookie.
Summary of devices
| Device | Highest layer read | Forwards by | Domains it separates |
|---|---|---|---|
| Hub, repeater | 1 | Nothing (copies bits) | None |
| Switch, bridge | 2 | Destination MAC | Collision domains |
| Router | 3 | Destination IP | Broadcast domains |
| Stateful firewall | 3 to 4 | IP, port, state | Policy zones |
| L7 load balancer, proxy | 7 | Application content | Policy and connection |
What changes at each hop
When a packet crosses a router, the layers behave differently:
- The Layer 3 header is mostly kept. The source and destination IP addresses stay the same from end to end. The router only decrements the time to live (TTL) and recalculates the header checksum. The exception is network address translation (NAT), which rewrites addresses.
- The Layer 2 header is replaced at every hop. The router removes the incoming frame, chooses an outgoing interface, and builds a new frame. The new frame has the router's MAC address as the source and the next hop's MAC address as the destination.
The switch passes the frame unchanged, because it only decides which port to use. The router builds a new frame, because it connects two separate Layer 2 networks.
Gotchas and edge cases
- Devices do not fit one layer neatly. A home "router" is usually a router, a switch, an access point, a DHCP server, a NAT gateway, and a firewall in one box. Classify each function, not the box.
- Layer numbers for firewalls vary. Vendors say "Layer 7 firewall" when the device reads application data, and "Layer 4 firewall" when it only reads ports. Check what the device inspects.
- Encryption hides layers. A switch or router can read its own header even when the payload is encrypted. A Layer 7 device cannot read HTTPS content unless it terminates TLS. This is why a load balancer that routes by URL path must decrypt the traffic first.
- Tunnels add headers. VPNs and overlay networks such as VXLAN wrap a whole packet or frame inside another one. You then see two sets of headers at the same layer, and the extra bytes reduce the effective MTU.
- UDP has a different name. A UDP PDU is a datagram, not a segment. UDP has an 8-byte header, so the overhead calculation above would change.
Exercise
A 3000-byte application message is sent over TCP with IPv4 over Ethernet. The MTU is 1500, and there are no IP or TCP options. How many frames are sent, and what is the size of the last frame including the 14-byte header and 4-byte FCS?
Show answer
The MSS is 1500 - 20 - 20 = 1460 bytes. 3000 / 1460 needs 3 segments: 1460, 1460, and 80 bytes of data. The last segment is 20 + 80 = 100 bytes, the packet is 120 bytes, and the frame is 14 + 120 + 4 = 138 bytes. Three frames are sent.
Exercise
Host A on one network sends a packet to Host B on another network through a switch and one router. Which addresses change when the packet passes through the router, and which stay the same?
Show answer
The source and destination MAC addresses change, because the router builds a new Ethernet frame for the next network. The source and destination IP addresses stay the same (unless NAT is in use). The router only decrements the TTL and updates the IP header checksum.
Exercise
You replace a hub with a switch on a small network. Which kind of domain gets smaller, and which stays the same size?
Show answer
Collision domains get smaller. Each switch port becomes its own collision domain, whereas a hub puts all devices in one. The broadcast domain stays the same size, because a switch floods broadcasts to all ports in the same VLAN. Only a router, or a separate VLAN, splits it.