30  Encapsulation & PDUs

Key Concepts
  • Encapsulation: The process of wrapping data from a higher layer with a header (and sometimes trailer) from the current layer before passing it down
  • PDU (Protocol Data Unit): The named unit of data at each network layer — frame (data link), packet (network), segment (transport), message (application)
  • Header: Control information prepended to the payload by each protocol layer, specifying addressing, length, sequence numbers, and error detection
  • Trailer: Control information appended after the payload, typically carrying error detection codes (CRC/FCS)
  • Decapsulation: The process of stripping headers and trailers as data moves up the protocol stack at the receiving device
  • MTU (Maximum Transmission Unit): The largest payload size a network link can carry in one frame; exceeding the MTU requires fragmentation
  • Data Link Frame: The PDU at layer 2; encapsulates an IP packet with MAC addresses, type field, and FCS trailer

30.1 In 60 Seconds

Encapsulation is the process of wrapping data in protocol headers as it moves down the network stack – each layer adds its own “envelope” containing addressing and control information. Data becomes a Segment (Transport), then a Packet (Network), then a Frame (Data Link), then Bits (Physical). This explains why small IoT messages become much larger on the wire and why protocols like 6LoWPAN exist to compress those headers back down.

30.2 Learning Objectives

By the end of this chapter, you will be able to:

  • Explain encapsulation: Describe how headers are added as data moves down the protocol stack
  • Explain decapsulation: Describe how headers are removed as data moves up the stack
  • Classify Protocol Data Units (PDUs): Identify the correct PDU name at each layer (Data, Segment, Packet, Frame, Bits)
  • Calculate packet overhead: Determine the byte overhead added by protocol headers for a given payload size
  • Compare protocol stacks: Evaluate the overhead efficiency of TCP/IP versus IoT-optimized stacks such as CoAP/6LoWPAN
  • Apply header compression rationale: Select appropriate compression techniques (6LoWPAN, SCHC) based on payload size and energy constraints
  • Construct an encapsulation trace: Follow an IoT message from application to physical transmission and back, identifying each PDU transformation
MVU: Minimum Viable Understanding

Core concept: Encapsulation is like putting a letter in envelopes – each layer adds its own “envelope” (header) around the data from the layer above. At the destination, each layer removes its envelope and passes the contents up.

Why it matters: Understanding encapsulation explains why small IoT messages become much larger on the wire (header overhead), why protocols like 6LoWPAN compress headers, and how to interpret packet captures in Wireshark.

Key takeaway: Data becomes Segment (Transport) becomes Packet (Network) becomes Frame (Data Link) becomes Bits (Physical). Each transformation adds addressing and control information.

30.3 Prerequisites

Just like a letter gets wrapped:

  1. Letter (your message) goes in an…
  2. Envelope (with sender/receiver address) goes in a…
  3. Mail bag (sorted by region) loaded on a…
  4. Truck/plane (physical transport)

Each layer adds its “envelope” (header) around the data from the layer above.

At the destination, each layer removes its envelope and passes the contents up.

“I just measured the temperature – it is 23.5 degrees!” announced Sammy the Sensor proudly. “But how do I get this tiny number all the way to the cloud server?”

Max the Microcontroller grinned. “Easy! Think of it like wrapping a present. First, I take your reading and put it in an envelope labeled with port numbers – that is the Transport layer making it a Segment. Then I put THAT envelope inside a bigger one with IP addresses – now it is a Packet. Then Lila wraps it again with MAC addresses to make a Frame.”

Lila the LED blinked excitedly. “And I hand the whole thing to the Wi-Fi radio, which turns it into actual radio Bits – ones and zeros flying through the air! Each wrapper adds information so the message knows where to go.”

“But doesn’t all that wrapping make my tiny 4-byte reading really big?” Sammy asked. Bella the Battery sighed, “Yes! Your little temperature reading can become 80 bytes or more with all those headers. That is why I care about encapsulation – every extra byte costs me energy to transmit. Protocols like 6LoWPAN were invented to squish those headers back down and save my power!”


30.4 How Encapsulation Works: Step-by-Step

Think of encapsulation like preparing a package for shipping. Each layer wraps everything it receives from the layer above, adding its own header with control information.

Diagram showing data being progressively wrapped with headers at each network layer: application data wrapped by transport header to form a segment, then by network header to form a packet, then by data link header and trailer to form a frame, and finally converted to bits for physical transmission

PDU encapsulation through the protocol stack
Figure 30.1

Step 1: Application creates data

  • You write “Hello!” (5 bytes)
  • Application Layer: This is your raw message

