How Switches Learn and Forward
Switches operate at Layer 2 (Data Link layer) and make forwarding decisions based on MAC addresses. When a frame arrives, the switch learns the source MAC address and the port it arrived on, adding or updating the MAC address table (also called CAM table). The switch then looks up the destination MAC: if found, it forwards the frame only to that port (unicast forwarding). If not found, it floods the frame to all ports except the incoming port (unknown unicast flooding).
Switch frame handling: known unicast → forward to specific port. Unknown unicast → flood all ports. Broadcast (FF:FF:FF:FF:FF:FF) → flood all ports. Multicast → flood all ports (unless multicast snooping is configured). MAC address table entries age out after a timeout (typically 300 seconds) if no frames are seen from that MAC.
VLANs and Trunking
VLANs (Virtual LANs) divide a single physical switch into multiple logical broadcast domains using IEEE 802.1Q tags. Devices in different VLANs cannot communicate without a router or Layer 3 switch. Access ports carry traffic for a single VLAN (untagged) — used for end devices. Trunk ports carry traffic for multiple VLANs (tagged with VLAN IDs) — used between switches and for router-on-a-stick configurations.
Native VLAN: the VLAN that traffic is placed into on a trunk port when no 802.1Q tag is present. Default is VLAN 1 — change it for security. VLAN ID range: 1–4094 (VLAN 1 default, VLANs 1002–1005 reserved for legacy). Inter-VLAN routing requires a Layer 3 device (router with subinterfaces or Layer 3 switch with SVIs).
Spanning Tree Protocol (STP)
STP (IEEE 802.1D) prevents Layer 2 loops in networks with redundant switch paths. Without STP, broadcast frames would loop endlessly, consuming all bandwidth (broadcast storm). STP elects a root bridge (lowest bridge ID = priority + MAC address) and puts redundant ports into blocking state. Only the root bridge paths remain forwarding.
STP port states: Blocking → Listening → Learning → Forwarding. Convergence takes ~30–50 seconds. RSTP (Rapid STP, 802.1w) converges in ~1–2 seconds — the modern standard. MSTP (802.1s) supports multiple spanning tree instances per VLAN group.
PortFast: STP feature for access ports that skips the listening/learning states, immediately transitioning to forwarding. Used for end-device ports to eliminate the 30-second wait. BPDU Guard: automatically disables a PortFast port if it receives STP BPDUs (Bridge Protocol Data Units) — prevents rogue switches from affecting the STP topology.