← Kỹ sư mạng← Network Engineer
Kỹ sư mạngNetwork Engineer19 Th7, 2026Jul 19, 202620 phút đọc16 min read

Mô hình mạng & Các tầngNetwork Models & Layers

Thuộc bộ kiến thức Network Engineer Roadmap.

Tổng quan

Network model (mô hình mạng) là một framework tham chiếu phân tầng, chia bài toán khổng lồ “đưa dữ liệu từ một chương trình trên máy này tới một chương trình trên máy khác, ở bất kỳ đâu trên Trái Đất” thành một chồng (stack) các tầng nhỏ hơn, có định nghĩa rõ ràng. Mỗi tầng (layer) chỉ có một trách nhiệm duy nhất, chỉ giao tiếp với tầng ngay trên và ngay dưới nó, và có thể được thiết kế, cài đặt cũng như suy luận một cách độc lập.

Có hai mô hình thống trị lĩnh vực này:

Tại sao lại cần phân tầng?

Note này chính là bản đồ tư duy mà phần còn lại của roadmap dựa vào. Một khi bạn có thể đặt bất kỳ protocol, thiết bị hay lỗi nào vào đúng tầng của nó, mọi thứ còn lại — core protocols, network devices, và IP addressing & subnetting — sẽ tự khớp vào chỗ.

Kiến thức nền tảng

Mô hình OSI 7 tầng

Câu ghi nhớ kinh điển (từ trên xuống) là “All People Seem To Need Data Processing” (Application → Physical), hoặc từ dưới lên “Please Do Not Throw Sausage Pizza Away”.

Mỗi tầng trao đổi một PDU (Protocol Data Unit) với tầng ngang hàng (peer) trên host phía xa. Tên của PDU thay đổi khi bạn đi xuống stack — cần nhớ kỹ cách gọi tên này vì kỹ sư dùng nó liên tục.

#Tầng (Layer)PDUChức năng cốt lõiVí dụ protocol / chuẩnVí dụ thiết bịLỗi thường gặp
7ApplicationDataGiao diện tới ứng dụng người dùng; định nghĩa ngữ nghĩa của việc trao đổiHTTP, HTTPS, DNS, SMTP, FTP, SSH, gRPCFirewall (L7/WAF), API gateway, proxyLỗi 404/500, bad request, cert hết hạn, sai Host header
6PresentationDataChuyển đổi, encoding, serialization, mã hóa, nénTLS/SSL, JPEG, ASCII/UTF-8, JSON/Protobuf, gzip(thường là thư viện phần mềm)Sai encoding, TLS handshake thất bại, hỏng charset
5SessionDataThiết lập, quản lý, đóng session/dialog giữa các ứng dụngNetBIOS, RPC, SOCKS, (TLS session resumption)(phần mềm)Session timeout, session half-open, auth token hết hạn
4TransportSegment (TCP) / Datagram (UDP)Giao nhận end-to-end, ports/multiplexing, độ tin cậy, flow & congestion controlTCP, UDP, QUIC (trên UDP), SCTPL4 load balancer, stateful firewall, NATPort đóng/bị filter, connection refused/reset, retransmit, vấn đề MTU/MSS
3NetworkPacketĐịa chỉ logic và routing giữa các mạngIP (IPv4/IPv6), ICMP, IPsec, OSPF, BGPRouter, L3 switch, firewallNo route to host, TTL exceeded, sai subnet/gateway, routing loop
2Data LinkFrameGiao nhận node-to-node trên cùng một link; địa chỉ MAC; phát hiện lỗiEthernet, Wi-Fi (802.11), PPP, ARP, VLAN (802.1Q), STPSwitch, bridge, NIC, wireless APMAC flapping, sai VLAN, duplex mismatch, lỗi CRC, STP loop
1PhysicalBit (symbol)Truyền các bit thô qua môi trường; điện áp, ánh sáng, sóng radio, sơ đồ chânEthernet PHY, RJ45, fiber, DSL, USB, Bluetooth radioCáp, hub, repeater, transceiver, modemĐứt cáp, không có link light, hỏng SFP, nhiễu EMI, sai pinout, port down

