交換機會有下面的問題
1,單點斷開
2,為了防止單點斷開,就要做冗余,所以就會產生環路,環路產生后,就會產生廣播風暴。
使用,交換機 STP(spanning-tree 生成數)協議 ,可以解決上面的問題。
cisco交換機默認是開啟STP功能的。
為了防止環路,在環上選擇一個接口,阻塞它。
如何選擇阻塞哪個端口呢,根據選舉規則。
交換機在廣播域發送類似hello包的東西,叫bpdu:bridge protocol data unit
bpdu報文里有bridge id,有優先級,有交換機的mac地址(這個mac地址,不是交換機接口上的mac地址,而是交換機主板上的mac地址)。
交換機的mac地址,實際就是一個,那么每個接口上的mac地址是如何來的呢?
假設交換機主板上的mac地址是ca00.f000.0000,那么接口f0/0的mac地址就是ca00.f000.0001;f0/1接口的mac地址就是ca00.f000.0002,以此類推。
先對比優先級,默認的優先級是32768,范圍是0-65535,數字越小越優先
如果優先級相同,再對比主板上的mac地址,mac地址越小越優先。
根據優先級和mac地址,選擇出一個最優先的叫root,也叫根橋。
選擇出root后,root每2秒發送bpdu,20秒超時。root發送出的bpdu里有root id和bridge id,它們是相同的。
別的交換機接收到了root id后,再向周圍的交換機轉發,但轉發前,把從root過來的bridge id,換成自己的,root id不變。
非root交換機,必須找出一個最優的端口,保持和root聯通,這個端口叫根端口。
如何評判是最優的端口呢?根據STA算法,主要看接口的帶寬。帶寬越寬,越容易成為根接口。

stp操作:

