VLANs, Trunks, and Spanning Tree — Switching Fundamentals
A VLAN is a broadcast domain. A trunk carries many of them. Spanning tree keeps the redundant ones from burning the building down. The three ideas that run every LAN.
The office building analogy
Think of your network as an office building:
- VLANs are the floors. Finance works on floor 10, engineering on floor 20, printers on floor 30. People on the same floor talk freely. To reach another floor, you go through the lobby — that's the router (or Layer 3 switch), the one device allowed to move traffic between VLANs.
- Trunks are the elevator shafts. One shaft carries people from many floors at once. Each person wears a badge showing their floor — that's the 802.1Q tag stamped on frames crossing a trunk.
- Spanning tree is the fire marshal. The building has two stairwells for safety (redundant links), but the marshal locks one — if both were open, a rumor (a broadcast frame) could circle the stairs forever, getting louder each lap. The locked stairwell opens automatically if the main one fails.
Why VLANs exist
Picture a 48-port switch with finance, engineering, and printers all plugged in. Without VLANs, every broadcast from one printer interrupts every host on the switch, and everyone's traffic mingles. A VLAN (virtual LAN) carves the switch into isolated broadcast domains (groups of devices that hear each other's broadcasts, and nobody else's). Same hardware, separate Layer 2 worlds.
Hosts in VLAN 10 talk to each other directly at Layer 2 (the switching layer, which forwards by MAC address). To reach VLAN 20, traffic must go up to Layer 3 — through a router or Layer 3 switch that forwards by IP address. That boundary is the point: isolation without buying more hardware.
Access ports and trunk ports
Switch ports play two roles:
Access ports and trunk ports
Switch ports play two roles:
| Port type | Purpose | How frames look |
|---|---|---|
| Access | One VLAN, to an end device (PC, printer, server) | Untagged — the host never sees VLANs |
| Trunk | Many VLANs, switch-to-switch or switch-to-router | Tagged with 802.1Q, except the native VLAN |
The 802.1Q tag is a 4-byte header inserted into the Ethernet frame: a 2-byte TPID (tag protocol identifier — the value 0x8100 announces "this frame is tagged") and a 2-byte TCI (tag control information) carrying the 12-bit VLAN ID (1–4094 usable) plus 3 priority bits used for QoS. Tags get stripped when frames exit an access port — hosts stay blissfully unaware that VLANs exist.
The native VLAN trap
Every trunk has one native VLAN whose traffic crosses untagged. If both ends of a link disagree about which VLAN that is, frames from VLAN A arrive at the other side in VLAN B. Silent misdelivery. It is notoriously hard to spot, because pings within a VLAN still work when a link fails over.
Reading a BPDU like a packet capture
Switches hold STP elections by mailing each other BPDUs (Bridge Protocol Data Units) — small multicast frames that say "here is who I think the root is, and how far away it is." Below is an illustrative RSTP BPDU, the way Wireshark would show it. Click any field to see what it means.
0000 00 00 02 02 3c 80 00 00 1b 21 3a 4c 5d 00 00 00 ....<....!:L]....
0010 04 80 00 00 1b 21 3a 4c 5d 7f 80 01 00 01 00 14 .....!:L].........
0020 00 02 00 0f 00 .....
| Offset | Field | Bytes (hex) |
|---|---|---|
| 0–1 | Protocol Identifier — always A magic number that says "this is spanning tree." Every STP, RSTP, and MSTP BPDU starts with it. | 00 00 |
| 2 | Protocol Version — | 02 |
| 3 | BPDU Type — | 02 |
| 4 | Flags — Each bit is a yes/no signal. | 3c |
| 5–12 | Root Identifier — who the sender believes is root8 bytes: a 2-byte priority (80 00 = 32768, the default) plus the root's 6-byte MAC address. Lowest combined value wins the election. | 80 00 00 1b 21 3a 4c 5d |
| 13–16 | Root Path Cost — The sender's total cost to reach the root. A cost of 4 = one 1-Gbps hop. | 00 00 00 04 |
| 17–24 | Bridge Identifier — the sender itselfSame format as the Root Identifier: priority + MAC. If this matches the Root Identifier, the sender believes it is the root. | 80 00 00 1b 21 3a 4c 5d 7f |
| 25–26 | Port Identifier — Port priority (128, default) + port number (1). The tiebreaker when two ports offer equal-cost paths. | 80 01 |
| 27–34 | Timers — message age, max age, hello, forward delayFour 2-byte values in 1/256-second units: Message Age, Max Age (20s), Hello Time (2s), Forward Delay (15s). | 00 01 00 14 00 02 00 0f |
Want to watch these on a real wire? Capture on a trunk port and filter stp — the Wireshark filter builder will build the exact display filter for you.
Why spanning tree exists
Redundant links are how networks survive failures. They are also loops — and a loop on a switch fabric is extinction-level. A broadcast frame circles forever, gets copied at every switch, and multiplies: that is a broadcast storm. MAC address tables flap between ports, CPU spikes, and the whole VLAN is effectively dead within seconds.
Spanning Tree Protocol (STP) solves it by electing a root bridge and computing a loop-free tree: every redundant link ends up forwarding or deliberately blocked. Blocked ports wake up when something fails — redundancy you can actually use.
Root election and port roles
Bridges elect the root by the lowest bridge ID — a combo of a configurable priority (default 32768, set in steps of 4096) plus the switch's MAC address as the tiebreaker. Then every other bridge picks its shortest path to the root: the port on that path is its root port, each cable segment elects one designated port, and everything else gets blocked.
"Shortest" uses cumulative path cost (modern values): 10 Mbps = 100, 100 Mbps = 19, 1 Gbps = 4, 10 Gbps = 2. Faster pipe, cheaper cost, more likely to be the chosen path.
Classic STP walks ports through Blocking → Listening → Learning → Forwarding with ~15-second timers — roughly 30–50 seconds of darkness after a topology change. Rapid STP (802.1w) converges in seconds by negotiating link by link, and adds alternate and backup port roles so failover is instant.
! pick your root and backup-root ON PURPOSE — lowest MAC wins otherwise
spanning-tree vlan 10 priority 4096
spanning-tree vlan 10 priority 8192 ! on the backup root
! edge ports: skip the 30s wait, die if anyone talks STP back
interface range Gi1/0/1 - 24
spanning-tree portfast
spanning-tree bpduguard enable
! never let a downstream switch become root
interface Gi1/0/25
spanning-tree guard root
Portfast tells the switch "this port leads to a PC, not another switch — skip the waiting and forward immediately." BPDU guard is the seatbelt: if a BPDU ever arrives on a portfast port, the port shuts down instead of risking a loop. Root guard refuses to let a downstream switch advertise a better root through that port.
The checklist that saves real LANs
- Place the root. If you did not configure priority, your root bridge is whoever has the lowest MAC — often a closet switch. Set it explicitly.
- Portfast only on edge ports. Loops downstream of portfast are unprotected — BPDU guard is the seatbelt.
- Verify after changes: show spanning-tree vlan 10 [brief|detail] — find "Root ID" and make sure it is who you expect.
- RSTP minimum in 2026. Run rapid per-VLAN or MSTP; nobody waits 50 seconds for convergence on purpose anymore.
The topology, drawn
Here is the building from the analogy: one switch per floor, VLANs 10/20/30 trunked between them, and a router on a stick doing the inter-VLAN routing. Sizing the subnets behind these VLANs is a two-minute job with the subnet calculator.
- VLANs divide one switch into separate broadcast domains — isolation without more hardware.
- 802.1Q trunks multiplex VLANs; native VLAN mismatch is a classic silent failure.
- STP elects a root bridge and blocks redundant links to prevent broadcast storms.
- Use RSTP edge ports and BPDU guard in real deployments.