Sub topic 1
Why Networks Are Built in Layers
Prerequisites
This chapter assumes you can do the following:
- You use networks and the internet daily, and you know what a client and a server are.
- You can run commands in a terminal and read short Python scripts.
- You have heard terms such as IP address, port, HTTP, and Wi-Fi, but you do not need to know exactly how they work. Later chapters cover each one.
The problem layering solves
Consider what happens when a browser loads a web page. The page text must be formatted into a request. The request must be split into pieces that fit the network. Each piece needs a destination address and a route across many networks. Bits must become voltage, light, or radio waves. Lost pieces must be detected and resent, and the receiver must put the pieces back in order.
One program that did all of this would be huge. It would also be tied to specific hardware. Every new application, such as email, video calls, or a game, would repeat the same work. Every new link technology, such as a new Wi-Fi standard, would force changes in every application.
Network designers handle this the way software engineers handle any large system. They divide the work into layers. Each layer solves one part of the problem and builds on the layer below it.
What a layer is
A layer is a group of related functions with a defined job. It has three parts you should keep apart:
- Service: what the layer offers to the layer above. For example, the transport layer offers "deliver these bytes to a program on another host."
- Interface: how the layer above asks for that service. On most systems this is a set of function calls, such as the sockets API.
- Protocol: the rules a layer uses to talk to the same layer on another machine. TCP on your laptop follows the TCP rules to talk to TCP on the server.
Two rules govern how layers interact:
- A layer uses only the service of the layer directly beneath it. It does not need to know how that layer works.
- A layer talks to its peer layer on the remote machine through a protocol. In practice, the data still travels down the local stack, across the physical medium, and up the remote stack.
The second rule gives each layer a virtual conversation with its peer. Your HTTP client acts as if it speaks directly to the HTTP server. In reality, every message passes through all the layers below it on both machines.
What layering buys you
Independent change
Because each layer sees only the interface below it, you can replace the implementation without touching the layers above. IP runs over Ethernet, Wi-Fi, cellular, fiber, and satellite links. A web browser does not change when you move from a cable to Wi-Fi. The link layer changes, and everything above it keeps working.
Reuse
One transport protocol serves many applications. HTTP, SSH, and SMTP all use TCP. Each of them gets ordered, reliable delivery without implementing it. The same holds in the other direction. One application protocol can run over different networks without modification.
Bounded complexity
A person can understand one layer at a time. A developer writing a web service does not need to know how a switch forwards Ethernet frames. A network engineer configuring routes does not need to parse HTTP headers. Each person works with a smaller model.
Interoperability
Layers give vendors and standards bodies precise targets. If two implementations follow the same TCP specification, they interoperate, whoever wrote them. This is why a phone, a Linux server, and a router from different companies can exchange data.
Focused troubleshooting
When something fails, layers give you an order for testing. Check whether the cable or Wi-Fi link is up, then whether IP reaches the destination, then whether the port accepts connections, then whether the application answers correctly. The last chapter of this tutorial develops that method fully. For now, remember that layering makes it possible.
Seeing the separation in code
The sockets API is the interface between your program and the transport layer. This pair of scripts sends one UDP message on the local machine. Start the receiver first.
# receiver.py
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.bind(("127.0.0.1", 9999))
data, addr = s.recvfrom(2048)
print(f"received {data!r} from {addr}")Then run the sender in a second terminal.
# sender.py
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(b"hello", ("127.0.0.1", 9999))The sender supplies only three things: the application data, an address, and a UDP port. It never builds a UDP header, an IP header, or an Ethernet frame. The operating system adds the UDP, IP, and link-layer headers below it. The sender also never decides whether the traffic leaves through Wi-Fi or a cable. If you change 127.0.0.1 to a remote address, the same code works unchanged, and the routing and link-layer steps change without any edit to the program.
You can see the same idea from the other direction. Scapy, a Python packet library, builds each layer explicitly and stacks the layers with the / operator.
from scapy.all import Ether, IP, TCP
pkt = Ether() / IP(dst="192.0.2.10") / TCP(dport=80, flags="S")
pkt.show()The output lists an Ethernet section, an IP section, and a TCP section, in that order. Scapy makes visible what the operating system normally hides. Each layer is a separate piece with its own fields. The address 192.0.2.10 belongs to a range reserved for documentation, so the packet is safe to build, though you should not send it anywhere you do not control.
The cost of layers
Layering is a trade-off. It is the dominant design, but you should know what it costs.
| Cost | What it means in practice |
|---|---|
| Header overhead | Each layer adds its own header. A small payload can be mostly headers. |
| Hidden information | A layer cannot see details of other layers. TCP does not know whether it runs over Wi-Fi, which can lose packets for reasons that have nothing to do with congestion. |
| Duplicated work | Several layers may each check for errors or each do some kind of addressing. |
| Layer violations | Real devices often look across layers, which makes the clean picture less accurate. |
The last row matters most in practice. A NAT device rewrites IP addresses and also transport ports, so it works at two layers at once. A firewall may inspect application data. A load balancer may make routing decisions from HTTP headers. QUIC, a newer protocol, gives up a clean split by combining transport and security features and running over UDP. These designs exist because crossing layers solves real problems, and they pay for it with complexity. The mapping chapters later in this tutorial return to these cases.
Two models for the same idea
Two layered models are in common use. The OSI reference model has seven layers and was written as a general framework for talking about any network. The TCP/IP model has four layers and describes the protocol suite the internet actually runs on. They share the same principle: stacked layers, each providing a service to the one above.
This chapter stops at the principle. The next chapters introduce the seven OSI layers and the four TCP/IP layers in order, then show how data is wrapped on its way down and unwrapped on its way up.
Common errors
- Believing layers talk only to their neighbors on the wire. Peer layers communicate by protocol, but the bytes always go through every layer below. There is no direct path between two application layers.
- Treating the layer number as a speed or importance ranking. Higher layers are closer to the user, not better or faster.
- Assuming each device uses all layers. A switch forwarding Ethernet frames works at the link layer. A router works up through the internet layer. End hosts run the whole stack.
- Assuming every protocol fits one layer. Several protocols, including ARP, ICMP, and TLS, are famous for not doing so. A later chapter covers them.
Exercise
A developer switches a server from a wired Ethernet connection to Wi-Fi. The web application code does not change, and it keeps working. Which two layering properties explain this, and which layer actually changed?
Show answer
The properties are that each layer uses only the service of the layer below through a fixed interface, and that implementations can be replaced independently. The link layer changed (Ethernet to Wi-Fi). IP, TCP, and the application above it continue to use the same service from the layer below, so they need no change.
Exercise
In the sender script above, which headers does your program build itself, and which does the operating system add? Name the layer for each.
Show answer
The program builds none of the headers. It supplies only the application data, a destination address, and a UDP port. The operating system adds the UDP header (transport layer), the IP header (internet layer), and the link-layer header and trailer.
Exercise
Give one advantage and one disadvantage of layering, using a concrete example for each.
Show answer
Advantage: reuse, since HTTP, SSH, and SMTP can all rely on TCP for reliable delivery without each implementing it. Disadvantage: hidden information, since TCP cannot tell whether a lost packet came from congestion or from a noisy Wi-Fi link, so it may react wrongly. Header overhead and layer violations such as NAT are also valid answers.