A front-end developer reports an API call error.
The back-end developer checks the HTTP status code, the infrastructure engineer checks the TCP connection and firewall, and the network engineer looks at the IP path.
Each person is looking at a different level of the same "request failed."
Networks are divided into
layers by role
in exactly this way.
In this article, we explain why a layered architecture is needed and what
service model
each layer provides to the layer above.
We then observe on macOS the
encapsulation
process, in which headers are added and removed as data passes through the layers.
Each layer uses the services of the layer directly below it, adds its own functionality, and provides a service to the layer directly above it.
Consider an international business trip.
Buying a ticket, checking baggage, boarding, taking off, and flying happen in order, and at the destination, landing, deplaning, and baggage claim happen in reverse order.
The baggage step does not need to know which route the plane takes, and baggage handling stays the same even if the route changes.
A layered network architecture has the same advantages.
Modularity
The implementation of each layer can be changed independently. For example, switching the link from Ethernet to WiโFi leaves the web application code unchanged.
Managing complexity
A huge communication system can be designed and analyzed by dividing it into roles.
Clear fault isolation
When a problem occurs, you can narrow down which layer it belongs to.
๐
Layering also has drawbacks.
Multiple layers may duplicate similar functions, such as error detection, and when an upper layer needs information from a lower layer,
designs that break the principle of layer separation
The Internet protocol stack is described as five layers.
From the top, they are the application, transport, network, link, and physical layers.
The per-layer requirements that Internet hosts must meet are documented in RFC 1122โ.
Representative protocols and data units of the five Internet layers, and the encapsulation process in which one header is added at each lower layer
This is the layer where network applications and their protocols reside.
HTTP for the web, SMTP for email, and DNS, which translates domain names into IP addresses, belong here.
The unit of data exchanged at this layer is called a
carrying a segment from the source host to the destination host through routers.
IP
, which defines the datagram format and addressing scheme, is the core protocol, and the various routing protocols that determine paths also belong to this layer.
The network layer uses the link layer's services to pass a datagram to the next node on the path.
Ethernet and WiโFi are typical examples, and the unit of data is the
frame
.
A datagram may traverse different link-layer protocols on different segments on its way to the destination.
within a frame along the link to the next node.
Even for the same Ethernet, the physical-layer protocol differs depending on whether the medium is UTP, coaxial cable, or optical fiber.
In a layered architecture, the functionality each layer promises to the layer above is called its
service model
.
For example, an application using TCP can expect the service that "the bytes it sends arrive in order without any missing."
In contrast, IP below it provides only
best-effort delivery
, so datagrams may be lost, reordered, or duplicated.
In other words, TCP performs retransmission and reordering on top of unreliable IP to create a reliable service.
Adding functionality to the services of a lower layer to provide a better service to the layer above
The OSI reference model proposed by ISO divides network communication into seven layers (Cloudflare ยท What is the OSI Model?โ).
The five Internet layers have no separate OSI
presentation layer
or
session layer
.
The presentation layer handles data compression, encryption, and format conversion, while the session layer handles synchronization and checkpointing of data exchange.
On the Internet, applications implement these functions themselves when needed.
For example, HTTPS encryption is handled by a TLS library, and JSON serialization by application code.
When an application-layer message M is passed to the transport layer, the transport layer adds a header Ht containing port numbers and error-detection information to create a segment.
The network layer adds a header Hn containing the source and destination IP addresses to create a datagram, and the link layer adds a header Hl to create a frame.
This process is called
encapsulation
.
On the receiving side, the reverse happens:
decapsulation
, in which each layer, starting from the link layer, checks and removes its own header before passing the data up.
Because the last header added is the first one removed, this process works in the same order as a
handle up to the network layer.
Only hosts implement all five layers.
That is why a router decides paths by looking at the IP header but, in principle, does not interpret the TCP header or the contents of an HTTP message.
However, some devices, such as firewalls and load balancers,
inspect up to the transport or application layer
, so when analyzing a failure you need to check which layers the devices on the path handle.
๐จโ๐ป
When analyzing an API request error, checking layer by layer is fast.
In the curl -v output, first see whether the DNS lookup, TCP connection, and TLS handshake succeeded, then check the HTTP status code and response body.
With tcpdump, you can see the per-layer headers of packets actually being exchanged.
First, start a packet capture in one terminal.
sudo tcpdump -i en0 -nn -e -c 6 tcp port 443 and host www.promleeblog.com
Send a request from another terminal.
curl -s -o /dev/null https://www.promleeblog.com
-e
Also prints the source and destination MAC addresses from the link-layer header.
-nn
Shows IP addresses and port numbers as numbers instead of converting them to names.
-c 6
Exits after capturing six packets.
In one line of output, the MAC addresses are link-layer information, the IP addresses are network-layer information, and the port numbers and TCP flags such as Flags [S] are transport-layer information.
Because this is an HTTPS request, the HTTP message is encrypted with TLS and its contents are not visible.
It requires sudo privileges, and if you use a wired connection, specify that interface's name instead of en0.
The same tcpdump command works on Linux, and on Windows you can view the same information with Wireshark.
application, transport, network, link, and physical
, and each layer adds functionality to the services of the layer below to provide a service to the layer above.
Data undergoes
encapsulation
, gaining headers as it moves down the layers on the sending side, and decapsulation, shedding headers on the receiving side.
Routers and switches handle only the layers they need, so when a failure occurs, you should first identify which layer the problem belongs to.
This concludes the Network Intro series.
In a later series, we plan to cover the topmost layer from this article, the