Sub topic 1
Why Networks Are Modeled in Layers
Prerequisites
This chapter assumes you already know the following:
- What a computer network and the internet are, at a basic level.
- Common terms such as IP address, web server, and router.
- How to run simple commands in a terminal. A few examples use Python 3 and standard Linux tools.
You do not need to know any protocol in detail. This chapter explains why the seven-layer model exists. Later chapters cover each layer.
The problem layering solves
Sending a web page from a server to your laptop involves many separate jobs:
- Turning data into electrical, optical, or radio signals.
- Deciding which device on a local link should receive a signal.
- Finding a path across many networks.
- Making sure lost data is sent again.
- Encrypting the data.
- Formatting the request so that a web server understands it.
Suppose one program did all of this. Every web browser, mail client, and game would need code for Wi-Fi, Ethernet, fiber, and cellular links. Changing one part, such as replacing copper cable with fiber, would mean rewriting everything.
Layering avoids this. The communication task is split into a stack of layers. Each layer does one well-defined job and talks to its neighbors through a fixed interface. A layer does not need to know how the layers around it work. It only needs to know what service the layer below offers and what service it must provide to the layer above.
What a layered model gives you
- Modularity. You can replace one layer's implementation without touching the others. Your browser works the same over Wi-Fi, Ethernet, or a mobile connection, because HTTP never deals with radio signals.
- Interoperability. Vendors that follow the same layer definitions can build equipment that works together. A router from one company can forward packets from a switch made by another.
- Simpler design and debugging. Each layer is a smaller problem. When something breaks, you can test one layer at a time.
- A shared vocabulary. "That is a Layer 2 problem" tells another engineer where to look.
Layering has costs too. Each layer adds header bytes and processing time. Strict separation can also hide information that another layer could use, for example a transport protocol that cannot tell whether a loss was caused by congestion or by a noisy radio link. Real systems sometimes bend the boundaries for this reason.
The OSI model
The Open Systems Interconnection (OSI) reference model was published by the International Organization for Standardization (ISO). It divides networking into seven layers. Layer 1 is at the bottom, closest to the wire, and Layer 7 is at the top, closest to the user's application.
| Layer | Name | Main responsibility |
|---|---|---|
| 7 | Application | Protocols that applications use to talk, such as HTTP, DNS, SMTP |
| 6 | Presentation | Data encoding, encryption, compression |
| 5 | Session | Starting, managing, and ending dialogues between applications |
| 4 | Transport | End-to-end delivery between processes, ports, reliability (TCP, UDP) |
| 3 | Network | Logical addressing and routing between networks (IP) |
| 2 | Data Link | Delivery between devices on the same link, using MAC addresses |
| 1 | Physical | Moving raw bits over a medium |
A common memory aid, from Layer 7 down to Layer 1, is "All People Seem To Need Data Processing". From Layer 1 up, it is "Please Do Not Throw Sausage Pizza Away".
How layers communicate
Two kinds of communication happen in a layered model.
- Vertical communication. On one host, each layer uses the services of the layer directly below it and provides services to the layer directly above it. The boundary between two adjacent layers is called an interface.
- Horizontal communication. A layer on one host exchanges messages with the same layer on the other host. These exchanges follow a protocol, which is the set of rules for the messages. The two TCP implementations on the two hosts talk to each other, even though the messages actually travel down through the lower layers, across the wire, and back up.
The terms matter. A service is what a layer offers to the layer above. A protocol is how peer layers on different hosts talk. The service interface is local to one machine. The protocol is shared between machines.
A first look at a layer boundary in code
The next two examples show that layers have different jobs. Both are short and can be run as shown.
The first parses a 14-byte Ethernet II header. This is Layer 2 information: a destination MAC address, a source MAC address, and an EtherType. The EtherType tells the receiver which upper-layer protocol is inside the frame. Here, 0x0806 means ARP.
import struct
def mac(b):
return ":".join(f"{x:02x}" for x in b)
# 14-byte Ethernet II header: destination MAC, source MAC, EtherType
header = bytes.fromhex("ffffffffffff" "001122334455" "0806")
dst, src, ethertype = struct.unpack("!6s6sH", header)
print(mac(dst), mac(src), hex(ethertype))
# ff:ff:ff:ff:ff:ff 00:11:22:33:44:55 0x806The destination ff:ff:ff:ff:ff:ff is the broadcast address, so every device on the local link receives the frame. The EtherType field is how Layer 2 hands data upward without knowing what the payload means.
The second example works at a much higher level. It opens a TCP connection and sends a plain-text HTTP request. The program writes only the Layer 7 text. The operating system handles TCP (Layer 4), IP (Layer 3), and the link and physical layers.
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])This prints a status line such as HTTP/1.1 200 OK. The code never mentions MAC addresses, routing, or signals. That is the benefit of layering: the program works whether the laptop is on Wi-Fi or a wired link.
Why seven layers?
The seven-layer split is a design choice, not a law of nature. The OSI designers grouped functions using a few principles:
- Create a new layer where the level of abstraction is clearly different.
- Give each layer a well-defined function.
- Choose boundaries so that information crossing the interface is minimal.
- Keep the number of layers large enough that distinct functions are not crammed together, and small enough that the stack stays manageable.
Not every layer carries the same weight in practice. Layers 1 to 4 correspond closely to distinct, well-understood mechanisms: signals, frames, packets, and end-to-end transport. Layers 5 and 6 are often merged into the application in real software. For example, TLS encrypts data (a Layer 6 job) but is implemented as a library that applications call, and there is no separate "session layer" program on most hosts. Later chapters return to this when the OSI model is compared with the TCP/IP model.
Common misconceptions
- "Every network follows OSI exactly." The Internet runs on TCP/IP, whose protocols were designed before and independently of OSI's seven-layer scheme. OSI is a reference model. It is used to teach and to discuss, not as a specification that real software implements layer by layer.
- "Each layer is a separate piece of software." Layers 1 and 2 are often in hardware and firmware, Layers 3 and 4 are in the operating system kernel, and Layers 5 to 7 are often inside one application. Sometimes a single protocol spans what the model treats as more than one layer.
- "Data stays unchanged as it moves down." Each layer adds its own information to the data. This wrapping is called encapsulation, and the next chapter covers it.
- "A problem is always at one layer." A fault in a lower layer shows up as symptoms higher up. A loose cable (Layer 1) looks like a failed website (Layer 7). Layer thinking helps you decide where to start, not where the fault must be.
Using layers as a thinking tool
When you meet an unfamiliar protocol or device, ask three questions:
- What problem does it solve: signaling, local delivery, routing, end-to-end delivery, dialogue control, data format, or application meaning?
- What does it assume the layer below already provides?
- What does it promise to the layer above?
For example, IP (Layer 3) assumes that some link can carry a packet one hop, and promises best-effort delivery across networks. It does not promise that packets arrive, arrive once, or arrive in order. TCP (Layer 4) builds those promises on top of IP's weaker service. Seeing a protocol as "a bridge between what is below and what is above" makes new protocols much easier to place.
Exercise
A developer replaces a wired Ethernet connection with Wi-Fi. The web browser needs no changes. Which property of layered models explains this, and which layers changed?
Show answer
Modularity: each layer depends only on the service offered by the layer below. Only the Physical and Data Link layers changed (Wi-Fi signals and frames instead of Ethernet cable). The browser works at the Application layer and uses the same HTTP over TCP and IP as before.
Exercise
What is the difference between a service and a protocol in a layered model?
Show answer
A service is what a layer offers to the layer directly above it on the same host, through an interface. A protocol is the set of rules that peer layers on two different hosts use to exchange messages with each other.
Exercise
In the Ethernet header example, which field tells the receiver which upper-layer protocol is carried in the frame, and what does the value 0x0806 mean?
Show answer
The EtherType field. The value 0x0806 means the payload is an ARP message.