Sub topic 1
Why Networks Are Modeled in Layers
Prerequisites
This chapter assumes you know what a computer network and the internet are. It also assumes you recognize terms such as IP address, web server, and router, and that you can run simple commands in a terminal. You do not need to know the individual OSI layers yet. Later chapters cover them one by one.
The problem layering solves
Sending a web page from a server to your laptop involves many separate jobs. Something must turn bits into electrical, optical, or radio signals. Something must pick the next device on the local link. Something must choose a route across many networks. Something must retransmit lost data. Something must understand the request for the page itself.
If one program did all of this, every application would need to know about every cable type, every network card, and every routing rule. Changing Wi-Fi to Ethernet would mean rewriting your browser.
A layered model avoids this by dividing the work into layers. Each layer has one job. It offers a service to the layer above it and uses the service of the layer below it. The rules for how two peer layers talk to each other on different machines form a protocol. The boundary between two adjacent layers on the same machine is an interface.
The OSI model
The OSI model (Open Systems Interconnection) was published by ISO as a reference model. It has seven layers. Layer 7 is nearest the user's application, and layer 1 is nearest the wire.
| Layer | Name | Core question it answers |
|---|---|---|
| 7 | Application | What does the application want to say? |
| 6 | Presentation | How is the data encoded, compressed, or encrypted? |
| 5 | Session | How is the dialogue started, kept, and ended? |
| 4 | Transport | How is data delivered between processes, reliably or not? |
| 3 | Network | How does a packet reach a host on another network? |
| 2 | Data Link | How does a frame cross one local link? |
| 1 | Physical | How are bits sent as signals? |
A common memory aid, from top to bottom, is "All People Seem To Need Data Processing."
What you gain from layers
- Independence. You can replace one layer's implementation without changing the others. HTTP works the same over Wi-Fi and over a wired link, because the layers below hide the difference.
- Interoperability. Vendors who follow the same interface and protocol rules can build parts that work together.
- Simpler design. Each layer solves a smaller problem, so it is easier to build and test.
- Shared vocabulary. Engineers say "a layer 2 problem" or "a layer 7 firewall" and everyone knows roughly what is meant.
- Focused troubleshooting. You can test one layer at a time, which a later chapter covers.
A worked example
Suppose you open http://example.com in a browser. Each layer contributes one part of the work:
- The browser creates an HTTP request (layer 7).
- The operating system opens a TCP connection to port 80 (layer 4).
- IP addresses the data to the server's address (layer 3).
- Ethernet or Wi-Fi sends it to the next device on your local link, usually your router (layer 2).
- The network card turns the frame into signals (layer 1).
Each layer only deals with its own information. TCP does not care whether the HTTP request asks for a picture or a page, and Ethernet does not care whether the packet inside is going to a web server or a mail server.
You can see this separation in code. The following script opens a connection and sends a request. It never mentions cables, MAC addresses, or routes, because the layers below handle them.
import socket
request = b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n"
with socket.create_connection(("example.com", 80), timeout=5) as s:
s.sendall(request)
data = s.recv(200)
print(data.decode("ascii", errors="replace").splitlines()[0])The program writes layer 7 text. The call to create_connection asks the operating system for a layer 4 service. Everything beneath that is hidden.
Limits of the model
Layering also has costs. Each layer adds header bytes and processing. Strict separation can hide information that a lower or higher layer would find useful, such as an application not knowing that a link is slow.
Common misunderstandings
- "Each layer talks directly to the same layer on the other machine." Logically it does, through protocol rules. Physically, data goes down the stack, across the medium, and up the stack on the other side. The next chapter explains how.
- "A protocol belongs to exactly one layer." Many do not fit that cleanly. Use the layer number as a guide, not a strict rule.
- "Higher layers are more important." Each layer is needed. A fault at any layer stops communication.
Exercise
A developer switches a server from a wired Ethernet connection to Wi-Fi, and the web application keeps working with no code changes. Which benefit of layering explains this, and which layers changed?
Show answer
Independence between layers. Only the lower layers changed (Data Link and Physical, layers 2 and 1). The application protocol (HTTP) and the transport (TCP) are unaware of the link type, because each layer uses only the service of the layer below it.
Exercise
Name the OSI layer that answers each question: (a) how a frame crosses one local link, (b) how a packet reaches a host on another network, (c) how data is delivered between processes.
Show answer
(a) Data Link, layer 2. (b) Network, layer 3. (c) Transport, layer 4.