Step 2: Transport adds reliability info

  • TCP wraps your message with a header (20 bytes minimum)
  • Header contains: source port, destination port, sequence number, acknowledgment
  • Now you have a Segment: [TCP header][Hello!] = 25 bytes

Step 3: Network adds addressing

  • IP wraps the segment with routing information (20 bytes for IPv4)
  • Header contains: source IP address, destination IP address, TTL, protocol type
  • Now you have a Packet: [IP header][TCP header][Hello!] = 45 bytes

Step 4: Data Link adds local delivery info

  • Ethernet wraps the packet with MAC addresses and error checking (14-byte header + 4-byte trailer)
  • Header contains: destination MAC (6 bytes), source MAC (6 bytes), EtherType (2 bytes)
  • Trailer contains: Frame Check Sequence (4-byte CRC)
  • Now you have a Frame: [Eth header][IP header][TCP header][Hello!][FCS] = 63 bytes

Step 5: Physical transmits as bits

  • The frame is converted to electrical signals, light pulses, or radio waves
  • Now it is Bits: 63 bytes x 8 = 504 bits transmitted

At the destination, decapsulation reverses this process:

  • Physical receives bits and reconstructs Frame
  • Data Link checks FCS, strips Ethernet header and trailer, passes Packet up
  • Network checks IP address, strips IP header, passes Segment up
  • Transport checks sequence, strips TCP header, passes Data up
  • Application receives “Hello!” (original 5 bytes)

Key Insight: Your 5-byte message became 63 bytes on the wire – that is 92% overhead (only 8% of the transmitted data is your actual message). This is why IoT uses header compression.


30.5 Protocol Data Units (PDUs)

As data moves through the protocol stack, it receives different names reflecting its structure at each layer.

30.5.1 PDU Names by Layer

Layer PDU Name Contains
Application Data Raw application data (email, file, sensor reading)
Transport Segment (TCP) or Datagram (UDP) Data + port numbers + sequence/length info
Internet Packet Segment/datagram + source/destination IP addresses
Network Access Frame Packet + source/destination MAC addresses + error check
Physical Bits Electronic, radio, or optical signals
Protocol Data Units at each network layer: data at application, segment at transport, packet at network, frame at data link, and bits at physical layer
Figure 30.2: Protocol Data Units (PDUs) at each network layer showing frames, packets, segments, and data

30.5.2 Visualizing the Data Journey

When you send “Hello!” to a smart device, each layer wraps and unwraps the data:

SENDING (Your Phone)                RECEIVING (Smart Device)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
APP:    "Hello!"                    APP:    "Hello!"
           | add MQTT header                   ^ remove MQTT
TRANS:  [TCP][Hello!]               TRANS:  [TCP][Hello!]
           | add IP header                     ^ remove IP
NET:    [IP][TCP][Hello!]           NET:    [IP][TCP][Hello!]
           | add Wi-Fi header                  ^ remove Wi-Fi
LINK:   [Wi-Fi][IP][TCP][Hello!]    LINK:   [Wi-Fi][IP][TCP][Hello!]
           |                                   ^
PHYS:   ~~~radio waves~~~    >>>    PHYS:   ~~~radio waves~~~
Timeline diagram showing data being encapsulated layer by layer during transmission on the sending side, traveling across the physical medium, and being decapsulated layer by layer on the receiving side
Figure 30.3: Data journey through layers as a timeline showing encapsulation during sending and decapsulation during receiving


30.6 IoT Example: Temperature Sensor

A temperature sensor sends a reading to the cloud. This example uses a constrained IoT protocol stack with CoAP over UDP (not TCP) to minimize overhead.

Layer What Happens Protocol Used
Application Format: {"temp": 72} CoAP
Transport Add port numbers, length, checksum UDP
Network Add compressed IPv6 addresses 6LoWPAN
Data Link Add device MAC addresses 802.15.4 MAC
Physical Convert to radio signal 2.4 GHz radio

Application Layer Data:

{
  "sensor_id": "TEMP-001",
  "temperature": 22.5,
  "humidity": 45.0,
  "timestamp": "2025-10-25T10:30:00Z"
}

After each layer adds headers:

  1. Data: CoAP header (4 bytes) wraps the JSON payload
  2. Datagram: UDP header (8 bytes) adds source/destination port and checksum
  3. Packet: 6LoWPAN-compressed IPv6 header (~6 bytes) adds device and gateway addresses
  4. Frame: 802.15.4 MAC header + FCS (~25 bytes) adds hardware addresses and error check
  5. Bits: Transmitted as 2.4 GHz radio signals

