---
title: "Spanning Tree Protocol — STP / RSTP / PVST+"
domain: network
subdomain: vlan
type: snippet
tags: [stp, rstp, pvst, spanning-tree, cisco, portfast, bpdu-guard, loop]
difficulty: intermediate
status: stable
updated: "2025-05-14"
---
## Concepts fondamentaux

```
STP (802.1D) = prévient les boucles L2 en bloquant des ports redondants
RSTP (802.1w) = convergence rapide (<1s vs 30-50s pour STP classique)
PVST+ = Per-VLAN Spanning Tree (Cisco) — une instance STP par VLAN
Rapid PVST+ = RSTP + PVST+ (recommandé sur Cisco moderne)
MSTP (802.1s) = Multiple STP — regrouper plusieurs VLANs par instance

Élection Root Bridge :
  → Le switch avec le Bridge ID (priorité + MAC) le plus bas devient Root
  → Priorité par défaut = 32768 ; incréments de 4096
  → En pratique : configurer manuellement la priorité du root

Rôles des ports :
  Root Port (RP)       : meilleur chemin vers le Root Bridge (1 par switch)
  Designated Port (DP) : port actif sur chaque segment (forwarding)
  Blocked/Alternate    : port bloqué pour éviter les boucles
  Backup Port (RSTP)   : backup d'un Designated sur le même segment

États des ports STP classique : Disabled → Blocking → Listening → Learning → Forwarding
États RSTP : Discarding → Learning → Forwarding
```

## Configuration Cisco — STP de base

### Vérifier l'état STP actuel

```text
show spanning-tree
show spanning-tree vlan {{VLAN_ID}}
show spanning-tree summary
show spanning-tree detail
```

### Choisir le mode STP

```text
conf t
! Rapid PVST+ (recommandé — défaut sur IOS moderne)
spanning-tree mode rapid-pvst

! PVST+ classique (compatibilité anciens équipements)
spanning-tree mode pvst

! MSTP
spanning-tree mode mst
```

### Configurer le Root Bridge

```text
conf t
! Méthode 1 : forcer la priorité manuellement (multiple de 4096)
spanning-tree vlan 10 priority 4096       ! Root primaire
spanning-tree vlan 10 priority 8192       ! Root secondaire

! Méthode 2 : macro Cisco (calcule automatiquement la priorité optimale)
spanning-tree vlan 10 root primary
spanning-tree vlan 10 root secondary

! Pour plusieurs VLANs d'un coup
spanning-tree vlan 10,20,30 root primary
spanning-tree vlan 40,50,60 root secondary

! Vérifier
show spanning-tree vlan 10 | include Root
```

### Diameter et hello timers

```text
conf t
! Modifier les timers (seulement sur le Root Bridge !)
spanning-tree vlan 10 hello-time 2        ! défaut = 2s
spanning-tree vlan 10 forward-time 15     ! défaut = 15s (listening+learning)
spanning-tree vlan 10 max-age 20          ! défaut = 20s

! En Rapid PVST+, les timers sont négociés automatiquement — ne pas modifier
```

## PortFast

```text
! Activer sur un port access individuel (postes, imprimantes, serveurs)
! → passe directement en Forwarding sans attendre la convergence STP
conf t
interface GigabitEthernet0/1
 spanning-tree portfast

! Activer globalement sur TOUS les ports access (attention : pas sur les trunks)
conf t
spanning-tree portfast default

! Désactiver PortFast sur un trunk (si portfast default est activé globalement)
interface GigabitEthernet0/24
 spanning-tree portfast disable

! Vérifier
show spanning-tree interface GigabitEthernet0/1 portfast
show running-config | include portfast
```

## BPDU Guard

```text
! BPDU Guard : si un BPDU est reçu sur un port PortFast → err-disable immédiat
! Empêche qu'un switch non autorisé soit branché sur un port access

! Activer sur une interface spécifique
conf t
interface GigabitEthernet0/1
 spanning-tree bpduguard enable

! Activer globalement sur tous les ports PortFast
conf t
spanning-tree portfast bpduguard default

! Récupérer un port en err-disable
interface GigabitEthernet0/1
 shutdown
 no shutdown

! Récupération automatique (optionnel)
conf t
errdisable recovery cause bpduguard
errdisable recovery interval 300      ! secondes

! Vérifier les ports err-disabled
show interfaces status err-disabled
show errdisable recovery
```