Một cách nhớ tiện: các tầng 1–4 là các tầng “thấp”/hạ tầng (đưa các bit tới đúng port trên đúng host một cách tin cậy), còn các tầng 5–7 là các tầng “cao”/ứng dụng (làm cho dữ liệu có ý nghĩa với phần mềm). Network engineer chủ yếu sống ở L1–L4; developer chủ yếu sống ở L5–L7.

Lưu ý về các tầng “phụ”. Trong thực tế, các tầng 5, 6 và 7 khá mờ nhòe — cùng một protocol (ví dụ HTTPS) trải cả presentation (TLS) lẫn application (HTTP) cùng lúc. Đây là một lý do khiến mô hình TCP/IP gộp chúng lại.

Mô hình TCP/IP và cách nó ánh xạ sang OSI

Mô hình TCP/IP là thứ Internet thật sự triển khai (các tầng của nó được định nghĩa trong RFC 1122). Nó có hai cách mô tả phổ biến — bản 4 tầng và bản 5 tầng (tách tầng dưới cùng ra, khớp với OSI hơn). Cả hai đều đúng; bản 5 tầng dễ dạy hơn vì nó tách phần cáp (Physical) khỏi phần đóng frame (Data Link).

TCP/IP (4 tầng)TCP/IP (5 tầng)Tương đương OSIVí dụ protocol
ApplicationApplication7 + 6 + 5 (App, Presentation, Session)HTTP, DNS, TLS, SSH, SMTP
TransportTransport4 (Transport)TCP, UDP, QUIC
InternetInternet (Network)3 (Network)IP, ICMP, IPsec
Link / Network AccessData Link2 (Data Link)Ethernet, ARP, Wi-Fi
Physical1 (Physical)Cáp, radio, fiber

Vì sao stack thực tế là TCP/IP chứ không phải OSI:

Vậy: dùng OSI để có từ vựng và để troubleshoot; hiểu rằng các bit trên dây tuân theo mô hình TCP/IP.

Encapsulation và decapsulation

Dữ liệu không “dịch chuyển tức thời” xuống stack — mỗi tầng bọc (wrap) PDU từ tầng trên bằng header của riêng nó (và đôi khi cả trailer), thêm vào thông tin điều khiển mà tầng ngang hàng cần. Việc bọc này là encapsulation khi đi xuống (gửi), và decapsulation — gỡ từng header — khi đi lên (nhận). Đây là khái niệm cơ học quan trọng nhất trong networking.

Gửi (trên → dưới, encapsulation):