Total journey: 5 layers down at the sensor, over the air, 5 layers up at the gateway, then through the internet to the cloud.


30.7 Encapsulation and Decapsulation

Encapsulation (sending): Headers and trailers are progressively added as data moves DOWN the layers. Each layer treats everything it receives from above as its payload.

Decapsulation (receiving): Headers and trailers are progressively removed as data moves UP the layers. Each layer strips its own header and passes the contents to the layer above.

Network encapsulation visualized through a postal system analogy: data is wrapped with headers at each layer during transmission (like placing a letter in an envelope, then a mail bag, then a delivery truck) and unwrapped at each layer during reception

Postal system analogy for network encapsulation
Figure 30.4: Network encapsulation visualized through a postal system analogy: data is wrapped with headers at each layer during transmission and unwrapped at each layer during reception.

Detailed encapsulation process diagram showing application data being wrapped by transport header to form a segment, then network header to form a packet, then data link header and trailer to form a frame, with the reverse decapsulation process shown on the receiving side

Encapsulation and decapsulation process
Figure 30.5

30.7.1 The Jacket Analogy

Sending an email – data moves through layers like putting on jackets:

Encapsulation shown as layers of jackets: each network layer adds a jacket (header) with specific control information, and the corresponding layer at the destination knows how to remove that jacket

Jacket analogy for encapsulation layers
Figure 30.6
  • Each layer puts a “jacket” on the data (with zippers/buttons representing protocol rules)
  • The equivalent layer at the destination knows how to remove that jacket
  • Only layers at the same level understand each other’s jackets

Headers contain control and management information:

  • Addressing (where to send)
  • Sequencing (order of packets)
  • Error checking (data integrity)
  • Protocol identification (what is inside)
Email message being encapsulated through network layers: the email text becomes data, wrapped by SMTP and TCP headers to form a segment, then IP header to form a packet, then Ethernet header and trailer to form a frame for transmission
Figure 30.7: Email transmission example showing encapsulation through network layers

30.8 Byte-Level Analysis: Understanding Overhead

30.8.1 Detailed Frame Structure

Byte-level breakdown of a complete Ethernet frame: 14-byte Ethernet header (6 bytes destination MAC, 6 bytes source MAC, 2 bytes EtherType), 20-byte IP header, 20-byte TCP header, variable payload, and 4-byte FCS trailer, totaling 58 bytes of overhead before any application data
Figure 30.8: Detailed byte-level structure of a network frame showing exact header sizes at each layer, revealing why IoT uses header compression

30.8.2 Header Overhead Calculation

Component Size Purpose
Ethernet Header 14 bytes Dest MAC (6) + Src MAC (6) + EtherType (2)
IP Header 20 bytes min Version, length, TTL, addresses, etc.
TCP Header 20 bytes min Ports, sequence numbers, flags, etc.
Frame Check Sequence 4 bytes Error detection (CRC-32)
Total Overhead 58 bytes Before any payload!

For IoT implications:

  • A 10-byte sensor reading becomes 68+ bytes on the wire
  • That is 85% overhead for small payloads (58 / 68 = 85.3%)
  • This is why IoT protocols like 6LoWPAN compress IPv6 headers from 40 bytes down to 2-6 bytes
Try It: Protocol Overhead Calculator

Explore how payload size and protocol choice affect transmission efficiency for IoT devices.


Worked Example: Protocol Overhead Comparison for a 20-Byte Sensor Reading

Scenario: A soil moisture sensor sends a 20-byte reading every 15 minutes. Compare the total bytes-on-wire for three protocol stacks.

Stack A: MQTT over TCP/IPv4 over Wi-Fi (802.11)

Layer Header Size Cumulative
Application MQTT PUBLISH (topic + QoS 0) 12 bytes 32 bytes
Transport TCP header 20 bytes 52 bytes
Network IPv4 header 20 bytes 72 bytes
Data Link Wi-Fi 802.11 header + FCS 36 bytes 108 bytes
Total on air 108 bytes Overhead: 81%

Stack B: CoAP over UDP/IPv6/6LoWPAN over 802.15.4 (Zigbee PHY)

Layer Header Size Cumulative
Application CoAP header (4 bytes) + URI option 8 bytes 28 bytes
Transport UDP header 8 bytes 36 bytes
Network 6LoWPAN compressed IPv6 6 bytes 42 bytes
Data Link 802.15.4 header + FCS 25 bytes 67 bytes
Total on air 67 bytes Overhead: 70%

Stack C: LoRaWAN (Class A, no IP layer)

