Computer Networking › Section A · Computer networks and the internet › Topic A.5

Protocol layers and encapsulation

The internet runs hundreds of protocols on billions of different devices. Layering is the idea that keeps this manageable: each layer does one job for the layer above it, and encapsulation is how the layers cooperate on every single packet.

Why build a network in layers

A network has to do many separate jobs: turn bits into signals, move frames across one link, find a route across many networks, deliver data reliably to the right program, and support each application. Layering splits these jobs into a stack. Each layer provides a service to the layer above it by doing some work of its own and using the service of the layer directly below. The great benefit is modularity: a layer's implementation can change completely, as long as it offers the same service. Wi-Fi can replace Ethernet without any change to browsers, and a new application needs no change to routers.

Service models and peer protocols

The service a layer offers upwards is its service model. For example, a layer may promise reliable, in-order delivery while using an unreliable layer below it, by detecting losses and resending. Each protocol belongs to one layer, and the protocols of all the layers together form the protocol stack. Peers at the same layer on different machines exchange messages according to their protocol, but physically the data goes down one stack, across the links and up the other. Upper layers are mostly software in hosts; the link and physical layers live largely in network interface hardware.

The top two layers

This course uses the five-layer internet stack. At the top, the application layer holds network applications and their protocols, such as HTTP for the web, SMTP for email and DNS for translating names into addresses; its unit of data is a message. Below it, the transport layer carries application messages between the two end points. The internet has two transport protocols: TCP, which offers a connection, guaranteed in-order delivery, flow control and congestion control, and UDP, which offers a bare, connectionless service with no guarantees. A transport-layer packet is called a segment.

The bottom three layers

The network layer carries packets, called datagrams, across many networks, all the way between the two hosts. It contains IP, which defines the datagram's fields and how hosts and routers act on them, and the routing protocols that choose paths. The link layer moves a frame from one node to the next node on the path, across a single link; Ethernet, Wi-Fi and the DOCSIS cable protocol are link-layer protocols, and a datagram may cross a different kind of link on each hop. The physical layer moves the individual bits of a frame from one node to the next as signals; its protocols depend on both the link and the medium, such as twisted pair or fibre.

The OSI model and layer numbers

The International Organization for Standardization published the Open Systems Interconnection (OSI) reference model in 1984. It has seven layers, numbered from the bottom: physical (1), data link (2), network (3), transport (4), session (5), presentation (6) and application (7). Presentation covers data formats, compression and encryption; session covers checkpointing and synchronising a long exchange. The internet stack has no separate layers for these: an application that needs them builds them in. OSI's numbers remain everyday vocabulary, as in a layer 2 switch or a layer 7 load balancer. The Computer Science topic Internet protocols: layers, TCP, and UDP counts four layers, merging link and physical.

Encapsulation

As data moves down the stack at the sender, each layer adds a header of its own information to what it receives from the layer above. The application message M becomes the payload of a segment, with a transport header such as port numbers; the segment becomes the payload of a datagram, with a network header such as source and destination IP addresses; the datagram becomes the payload of a frame, with a link header and often a trailer for error checking. At each receiving node, the matching layer reads and removes its own header and passes the payload up.

Worked example: the cost of headers

An application message is sent over TCP (20-byte header), IPv4 (20-byte header) and Ethernet (14-byte header plus a 4-byte trailer). 1. Extra bytes per frame: \(20 + 20 + 14 + 4 = 58\). 2. A 100-byte message gives a \(158\)-byte frame, so headers are \(58 \div 158 \approx 36.7\%\) of the bytes sent. 3. A 1,460-byte message gives a 1,518-byte frame, and headers are \(58 \div 1{,}518 \approx 3.8\%\). 4. So small messages pay a large overhead, and bulk transfers a small one. Check: \(1{,}460 + 58 = 1{,}518\), the largest standard Ethernet frame not counting the preamble that precedes it on the wire.

Which devices run which layers

Hosts implement all five layers. Routers implement layers 1 to 3: they read the network-layer header to forward each datagram, so they never need to look inside the segment. Link-layer switches implement only layers 1 and 2 and forward frames using link-layer addresses. So the core runs a small, common set of layers, and everything above lives in hosts. Some devices, called middleboxes, break the pattern. Firewalls, network address translators and load balancers may read or rewrite transport or even application headers. The topic Middleboxes returns to them.

Worked example: one frame's journey

A laptop sends a request to a server through an Ethernet switch and then a router. 1. The laptop's stack builds a message, segment, datagram and frame, and sends the frame's bits. 2. The switch rebuilds the frame from the bits, reads the link header to choose an output port, and sends the same frame on; it never opens the datagram. 3. The router removes the frame, reads the datagram's destination address and looks it up, then wraps the datagram in a new frame for the next link. 4. The server's stack removes the frame, datagram and segment headers in turn and hands the message to the server program. Check: only the two hosts ever touched the segment or the message.

The costs of layering

Layering is not free. Every layer adds header bytes, as the worked example showed. Functions can be duplicated: error checking happens on each link and again end to end. And strict separation hides information that another layer could use; for example, a transport protocol may not know whether a loss was caused by congestion or by radio interference, and the right reaction differs. Real systems therefore sometimes cross layers deliberately, yet the layered model remains the main tool for designing networks and locating faults.

Common mistakes to avoid

First, mixing up the unit names: message at the application layer, segment at transport, datagram at network, frame at link. Second, thinking routers run TCP for the traffic passing through; they stop at the network layer. Third, confusing a service, what a layer offers, with its implementation, how it delivers it. Fourth, assuming OSI's session and presentation layers exist as separate parts of the internet stack. Fifth, believing a switch rewraps frames the way a router does; a switch forwards the frame itself.

Key takeaways

Layering divides networking into a stack in which each layer serves the one above using the one below, so implementations can change independently. The five-layer internet stack is application (messages; HTTP, SMTP, DNS), transport (segments; TCP, UDP), network (datagrams; IP and routing protocols), link (frames; Ethernet, Wi-Fi) and physical (bits on a medium). OSI (1984) has seven layers, adding presentation and session, and supplies the common layer numbers. Encapsulation adds a header at each layer on the way down and removes it on the way up. Hosts run all five layers, routers layers 1 to 3, switches layers 1 and 2. The costs are overhead, duplication and hidden information.

Flashcards

Flashcard

Layering

Each layer serves the one above using the one below

Implementations can change independently.

Test your understanding

Question 1 of 5

A 500-byte message travels over TCP (20-byte header), IPv4 (20 bytes) and Ethernet (14-byte header plus 4-byte trailer). How many bytes are in the frame?

Question 2 of 5

A router receives a frame carrying an IP datagram that holds a TCP segment. Which headers does it normally read to forward the packet?

Question 3 of 5

At which layer does the internet stack place the job of choosing a path across many networks, and what is its unit of data called?

Question 4 of 5

A company replaces its office Ethernet with Wi-Fi, and its web browsers keep working unchanged. Which property of layering does this show?

Question 5 of 5

Which pair of layers does the OSI model have that the five-layer internet stack does not?

← Previous topicNext topic →