Application:  [ Data ]                                        (ví dụ một HTTP request)
Transport:   [ TCP hdr | Data ]                    → SEGMENT  (thêm src/dst port, seq#, flags)
Network:     [ IP hdr | TCP hdr | Data ]           → PACKET   (thêm địa chỉ IP src/dst, TTL)
Data Link:   [ Eth hdr | IP hdr | TCP hdr | Data | Eth trailer ] → FRAME (thêm MAC src/dst, FCS/CRC)
Physical:    0101110010101110...                    → BITS     (mã hóa thành tín hiệu điện/quang/radio)

Nhận (dưới → trên, decapsulation): ngược lại y hệt. NIC biến tín hiệu về lại thành frame, kiểm tra FCS, gỡ Ethernet header/trailer → đẩy packet lên. Tầng IP kiểm tra IP đích và gỡ IP header → đẩy segment lên. TCP kiểm tra port, ráp lại/sắp xếp lại, gỡ header của nó → giao dữ liệu cho ứng dụng đang lắng nghe trên port đó.

Những mô hình tư duy quan trọng mà điều này mở ra:

Nhìn kỹ tầng Transport: TCP vs UDP

Layer 4 là nơi “các mạng” trở thành “các kết nối giữa các chương trình”. Có hai protocol thống trị.

TCP (Transmission Control Protocol) — connection-oriented, tin cậy, có thứ tự, dạng byte-stream.

UDP (User Datagram Protocol) — connectionless, không tin cậy, dạng message. Chỉ có port nguồn/đích, độ dài, và một checksum. Không handshake, không ACK, không sắp thứ tự, không congestion control. Nó là một lớp bọc mỏng trên IP: “fire and forget” (bắn rồi quên). Nếu cần độ tin cậy, bạn tự xây bên trên (đó chính xác là điều QUIC làm trên UDP).

Thuộc tínhTCPUDP
Kết nốiConnection-oriented (có handshake)Connectionless
Độ tin cậyĐảm bảo giao nhận (ACK + retransmit)Best-effort, không đảm bảo
Thứ tựGiao đúng thứ tựKhông sắp thứ tự
Flow / congestion controlKhông
Kích thước header20+ byte8 byte
Tốc độ / độ trễĐộ trễ cao hơn (setup + ACK)Độ trễ thấp hơn, ít overhead
Tên PDUSegmentDatagram
Broadcast/multicastKhông
Dùng điển hìnhHTTP(S), SSH, SMTP, kết nối DB, truyền fileDNS, DHCP, VoIP, video streaming, game, QUIC/HTTP-3

Nguyên tắc chung: dùng TCP khi độ chính xác quan trọng hơn tốc độ (tải một trang web, truyền một file, một truy vấn database — mất một byte là không chấp nhận được). Dùng UDP khi tính kịp thời quan trọng hơn sự hoàn hảo và bạn có thể chịu/tự xử lý việc mất gói (một gói thoại bị rớt còn hơn là bị trễ; một bản cập nhật trạng thái game bị rớt sẽ được bản kế tiếp thay thế). Lưu ý web hiện đại đang chuyển sang QUIC/HTTP-3, chạy các stream tin cậy, có thứ tự trên UDP để có được đảm bảo của TCP mà không bị head-of-line blocking và handshake chậm.

Khái niệm chính

Nói chuyện theo tầng: “L2 vs L3”, “L4 vs L7”

Lợi ích thực tế lớn nhất của các mô hình này là một bộ từ vựng chung để mô tả một thiết bị hay một quyết định vận hành ở đâu.

Thiết bị L2 vs L3 (xem network devices):

Middlebox L4 vs L7 — sự phân biệt này thống trị hạ tầng hiện đại:

L4 (Transport) load balancer / firewallL7 (Application) load balancer / firewall
Nhìn thấyĐịa chỉ IP + port TCP/UDPToàn bộ dữ liệu ứng dụng (URL, header, cookie, HTTP method)
Ra quyết định dựa trên5-tuple (src/dst IP, src/dst port, protocol)Hostname, path, header, cookie, nội dung request
Ví dụAWS NLB, HAProxy ở chế độ TCP, iptables, IPVSAWS ALB, Nginx/Envoy, API gateway, WAF
Chi phíNhanh, rẻ, không phụ thuộc protocolTốn CPU hơn (phải parse & thường phải terminate TLS)
Có thể làmRoute “toàn bộ traffic trên :443 tới pool này""Route /api/* chỗ này, /images/* chỗ kia; chặn mẫu SQL-injection”

Một firewall L4 quyết định “cho phép/từ chối dựa trên IP + port” (stateful firewall kinh điển). Một firewall L7 / WAF hiểu protocol ứng dụng và có thể chặn, ví dụ, một HTTP request độc hại dù port đang mở hợp lệ.

Khoanh vùng lỗi theo tầng (chiếc thang troubleshooting)

Vì stack được phân tầng, cách debug chuyên nghiệp là cô lập tầng đang hỏng từ dưới lên (hoặc dùng chia-để-trị). Một checklist kinh điển:

TầngCâu hỏiCông cụ / bằng chứng
L1 PhysicalCó link không? Cáp/SFP có tốt không?Link LED, ethtool, interface counters (CRC/error), đổi cáp
L2 Data LinkĐúng VLAN chưa? MAC đã học chưa? ARP có phân giải không?arp -a, MAC table của switch, ip neigh, kiểm tra VLAN tag
L3 NetworkCó tới được IP không? Route/gateway đúng chưa?ping, traceroute/tracert, ip route, kiểm tra subnet mask
L4 TransportPort có mở/listening/tới được không?telnet host port, nc -zv, ss -tlnp, tìm RST/SYN retransmit
L5–L7 AppApp có trả lời đúng không? TLS ổn không?curl -v, openssl s_client, log app, HTTP status code

Kỷ luật ở đây: đừng debug app (L7) khi cáp đang bị rút (L1). Câu đùa “It’s always DNS” tồn tại chính vì kỹ sư hay nhảy cóc qua các tầng. ping chạy nhưng curl lỗi → L3 ổn, hãy nhìn L4/L7. ping bằng IP mà không tới nhưng interface đang up → nhìn L2/L3 (routing, ARP, gateway).

Các tầng cao trong thực tế: session, presentation, application

Trong thế giới TCP/IP, ba tầng OSI này bị gộp thành “Application”, nhưng các mối quan tâm thì vẫn có thật và đáng gọi tên:

Một ví dụ cụ thể gắn kết mọi thứ lại — tải https://example.com:

  1. DNS (L7, trên UDP/L4) phân giải example.com → một IP.
  2. TCP (L4) 3-way handshake tới IP của server trên port 443.
  3. TLS (L6) handshake thương lượng mã hóa và xác thực certificate.
  4. HTTP (L7) request/response chảy bên trong đường hầm TLS đã mã hóa.
  5. Bên dưới tất cả, mọi byte đã được encapsulate thành TCP segment → IP packet → Ethernet/Wi-Fi frame → bits, được các router (L3) định tuyến từng hop và các switch (L2) chuyển mạch từng link.

Chỉ một URL đó đã vận dụng toàn bộ stack — và đó chính xác là lý do mô hình phân tầng là nền tảng mà mọi thứ khác xây lên trên.

Best Practices

Tài liệu tham khảo

Part of the Network Engineer Roadmap knowledge base.

Overview

A network model is a layered reference framework that breaks the enormous problem of “get data from one program on one machine to another program on another machine, anywhere on Earth” into a stack of smaller, well-defined layers. Each layer has a single responsibility, talks only to the layers directly above and below it, and can be designed, implemented, and reasoned about independently.

Two models dominate the field:

Why bother with layering at all?

This note is the mental map the rest of the roadmap hangs off. Once you can place any protocol, device, or failure on the correct layer, everything else — core protocols, network devices, and IP addressing & subnetting — clicks into place.

Fundamentals

The OSI 7-layer model

The classic mnemonic (top to bottom) is “All People Seem To Need Data Processing” (Application → Physical), or bottom to top “Please Do Not Throw Sausage Pizza Away”.

Each layer exchanges a PDU (Protocol Data Unit) with its peer layer on the remote host. The PDU name changes as you move down the stack — this naming is worth memorizing because engineers use it constantly.

#LayerPDUCore functionExample protocols / standardsExample devicesTypical failures
7ApplicationDataInterface to the end-user application; defines the semantics of the exchangeHTTP, HTTPS, DNS, SMTP, FTP, SSH, gRPCFirewalls (L7/WAF), API gateways, proxies404/500 errors, bad request, expired cert, wrong Host header
6PresentationDataTranslation, encoding, serialization, encryption, compressionTLS/SSL, JPEG, ASCII/UTF-8, JSON/Protobuf, gzip(usually software libraries)Encoding mismatch, TLS handshake failure, charset corruption
5SessionDataEstablishes, manages, and tears down sessions/dialogs between appsNetBIOS, RPC, SOCKS, (TLS session resumption)(software)Session timeout, half-open sessions, auth token expiry
4TransportSegment (TCP) / Datagram (UDP)End-to-end delivery, ports/multiplexing, reliability, flow & congestion controlTCP, UDP, QUIC (over UDP), SCTPL4 load balancers, stateful firewalls, NATPort closed/filtered, connection refused/reset, retransmits, MTU/MSS issues
3NetworkPacketLogical addressing and routing between networksIP (IPv4/IPv6), ICMP, IPsec, OSPF, BGPRouters, L3 switches, firewallsNo route to host, TTL exceeded, wrong subnet/gateway, routing loops
2Data LinkFrameNode-to-node delivery on the same link; MAC addressing; error detectionEthernet, Wi-Fi (802.11), PPP, ARP, VLAN (802.1Q), STPSwitches, bridges, NICs, wireless APsMAC flapping, VLAN mismatch, duplex mismatch, CRC errors, STP loops
1PhysicalBit (symbol)Transmission of raw bits over the medium; voltages, light, radio, pinoutsEthernet PHY, RJ45, fiber, DSL, USB, Bluetooth radioCables, hubs, repeaters, transceivers, modemsCable cut, no link light, bad SFP, EMI, wrong pinout, port down

A useful shorthand: layers 1–4 are the “lower”/plumbing layers (getting bits reliably between the right ports on the right hosts), while layers 5–7 are the “upper”/application layers (making the data meaningful to the software). Network engineers live mostly in L1–L4; developers live mostly in L5–L7.

A note on the “extra” layers. In real-world practice, layers 5, 6, and 7 are blurry — the same protocol (e.g. HTTPS) spans presentation (TLS) and application (HTTP) at once. This is one reason the TCP/IP model collapses them.

The TCP/IP model and how it maps to OSI

The TCP/IP model is what the Internet actually implements (its layers were defined in RFC 1122). It comes in two common depictions — a 4-layer version and a 5-layer version (which splits the bottom layer, matching OSI more closely). Both are correct; the 5-layer version is friendlier for teaching because it separates the cable (Physical) from the framing (Data Link).

TCP/IP (4-layer)TCP/IP (5-layer)OSI equivalentExample protocols
ApplicationApplication7 + 6 + 5 (App, Presentation, Session)HTTP, DNS, TLS, SSH, SMTP
TransportTransport4 (Transport)TCP, UDP, QUIC
InternetInternet (Network)3 (Network)IP, ICMP, IPsec
Link / Network AccessData Link2 (Data Link)Ethernet, ARP, Wi-Fi
Physical1 (Physical)Cables, radio, fiber

Why real stacks are TCP/IP, not OSI:

So: use OSI for vocabulary and troubleshooting; understand that the bits on the wire follow the TCP/IP model.

Encapsulation and decapsulation

Data doesn’t teleport down the stack — each layer wraps the PDU from the layer above with its own header (and sometimes a trailer), adding the control information that layer’s peer needs. This wrapping is encapsulation on the way down (send), and decapsulation — stripping each header off — on the way up (receive). This is the single most important mechanical concept in networking.

Sending (top → bottom, encapsulation):

Application:  [ Data ]                                        (e.g. an HTTP request)
Transport:   [ TCP hdr | Data ]                    → SEGMENT  (adds src/dst ports, seq#, flags)
Network:     [ IP hdr | TCP hdr | Data ]           → PACKET   (adds src/dst IP addresses, TTL)
Data Link:   [ Eth hdr | IP hdr | TCP hdr | Data | Eth trailer ] → FRAME (adds src/dst MAC, FCS/CRC)
Physical:    0101110010101110...                    → BITS     (encoded as electrical/optical/radio signals)

Receiving (bottom → top, decapsulation): the exact reverse. The NIC turns signals back into a frame, checks the FCS, and strips the Ethernet header/trailer → hands the packet up. The IP layer checks the destination IP and strips the IP header → hands the segment up. TCP checks the port, reassembles/reorders, strips its header → hands the data to the application listening on that port.

Key mental models this unlocks:

The transport layer up close: TCP vs UDP

Layer 4 is where “networks” become “connections between programs”. Two protocols dominate.

TCP (Transmission Control Protocol) — connection-oriented, reliable, ordered, byte-stream.

UDP (User Datagram Protocol) — connectionless, unreliable, message-oriented. Just source/destination ports, length, and a checksum. No handshake, no ACKs, no ordering, no congestion control. It’s a thin wrapper over IP: “fire and forget”. If you need reliability, you build it yourself on top (which is exactly what QUIC does over UDP).

PropertyTCPUDP
ConnectionConnection-oriented (handshake)Connectionless
ReliabilityGuaranteed delivery (ACK + retransmit)Best-effort, no guarantees
OrderingIn-order deliveryNo ordering
Flow / congestion controlYesNo
Header size20+ bytes8 bytes
Speed / latencyHigher latency (setup + ACKs)Lower latency, less overhead
PDU nameSegmentDatagram
Broadcast/multicastNoYes
Typical usesHTTP(S), SSH, SMTP, DB connections, file transferDNS, DHCP, VoIP, video streaming, gaming, QUIC/HTTP-3

Rule of thumb: use TCP when correctness matters more than speed (loading a web page, transferring a file, a database query — a lost byte is unacceptable). Use UDP when timeliness matters more than perfection and you can tolerate/handle loss yourself (a dropped voice packet is better than a delayed one; a dropped game state update is superseded by the next one). Note that modern web is shifting to QUIC/HTTP-3, which runs reliable, ordered streams over UDP to get TCP’s guarantees without its head-of-line blocking and slow handshake.

Key Concepts

Speaking in layers: “L2 vs L3”, “L4 vs L7”

The single most practical payoff of these models is a shared vocabulary for describing where a device or decision operates.

L2 vs L3 devices (see network devices):

L4 vs L7 middleboxes — this distinction dominates modern infrastructure:

L4 (Transport) load balancer / firewallL7 (Application) load balancer / firewall
SeesIP addresses + TCP/UDP portsFull application data (URLs, headers, cookies, HTTP methods)
Decisions based on5-tuple (src/dst IP, src/dst port, protocol)Hostname, path, header, cookie, request content
ExamplesAWS NLB, HAProxy in TCP mode, iptables, IPVSAWS ALB, Nginx/Envoy, API gateways, a WAF
CostFast, cheap, protocol-agnosticMore CPU (must parse & often terminate TLS)
Can doRoute “all traffic on :443 to this pool""Route /api/* here, /images/* there; block SQL-injection patterns”

An L4 firewall decides “allow/deny based on IP + port” (classic stateful firewall). An L7 firewall / WAF understands the application protocol and can block, say, a malicious HTTP request even though the port is legitimately open.

Localizing faults by layer (the troubleshooting ladder)

Because the stack is layered, the professional way to debug is to isolate the failing layer bottom-up (or use divide-and-conquer). A canonical checklist:

LayerQuestionTool / evidence
L1 PhysicalIs there link? Is the cable/SFP good?Link LED, ethtool, interface counters (CRC/errors), swap cable
L2 Data LinkRight VLAN? MAC learned? ARP resolving?arp -a, switch MAC table, ip neigh, check VLAN tags
L3 NetworkCan I reach the IP? Correct route/gateway?ping, traceroute/tracert, ip route, check subnet mask
L4 TransportIs the port open/listening/reachable?telnet host port, nc -zv, ss -tlnp, look for RST/SYN retransmits
L5–L7 AppDoes the app answer correctly? TLS ok?curl -v, openssl s_client, app logs, HTTP status codes

The discipline: don’t debug the app (L7) when the cable is unplugged (L1). “It’s always DNS” is a joke precisely because engineers skip layers. ping works but curl fails → L3 is fine, look at L4/L7. ping fails by IP but the interface is up → look at L2/L3 (routing, ARP, gateway).

The upper layers in practice: session, presentation, application

In the TCP/IP world these three OSI layers are collapsed into “Application”, but the concerns are still real and worth naming:

A concrete example tying it together — loading https://example.com:

  1. DNS (L7, over UDP/L4) resolves example.com → an IP.
  2. TCP (L4) 3-way handshake to the server’s IP on port 443.
  3. TLS (L6) handshake negotiates encryption and validates the certificate.
  4. HTTP (L7) request/response flows inside the encrypted TLS tunnel.
  5. Underneath all of it, every byte was encapsulated into TCP segments → IP packets → Ethernet/Wi-Fi frames → bits, routed hop-by-hop by routers (L3) and switched link-by-link by switches (L2).

That single URL exercises the entire stack — which is exactly why the layered model is the foundation everything else builds on.

Best Practices

References