Skip to content
Visual explainer 2h to learn 4 steps

Loops & Spanning Tree Protocol

Explain why switching loops are dangerous and how STP blocks redundant links.

By Invicly Tech, , in Computer Networking. About 11 minutes to read.

Topic 9 of 51

In short

Redundant links between switches protect against failures, but Ethernet frames have no TTL, so a loop lets broadcasts circle forever and melt the network. Spanning Tree Protocol elects a root bridge and blocks just enough ports to leave a loop-free tree. If an active link fails, a blocked port takes over.

Explained simply

Network designers like to connect switches with more than one path, so that one broken cable does not cut anyone off. But Ethernet has a weakness: frames carry no counter that stops them after a number of hops. If switches form a loop, a broadcast goes round and round forever.

Worse, every switch floods each broadcast out of all its ports, so copies multiply. Within seconds the links are full, switches' CPUs max out and the whole network stops working. This is called a broadcast storm.

Spanning Tree Protocol (STP) solves this automatically. The switches talk to each other, pick one switch as the centre (the root bridge) and work out the best path from every switch to it. Any extra link that would create a loop is put into a blocking state, where it carries no normal traffic.

The blocked links are not wasted. If an active link fails, STP notices and unblocks a backup path. The original STP took 30 to 50 seconds to do this. Rapid Spanning Tree (RSTP), used today, usually recovers in about a second.

Press Play on a diagram to walk through it one step at a time.

FlowHow STP builds a loop-free tree
  1. 1
    Exchange BPDUs

    Switches advertise their bridge IDs.

  2. 2
    Elect root bridge

    Lowest priority plus MAC wins.

  3. 3
    Pick root ports

    Each switch's best path to the root.

  4. 4
    Pick designated ports

    One forwarding port per segment.

  5. 5
    Block the rest

    Remaining ports discard to break loops.

CycleHow a loop turns into a broadcast storm
  1. 1
    One broadcast

    A host sends an ARP request

  2. 2
    Flooded

    Each switch copies it out of every port

  3. 3
    Comes back

    Copies return via the other path

  4. 4
    Multiplies

    No TTL, so copies never die

  5. 5
    Meltdown

    Links full, MAC tables flap, CPUs at 100%

  6. Back to step 1

In depth

Bridge IDs and the root election

Every switch has an 8-byte bridge ID: a 2-byte priority followed by its 6-byte MAC address. The default priority is 32768. On Cisco's per-VLAN spanning tree, the priority field also carries the VLAN number (the extended system ID), so a switch in VLAN 10 shows priority 32778. You can only set priority in steps of 4096.

At first, every switch claims to be root and sends BPDUs (bridge protocol data units) every 2 seconds. When a switch hears a lower bridge ID, it stops claiming and relays the better one. The lowest bridge ID wins: lowest priority first, and on a tie, lowest MAC address. If you leave everything at defaults, the oldest switch with the lowest MAC often wins, which may be a slow closet switch. Always set the root deliberately.

  • Bridge ID = priority (default 32768) + MAC
  • Priority in steps of 4096: 0, 4096, 8192 ... 61440
  • spanning-tree vlan 10 root primary sets a winning priority
  • Also set a secondary root for when the primary fails

Root ports, designated ports and path cost

Once the root is known, each other switch picks one root port: the port with the lowest total cost to the root. Each link adds its cost. With the original short costs, 10 Mbps = 100, 100 Mbps = 19, 1 Gbps = 4 and 10 Gbps = 2; newer long costs use 20,000 for 1 Gbps and 2,000 for 10 Gbps. Ties are broken by the lowest neighbour bridge ID, then the lowest neighbour port ID (port priority, default 128, plus port number).

On every link segment, exactly one port becomes designated: the one on the switch closest to the root, which forwards traffic onto that segment. All root bridge ports are designated. Any port that is neither root nor designated is blocked. The result is exactly one active path between any two switches.

MapPort roles in a three-switch triangle
SW1 root bridge (all ports designated)
  • SW2
    • Port to SW1: root port, cost 4
    • Port to SW3: designated (lower bridge ID)
  • SW3
    • Port to SW1: root port, cost 4
    • Port to SW2: blocked (alternate)

Classic STP states and timers

Original STP (802.1D) moves a port through states: blocking (listen to BPDUs only), listening (take part in the election, no learning), learning (learn MACs but do not forward), and forwarding. Three timers control it: hello 2 seconds, forward delay 15 seconds and max age 20 seconds.

A port coming up spends 15 seconds listening and 15 learning, so 30 seconds before it forwards. If a switch loses BPDUs from the root indirectly, it first waits up to 20 seconds of max age before reacting, which makes a total of 50 seconds. That is why plugging a laptop into a classic STP port used to leave it with no network for half a minute, long enough for DHCP to give up. PortFast fixes this on host ports by going straight to forwarding.

Flow802.1D port states
  1. Step 1
    Blocking

    Receives BPDUs only; up to 20 s max age

  2. Step 2
    Listening

    15 s: elects roles, no MAC learning

  3. Step 3
    Learning

    15 s: learns MACs, still not forwarding

  4. Step 4
    Forwarding

    Normal traffic flows

RSTP and the protections you should enable