Layer Header Size Cumulative
Application Payload only (no app header) 0 bytes 20 bytes
LoRaWAN MAC MAC header + MIC 13 bytes 33 bytes
Physical LoRa PHY header + CRC 12.25 bytes ~46 bytes
Total on air ~46 bytes Overhead: 57%

Approximate energy impact (assuming 40 mA TX current at 3.3 V, including typical radio wake-up costs):

Stack Bytes TX Time (approx) Energy per Message Notes
Wi-Fi 108 0.1 ms data + ~150 ms wake ~20 mJ Wake-up dominates energy cost
Zigbee 67 2.7 ms (250 kbps) ~0.35 mJ Fast wake-up, low overhead
LoRaWAN SF7 46 36 ms (5.5 kbps) ~4.8 mJ Long range but slow data rate

Key insight: Wi-Fi has the most overhead but the fastest data airtime – however, its high wake-up energy makes it the most expensive per message. LoRaWAN has the least protocol overhead but the longest airtime due to its low data rate. Zigbee provides the best energy efficiency for short-range indoor deployments. The 6LoWPAN header compression (40-byte IPv6 compressed to 6 bytes) saves 34 bytes per message – critical when the 802.15.4 PHY MTU is only 127 bytes.

30.10 Concept Relationships

Understanding how encapsulation concepts relate helps you optimize IoT protocol stacks:

Concept Depends On Affects Common Tradeoff
Encapsulation Layer architecture Total packet size, processing time More layers = more overhead
PDU Naming Layer position Documentation clarity, troubleshooting Different models use different names
Header Size Protocol features Bandwidth efficiency, battery life Rich features = larger headers
6LoWPAN Compression Context knowledge, stable addresses Payload ratio, processing cost Compression saves bandwidth but adds CPU cycles
Payload Ratio Header sizes, payload size Energy per useful byte Small payloads suffer most from overhead
MTU (Maximum Transmission Unit) Physical layer limits Fragmentation needs, efficiency Smaller MTU = more fragmentation

Key Insight: For a 10-byte sensor reading, standard TCP/IP overhead (58 bytes) means only 15% of transmitted data is useful. This is why IoT protocols like 6LoWPAN (compresses 40-byte IPv6 to 2-6 bytes) and CoAP (4-byte header vs HTTP’s 200+ bytes) are essential for battery-powered devices.

30.11 See Also

Explore related networking concepts:


Common Pitfalls

Encapsulation adds headers as data moves DOWN the stack at the sender. Decapsulation strips headers as data moves UP at the receiver. Fix: draw arrows showing both directions on a protocol stack diagram before answering any encapsulation question.

The IP layer at the receiver reads the IP header but passes the TCP segment inside unchanged to the transport layer. Fix: understand each layer’s scope: it adds/removes its own header and passes the rest intact.

Calling an IP packet a “frame” or a TCP segment a “packet” suggests a misunderstanding of layer boundaries. Fix: memorise the PDU name for each layer: frame (L2), packet (L3), segment (L4), message (L7).

30.12 Summary

This chapter covered how data is transformed as it moves through the protocol stack:

  • Encapsulation adds headers progressively as data moves DOWN the stack (Data to Segment to Packet to Frame to Bits), with each layer wrapping the previous layer’s output
  • Decapsulation removes headers progressively as data moves UP the stack, with each layer stripping its header and passing contents to the layer above
  • Protocol Data Units (PDUs) have specific names: Application data becomes Transport segment/datagram, then Network packet, then Data Link frame, finally Physical bits
  • Header overhead is significant: Standard TCP/IP over Ethernet adds 58 bytes to every message, meaning a 10-byte sensor reading is 85% overhead – this is why IoT uses header compression techniques like 6LoWPAN
  • Each header contains control information: addressing (where to send), sequencing (packet order), error checking (integrity), and protocol identification (what is inside)

30.13 Try It Yourself

Scenario: You are sending a 12-byte temperature/humidity reading ({temp:23.5,hum:65}) from a battery-powered sensor. Compare three protocol stacks.

Your Task: For each stack, calculate: 1. Total bytes on the wire (including all headers) 2. Goodput percentage (payload / total) 3. Which stack is most efficient

Stack A: HTTP POST with JSON over TCP/IPv4/Wi-Fi Stack B: CoAP with CBOR over UDP/IPv6/6LoWPAN/802.15.4 Stack C: MQTT over TCP/IPv4/Wi-Fi

Hint: Header sizes

