Sub topic 1
Why networks are built in layers
Prerequisites
This chapter assumes you can use a terminal and run simple commands. It also assumes you know what a client and a server are, and that you have used a network and the internet. You do not need to know any protocol details yet. Later chapters cover the OSI and TCP/IP models, and this one explains the idea they share.
The problem layers solve
Sending a web page from a server to a browser involves many separate jobs:
- Turning bits into electrical, optical, or radio signals.
- Delivering data across one link, such as a cable or a Wi-Fi network.
- Finding a path across many networks.
- Making sure data arrives complete and in order.
- Giving the data meaning, such as a web request or a DNS lookup.
If one program handled all of this, every application would need to know about every kind of cable and radio. Changing one part would risk breaking the rest. Network designers avoid this by splitting the work into layers.
What a layer is
A layer is a group of related functions with a defined job. It offers a service to the layer above it and uses the service of the layer below it. A layer does not need to know how the layers around it work. It only needs to know the interface, which is the agreed way to ask for the service.
A protocol is a set of rules that two peers follow to talk to each other at the same layer. A web browser and a web server follow HTTP with each other. They do not care whether the bits travel over Ethernet or Wi-Fi.
The dotted lines show a key idea. Each layer behaves as if it talks directly to the same layer on the other host. In reality, the data goes down the stack, across the medium, and up the other stack.
Benefits of layering
- Modularity. You can change one layer without rewriting the others. Moving from Wi-Fi to Ethernet does not change how your browser works.
- Interoperability. Devices from different vendors work together if they follow the same protocol at each layer.
- Simpler design and debugging. Each layer solves a smaller problem. When something breaks, you can test one layer at a time.
- Reuse. Many applications share the same transport and network layers. HTTP, SMTP, and DNS do not each need their own way to cross the internet.
- Shared vocabulary. Engineers say "a layer 2 problem" or "a transport issue" and others know where to look.
Costs of layering
Layering has trade-offs:
- Overhead. Each layer adds its own header, which is control information placed before the data. More headers mean more bytes on the wire.
- Hidden information. A layer cannot always see what it would need to make a good choice. For example, a transport protocol may not know that a link is lossy Wi-Fi, so it may treat packet loss as congestion.
- Imperfect boundaries. Some protocols do not fit neatly in one layer. A later chapter covers these cases.
Seeing layering in code
The following program sends one message. It supplies only the data, an address, and a port. The operating system adds the transport, network, and link-layer headers below it.
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(b"hello", ("127.0.0.1", 9999))The program never mentions Ethernet, routing, or checksums. That is layering at work. The socket API is the interface between the application and the layers below it.
Two models, one idea
Two layered models are in common use. The OSI reference model has seven layers and is used mainly for teaching and for naming layers. The TCP/IP model has four layers and describes the protocols the internet actually uses. The next chapters cover each one. Both rest on the idea in this chapter: each layer does one job and hides its details from the others.
Exercise
A browser developer wants the browser to work the same over Ethernet and Wi-Fi. Which benefit of layering makes this possible, and what is one cost of the design?
Show answer
Modularity. The browser uses the layers below through a fixed interface, so a change of link technology does not affect it. One cost is overhead: each layer adds a header, so more bytes travel on the wire for the same data.
Exercise
In the Python example, which parts of the packet does the program supply, and which parts does the operating system add?
Show answer
The program supplies the data (b"hello"), the destination address (127.0.0.1), and the UDP port (9999). The operating system adds the UDP, IP, and link-layer headers.