## BPDU Filter

```text
! BPDU Filter : supprime l'envoi ET la réception de BPDUs
! ATTENTION : dangereux — peut créer des boucles. Utiliser avec précaution.

! Sur une interface (bloque totalement les BPDUs)
interface GigabitEthernet0/1
 spanning-tree bpdufilter enable

! Globalement (seulement sur ports PortFast — si BPDU reçu, PortFast désactivé)
spanning-tree portfast bpdufilter default
```

## Root Guard

```text
! Root Guard : empêche un port de devenir Root Port
! → si un BPDU supérieur est reçu, le port passe en root-inconsistent (bloqué)
! Placer sur les ports Designated vers les switchs "inférieurs" (accès, distribution)

conf t
interface GigabitEthernet0/24
 spanning-tree guard root

! Vérifier les ports inconsistants
show spanning-tree inconsistentports
show spanning-tree vlan {{VLAN_ID}} inconsistentports
```

## Loop Guard

```text
! Loop Guard : si un port Root/Alternate cesse de recevoir des BPDUs
! → passe en loop-inconsistent au lieu de Forwarding (évite la boucle)
! Complémentaire de Root Guard — activer sur les ports non-Designated

conf t
! Sur une interface
interface GigabitEthernet0/1
 spanning-tree guard loop

! Globalement
spanning-tree loopguard default

! Vérifier
show spanning-tree detail | include loop
```

## PVST+ — Équilibrage de charge par VLAN

```text
! Stratégie : SW-CORE-1 = Root pour VLANs pairs, SW-CORE-2 = Root pour VLANs impairs
! → utilise les deux liens trunk simultanément

! Sur SW-CORE-1
conf t
spanning-tree vlan 10,30,50 root primary
spanning-tree vlan 20,40,60 root secondary

! Sur SW-CORE-2
conf t
spanning-tree vlan 20,40,60 root primary
spanning-tree vlan 10,30,50 root secondary
```

## Vérifications complètes

```text
! Vue globale de toutes les instances STP
show spanning-tree summary

! Détail complet d'une instance
show spanning-tree vlan {{VLAN_ID}} detail

! État des ports (roles, states, cost)
show spanning-tree vlan {{VLAN_ID}}

! Ports en état inconsistant (Root Guard, Loop Guard)
show spanning-tree inconsistentports

! Compteurs BPDU sur une interface
show spanning-tree interface GigabitEthernet0/1 detail

! Debug (à utiliser avec précaution en prod)
debug spanning-tree events
debug spanning-tree bpdu
```

### Interprétation de `show spanning-tree`

```
VLAN0010
  Spanning tree enabled protocol rstp
  Root ID    Priority    4106          ← 4096 + VLAN ID
             Address     aabb.cc00.0100
             This bridge is the root   ← ce switch EST le root
             Hello Time   2 sec  Max Age 20 sec  Forward Delay 15 sec

  Bridge ID  Priority    4106
             Address     aabb.cc00.0100

Interface           Role Sts Cost      Prio.Nbr Type
------------------- ---- --- --------- -------- ----
Gi0/1               Desg FWD 4         128.1    P2p
Gi0/2               Desg FWD 4         128.2    P2p
Gi0/3               Root FWD 4         128.3    P2p     ← Root Port
Gi0/4               Altn BLK 4         128.4    P2p     ← Bloqué
```

<Tip>
Activer BPDU Guard sur tous les ports access avec `spanning-tree portfast bpduguard default` — c'est la protection la plus simple et la plus efficace contre une boucle L2 involontaire. Si quelqu'un branche un switch non managé sur un port access, le port passe immédiatement en err-disable au lieu de propager une boucle sur tout le réseau.
</Tip>