Rapid Spanning Tree (802.1w, now part of 802.1D-2004 and 802.1Q) keeps the same election but reacts much faster. It has three port states: discarding, learning and forwarding. It adds two backup roles: an alternate port is a ready-made second path to the root, and a backup port is a spare link to the same segment. On point-to-point links, switches use a quick proposal and agreement handshake instead of timers, and a failed root port can switch to its alternate almost at once.

In production, add guards. Mark host ports as edge (PortFast) and enable BPDU guard, which shuts the port if a BPDU arrives, so a user's home switch cannot cause a loop. Root guard on downlinks stops a rogue switch from becoming root. Cisco's rapid-pvst and the standard MSTP (802.1s) are the common modes.

  • spanning-tree mode rapid-pvst
  • spanning-tree portfast on host ports
  • spanning-tree bpduguard enable on host ports
  • spanning-tree guard root on downlinks to access switches

Learn it in 4 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Explain broadcast storms from loops~30m
  2. 2
    Describe root bridge election~30m
  3. 3
    Identify root, designated and blocked ports~30m
  4. 4
    Compare STP with RSTP~30m

Key points

  • Without STP, a loop causes a broadcast storm within seconds.
  • Set the root bridge deliberately with a low priority on a core switch.
  • Classic STP converges in 30 to 50 seconds; RSTP in about a second.
  • Edge ports (PortFast) skip listening states for end hosts.

Key terms

Tap a card to flip it. 0/4 known

Examples

Working out the tree by hand

Three switches in a triangle, all links 1 Gbps (cost 4), all priority 32768. MACs: SW1 0011.0000.0001, SW2 0011.0000.0002, SW3 0011.0000.0003. Step 1: equal priorities, so the lowest MAC wins: SW1 is root, and both its ports are designated. Step 2: SW2's cost to root is 4 direct versus 8 via SW3, so its port to SW1 is the root port. The same applies to SW3. Step 3: on the SW2 to SW3 link, both switches have cost 4 to root, so the tie is broken by bridge ID: SW2 is lower, so its port is designated. Step 4: SW3's port on that link is blocked (alternate in RSTP).

Making the core switch root and protecting edge ports

The network uses defaults, and an old access switch has become root, so traffic takes odd paths. Step 1: switch to rapid PVST on all switches. Step 2: on the core, set root primary for the user VLANs, and on a second core set root secondary. Step 3: on every host port, enable PortFast and BPDU guard. Step 4: check show spanning-tree vlan 10. It should say This bridge is the root on the core, and the roles should match your design.

text
CORE1(config)# spanning-tree mode rapid-pvst
CORE1(config)# spanning-tree vlan 10,20 root primary
CORE2(config)# spanning-tree vlan 10,20 root secondary
ACC1(config)# interface range gi1/0/1 - 44
ACC1(config-if-range)# spanning-tree portfast
ACC1(config-if-range)# spanning-tree bpduguard enable
CORE1# show spanning-tree vlan 10

Quick reference

  • Root bridge = lowest bridge ID (priority, then MAC); default priority 32768
  • Root port: lowest cost to root; then lowest neighbour bridge ID; then port ID
  • Short costs: 100 Mbps = 19, 1 Gbps = 4, 10 Gbps = 2
  • 802.1D timers: hello 2 s, forward delay 15 s, max age 20 s (30 to 50 s convergence)
  • RSTP states: discarding, learning, forwarding; roles add alternate and backup
  • Host ports: spanning-tree portfast + spanning-tree bpduguard enable
  • show spanning-tree vlan 10 shows root ID, roles and states

Common mistakes

  • Leaving root election to chance, so an old edge switch becomes root.
  • Enabling PortFast on ports connected to other switches.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Two switches both have priority 32768. How is the root chosen between them?

Question 2

2With classic 802.1D defaults, how long does a new port take to reach forwarding?

Question 3

3What does BPDU guard do on a PortFast port?

Question 4

4In RSTP, what is an alternate port?

Question 5

5Why do Ethernet loops cause storms while IP routing loops eventually stop?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    In Packet Tracer, connect three switches in a triangle and find which switch is root and which port is blocked.

  2. Exercise 2medium

    Make SW2 the root for VLAN 10 using an explicit priority (not root primary) and confirm the change.

  3. Exercise 3hard

    SW3's root port uses a 100 Mbps link (cost 19) directly to the root. Its other path is two 1 Gbps hops through SW2. Which port becomes the root port, and how would you force the direct link instead?

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I turn off Spanning Tree to make my network faster?
No. With RSTP and PortFast on host ports there is almost no delay, and without STP a single wrong cable can take down the whole network.
What is the difference between STP, RSTP and MSTP?
STP (802.1D) is the original slow version. RSTP (802.1w) uses the same ideas but converges in about a second. MSTP (802.1s) runs RSTP for groups of VLANs, so different VLANs can use different paths.
Why is my port in err-disabled state?
Often BPDU guard or port security shut it. Remove the cause, then run shutdown and no shutdown on the interface, or configure errdisable recovery.
Do modern data centres still use STP?
Much less. Leaf-spine designs route at layer 3 between switches and use ECMP, so there are no layer 2 loops to block. STP is still standard in campus and office networks.
ShareXLinkedInWhatsApp