選擇出根端口和指定端口后,剩下的端口,就是要作為阻塞端口了,要阻塞它,不能通過它轉發流量,完美的解決了交換機的環路問題。
端口角色
-
DP:指定端口
發送bpdu
-
RP:根端口
不發送bpdu,只接受bpdu,能轉發流量
-
AP:決定出DP和RP后,剩下的端口,叫非指定端口(阻塞端口)
不發送bpdu,只接受bpdu,不能轉發流量
端口狀態
- block:初始狀態,會過度到listern
- listern:監聽bpdu,不能學習mac地址,不能轉發數據,停留15秒。任務是確認端口角色。非指定端口(阻塞端口的狀態從listern變成block。其他端口進入learn狀態。
- learn:可以學習mac地址,不能轉發數據。,停留15秒。進入forward狀態。
- forward:可以轉發數據了。根端口和指定端口就停留在此狀態了。
- disable:禁用端口的stp協議功能,不建議使用此狀態。有很多辦法能解決30秒過慢的問題。
bpdu報文種類:
- Configuration BPDU (CBPDU), used for Spanning Tree computation
- Topology Change Notification (TCN) BPDU, used to announce changes in the network topology
- Topology Change Notification Acknowledgment (TCA)
Linux中只定義了前兩種,最后一種是通過TCA flag置1發送config bpdu
環路解決了,那么單點失效的問題怎么解決呢?
當發生單點失效了,就讓原來阻塞的端口,不再阻塞,讓它轉發流量。
但是原來的阻塞接口,即使是不阻塞了,但是交換機是根據mac地址表轉發數據的,它沒有mac地址表的,還是不能實現轉發數據。怎么辦?
當檢測到某條鏈路單點失效后,離的最近的交換機的RP端口,會立即轉發TCN數據包,一層一層的向上轉發,直到根橋接受到了TCN,所以根橋就直到了哪條鏈路失效了。然后根橋就再發送一個CBPDU數據包,包里面的TC標識位會有值,其他交換機接收到了這個包后,馬上讓自己所以接口上的mac地址表的老化時間從300秒,變成15秒,15秒后,所有接口的mac地址表都被清空了,再有數據過來后,就泛洪,泛洪幾輪后,各個接口就學習到了mac,放到自己的mac地址表,然后網絡就又穩定了。
關鍵點總結:
- 打開阻塞的接口
- 檢知到單點失效的交換機從RP接口向上發送TCN報文,一層一層傳到根橋
- 根橋再發送一個CBPDU數據包,讓TC標識位有值
- 其他交換機發送TC位有值,調整mac地址表的老化時間
以上所說的內容都是STP標准,cisco還有專有的pvst
pvst:per vlan spanning tree。每一個vlan都運行一個生成樹。每個vlan里都是標准的STP。
交換機里是有vlan的,標准STP,阻塞接口時,不管這個接口屬於哪個vlan,一律阻塞。
pvst就可以精細到,雖然是通過一個接口,可以指定讓哪個vlan阻塞,讓哪個vlan不阻塞。
實驗:驗證pvst,誰是根橋,哪個接口被阻塞了。
拓撲圖:

1,查看所以vlan的生成樹
sw1:
sw1#show spanning-tree
VLAN0001
Spanning tree enabled protocol rstp
Root ID Priority 32769
Address aabb.cc00.0100
This bridge is the root
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 32769 (priority 32768 sys-id-ext 1)
Address aabb.cc00.0100
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300 sec
Interface Role Sts Cost Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Et0/0 Desg FWD 100 128.1 Shr
Et0/1 Desg FWD 100 128.2 Shr
Et0/2 Desg FWD 100 128.3 Shr
Et0/3 Desg FWD 100 128.4 Shr
Et1/0 Desg FWD 100 128.5 Shr
Et1/1 Desg FWD 100 128.6 Shr
Et1/2 Desg FWD 100 128.7 Shr
Et1/3 Desg FWD 100 128.8 Shr
--More--
上面說默認的bridge id是32768,這里為啥是32769呢,由於cisco用的是pvst,所有為了區分是哪個vlan的,在32768的基礎上,加 ,vlan號。這時的vlan號是1,所以32768+1=32769。
查看vlan的生成樹:show spanning-tree vlan 1
sw2:
sw2#show spanning-tree
VLAN0001
Spanning tree enabled protocol rstp
Root ID Priority 32769
Address aabb.cc00.0100
Cost 100
Port 1 (Ethernet0/0)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 32769 (priority 32768 sys-id-ext 1)
Address aabb.cc00.0200
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300 sec
Interface Role Sts Cost Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Et0/0 Root FWD 100 128.1 Shr
Et0/1 Desg FWD 100 128.2 Shr
Et0/2 Desg FWD 100 128.3 Shr
Et0/3 Desg FWD 100 128.4 Shr
Et1/0 Desg FWD 100 128.5 Shr
Et1/1 Desg FWD 100 128.6 Shr
Et1/2 Desg FWD 100 128.7 Shr
發送和sw1有下面的區別:
-
Cost 100
自己到根橋的開銷。
-
Port 1 (Ethernet0/0)
自己的哪個接口連接着根橋。這個端口一定是最優的,一定會出現在根端口的角色里。
確實它的角色是根端口:
Et0/0 Root FWD 100 128.1 Shr -
mac地址
我的mac地址aabb.cc00.0200,比根橋的mac地址一定要大,根的mac地址是aabb.cc00.0100,所以我在選舉的時候沒有成為根橋。
sw3:
sw3#show spanning-tree vlan 1
VLAN0001
Spanning tree enabled protocol rstp
Root ID Priority 32769
Address aabb.cc00.0100
Cost 100
Port 1 (Ethernet0/0)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 32769 (priority 32768 sys-id-ext 1)
Address aabb.cc00.0300
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300 sec
Interface Role Sts Cost Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Et0/0 Root FWD 100 128.1 Shr
Et0/1 Altn BLK 100 128.2 Shr
Et0/2 Desg FWD 100 128.3 Shr
Et0/3 Desg FWD 100 128.4 Shr
Et1/0 Desg FWD 100 128.5 Shr
Et1/1 Desg FWD 100 128.6 Shr
Et1/2 Desg FWD 100 128.7 Shr
發送和sw2有下面的區別:
-
自己的mac地址比sw2還大,是在選舉時,最不優先的。
-
自己有個端口是阻塞端口(AP),狀態是block。
Et0/1 Altn BLK 100 128.2 Shr
STP的配置點:指定誰是根橋。
指定原則:不能出現在2層網絡的接入層。
接入層是啥?看下圖。

IOU1和IOU2是接入層,它們不可以是根橋。
實驗:人為指定根橋。
拓撲圖:

1,手動指定sw3為根橋:
sw3(config)#spanning-tree vlan 1 root ?
primary Configure this switch as primary root for this spanning tree
secondary Configure switch as secondary root
在root后面可以輸入primary和secondary。
- primary:主根橋
- secondary:次根橋。當主根橋down后,不用經過選舉,次根橋自動變成主根橋。
sw3:成為了根橋。
sw3#show spanning-tree vlan 1
VLAN0001
Spanning tree enabled protocol rstp
Root ID Priority 24577
Address aabb.cc00.0300
This bridge is the root
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 24577 (priority 24576 sys-id-ext 1)
Address aabb.cc00.0300
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300 sec
Interface Role Sts Cost Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Et0/0 Desg FWD 100 128.1 Shr
Et0/1 Desg FWD 100 128.2 Shr
Et0/2 Desg FWD 100 128.3 Shr
Et0/3 Desg FWD 100 128.4 Shr
Et1/0 Desg FWD 100 128.5 Shr
Et1/1 Desg FWD 100 128.6 Shr
Et1/2 Desg FWD 100 128.7 Shr
Et1/3 Desg FWD 100 128.8 Shr
發現
Priority 24577:比默認值小了,所以比sw1和sw2都優先
sw1:沒有阻塞接口,mac地址比sw2小。
sw1#show spanning-tree vlan 1
VLAN0001
Spanning tree enabled protocol rstp
Root ID Priority 24577
Address aabb.cc00.0300
Cost 100
Port 2 (Ethernet0/1)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 32769 (priority 32768 sys-id-ext 1)
Address aabb.cc00.0100
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300 sec
Interface Role Sts Cost Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Et0/0 Desg FWD 100 128.1 Shr
Et0/1 Root FWD 100 128.2 Shr
Et0/2 Desg FWD 100 128.3 Shr
Et0/3 Desg FWD 100 128.4 Shr
Et1/0 Desg FWD 100 128.5 Shr
Et1/1 Desg FWD 100 128.6 Shr
Et1/2 Desg FWD 100 128.7 Shr
sw2:有阻塞接口。
sw2#show spanning-tree vlan 1
VLAN0001
Spanning tree enabled protocol rstp
Root ID Priority 24577
Address aabb.cc00.0300
Cost 100
Port 2 (Ethernet0/1)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 32769 (priority 32768 sys-id-ext 1)
Address aabb.cc00.0200
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300 sec
Interface Role Sts Cost Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Et0/0 Altn BLK 100 128.1 Shr
Et0/1 Root FWD 100 128.2 Shr
Et0/2 Desg FWD 100 128.3 Shr
Et0/3 Desg FWD 100 128.4 Shr
Et1/0 Desg FWD 100 128.5 Shr
Et1/1 Desg FWD 100 128.6 Shr
Et1/2 Desg FWD 100 128.7 Shr
2,通過指定交換機的優先級,來間接的讓誰成為根橋
sw1(config)#spanning-tree vlan 1 priority ?
<0-61440> bridge priority in increments of 4096
優先級必須是4096的整數倍。
sw1(config)#spanning-tree vlan 1 priority 1
% Bridge Priority must be in increments of 4096.
% Allowed values are:
0 4096 8192 12288 16384 20480 24576 28672
32768 36864 40960 45056 49152 53248 57344 61440
sw1(config)#spanning-tree vlan 1 priority 0
當輸入的數字不是4096的整數倍,會提示錯誤,並提供最接近的數字。
sw1:由於sw1的優先級是1,比sw2和sw3都高,所以成為了根橋。
sw1#show spanning-tree vlan 1
VLAN0001
Spanning tree enabled protocol rstp
Root ID Priority 1
Address aabb.cc00.0100
This bridge is the root
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 1 (priority 0 sys-id-ext 1)
Address aabb.cc00.0100
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300 sec
Interface Role Sts Cost Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Et0/0 Desg FWD 100 128.1 Shr
Et0/1 Desg FWD 100 128.2 Shr
Et0/2 Desg FWD 100 128.3 Shr
Et0/3 Desg FWD 100 128.4 Shr
sw2:優先級比sw3小,所以有阻塞接口
sw2#show spanning-tree vlan 1
VLAN0001
Spanning tree enabled protocol rstp
Root ID Priority 1
Address aabb.cc00.0100
Cost 100
Port 1 (Ethernet0/0)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 32769 (priority 32768 sys-id-ext 1)
Address aabb.cc00.0200
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300 sec
Interface Role Sts Cost Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Et0/0 Root FWD 100 128.1 Shr
Et0/1 Altn BLK 100 128.2 Shr
Et0/2 Desg FWD 100 128.3 Shr
Et0/3 Desg FWD 100 128.4 Shr
sw3:沒有阻塞接口。
sw3#show spanning-tree vlan 1
VLAN0001
Spanning tree enabled protocol rstp
Root ID Priority 1
Address aabb.cc00.0100
Cost 100
Port 1 (Ethernet0/0)
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Bridge ID Priority 24577 (priority 24576 sys-id-ext 1)
Address aabb.cc00.0300
Hello Time 2 sec Max Age 20 sec Forward Delay 15 sec
Aging Time 300 sec
Interface Role Sts Cost Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Et0/0 Root FWD 100 128.1 Shr
Et0/1 Desg FWD 100 128.2 Shr
Et0/2 Desg FWD 100 128.3 Shr
Et0/3 Desg FWD 100 128.4 Shr
Et1/0 Desg FWD 100 128.5 Shr
Et1/1 Desg FWD 100 128.6 Shr
Et1/2 Desg FWD 100 128.7 Shr
開啟STP功能
sw1(config)#spanning-tree mode ?
mst Multiple spanning tree mode
pvst Per-Vlan spanning tree mode
rapid-pvst Per-Vlan rapid spanning tree mode
# c/c++ 學習互助QQ群:877684253  # 本人微信:xiaoshitou5854