Typical header sizes:

  • HTTP request line + headers: ~180 bytes
  • MQTT PUBLISH header: 2-12 bytes (variable)
  • CoAP header: 4 bytes
  • TCP header: 20 bytes
  • UDP header: 8 bytes
  • IPv4 header: 20 bytes
  • IPv6 header: 40 bytes (compressed by 6LoWPAN to 6-10 bytes)
  • Wi-Fi 802.11 header + FCS: 36 bytes
  • 802.15.4 header + FCS: 25 bytes
  • JSON encoding of payload: {"temp":23.5,"hum":65} = 23 bytes
  • CBOR encoding: ~14 bytes (binary)
Solution: Protocol Stack Comparison

Stack A: HTTP/JSON over TCP/IPv4/Wi-Fi

Layer Header/Data Size
Application (HTTP) POST /data HTTP/1.1, Host, Content-Type, Content-Length headers 180 bytes
Application (JSON payload) {"temp":23.5,"hum":65} 23 bytes
Transport (TCP) Source/dest ports, sequence, ACK 20 bytes
Network (IPv4) Source/dest IPs, TTL, protocol 20 bytes
Data Link (Wi-Fi) MAC addresses, control, FCS 36 bytes
Total 279 bytes

Goodput: 23 / 279 = 8.2%

Stack B: CoAP/CBOR over UDP/IPv6/6LoWPAN/802.15.4

Layer Header/Data Size
Application (CoAP) Version, type, token, code 4 bytes
Application (CBOR payload) Binary encoding 14 bytes
Transport (UDP) Source/dest ports, length, checksum 8 bytes
Network (IPv6 compressed) 6LoWPAN IPHC compression 7 bytes
Data Link (802.15.4) MAC header + FCS 25 bytes
Total 58 bytes

Goodput: 14 / 58 = 24.1%

Stack C: MQTT over TCP/IPv4/Wi-Fi

Layer Header/Data Size
Application (MQTT) PUBLISH + topic “sensors/data” 22 bytes
Application (JSON payload) {"temp":23.5,"hum":65} 23 bytes
Transport (TCP) Source/dest ports, sequence, ACK 20 bytes
Network (IPv4) Source/dest IPs, TTL, protocol 20 bytes
Data Link (Wi-Fi) MAC addresses, control, FCS 36 bytes
Total 121 bytes

Goodput: 23 / 121 = 19.0%

Comparison Summary:

Stack Total Bytes Goodput Efficiency Ranking
CoAP/CBOR/6LoWPAN/802.15.4 58 24.1% Best (4.8x less data than HTTP)
MQTT/TCP/IPv4/Wi-Fi 121 19.0% Better (2.3x less data than HTTP)
HTTP/JSON/TCP/IPv4/Wi-Fi 279 8.2% Worst (baseline)

Key Insights:

  1. Protocol choice matters: Stack B transmits 4.8x less data than Stack A for the same sensor reading
  2. Binary encoding helps: CBOR (14 bytes) vs JSON (23 bytes) saves 39% on payload alone
  3. Header compression is critical: 6LoWPAN reduces IPv6 from 40 to 7 bytes (82% savings)
  4. For battery-powered sensors: Stack B (CoAP/6LoWPAN) can provide roughly 5x longer battery life compared to Stack A (HTTP) due to reduced transmission

When to use each:

  • Stack A (HTTP): Mains-powered devices, web API compatibility required, developer familiarity prioritized
  • Stack B (CoAP/6LoWPAN): Battery-powered sensors, constrained networks, maximum efficiency needed
  • Stack C (MQTT): Moderate power budgets, pub/sub messaging patterns, QoS features required

30.14 What’s Next

Continue your exploration of layered network models and IoT protocol stacks:

Topic Chapter Description
IoT Reference Models IoT Reference Models Explore IoT-specific layered architectures including the ITU-T model and Cisco 7-Level reference model, and how they extend traditional networking
OSI and TCP/IP Models OSI and TCP/IP Models Deepen your understanding of the OSI 7-layer and TCP/IP 4-layer models that define where encapsulation occurs
Labs and Implementation Layered Models: Labs and Implementation Hands-on practice with MAC/IP addressing, ARP analysis, and Python socket programming to observe encapsulation in action
6LoWPAN Header Compression 6LoWPAN Fundamentals Study the IPHC compression scheme that reduces 40-byte IPv6 headers to 2-6 bytes for constrained IoT networks
CoAP Application Protocol CoAP Fundamentals Learn how CoAP’s 4-byte header and REST-over-UDP design minimizes application-layer overhead on constrained devices
Layered Models Review Layered Models: Review Test your understanding of encapsulation, PDU naming, and overhead calculations with comprehensive knowledge checks

30.15 Knowledge Check