OSPF(Open Shortest Path First,開放式最短路徑優先)是基於鏈路狀態的內部網關協議。OSPF具備收斂快、路由無環、擴展性好等優勢。鏈路狀態算法路由協議互相通告的是鏈路狀態信息,每臺路由器都將本身的鏈路狀態信息(包含接口的IP地址和子網掩碼、網絡類型、該鏈路的開銷等)發送給其餘路由器,並在網絡中泛洪,當每臺路由器收集到網絡內全部鏈路狀態信息後,就能擁有整個網絡的拓撲狀況,而後根據整網拓撲狀況運行SPF算法,得出全部網段的最短路徑。算法
R1網絡
interface Ethernet0/0/0 ip address 10.0.10.254 255.255.255.0 interface GigabitEthernet0/0/0 ip address 10.1.1.1 255.255.255.252 interface GigabitEthernet0/0/2 ip address 10.1.1.5 255.255.255.252
R2ide
interface Ethernet0/0/0 ip address 10.0.20.254 255.255.255.0 interface GigabitEthernet0/0/0 ip address 10.1.1.2 255.255.255.252 interface GigabitEthernet0/0/1 ip address 10.1.1.9 255.255.255.252
R3oop
interface Ethernet0/0/0 ip address 10.0.30.254 255.255.255.0 interface GigabitEthernet0/0/1 ip address 10.1.1.10 255.255.255.252 interface GigabitEthernet0/0/2 ip address 10.1.1.6 255.255.255.252
在三個路由器的區域0上宣告本身的直連網段,宣告需寫反轉掩碼加密
[r1]ospf 1 [r1-ospf-1]area 0 [r1-ospf-1-area-0.0.0.0]network 10.0.10.0 0.0.0.255 [r1-ospf-1-area-0.0.0.0]network 10.1.1.0 0.0.0.3 [r1-ospf-1-area-0.0.0.0]network 10.1.1.4 0.0.0.3 [r2]ospf [r2-ospf-1]ar 0 [r2-ospf-1-area-0.0.0.0]network 10.0.20.0 0.0.0.255 [r2-ospf-1-area-0.0.0.0]network 10.1.1.0 0.0.0.3 [r2-ospf-1-area-0.0.0.0]network 10.1.1.8 0.0.0.3 [r3]ospf [r3-ospf-1]ar 0 [r3-ospf-1-area-0.0.0.0]network 10.0.30.0 0.0.0.255 [r3-ospf-1-area-0.0.0.0]network 10.1.1.4 0.0.0.3 [r3-ospf-1-area-0.0.0.0]network 10.1.1.8 0.0.0.3
[r1]display ospf interface OSPF Process 1 with Router ID 10.0.10.254 Interfaces Area: 0.0.0.0 (MPLS TE not enabled) IP Address Type State Cost Pri DR BDR 10.0.10.254 Broadcast DR 1 1 10.0.10.254 0.0.0.0 10.1.1.1 Broadcast DR 1 1 10.1.1.1 10.1.1.2 10.1.1.5 Broadcast DR 1 1 10.1.1.5 10.1.1.6
[r1]display ospf peer OSPF Process 1 with Router ID 10.0.10.254 Neighbors Area 0.0.0.0 interface 10.1.1.1(GigabitEthernet0/0/0)'s neighbors Router ID: 10.0.20.254 Address: 10.1.1.2 State: Full Mode:Nbr is Master Priority: 1 DR: 10.1.1.1 BDR: 10.1.1.2 MTU: 0 Dead timer due in 35 sec Retrans timer interval: 5 Neighbor is up for 00:05:08 Authentication Sequence: [ 0 ] Neighbors Area 0.0.0.0 interface 10.1.1.5(GigabitEthernet0/0/2)'s neighbors Router ID: 10.0.30.254 Address: 10.1.1.6 State: Full Mode:Nbr is Master Priority: 1 DR: 10.1.1.5 BDR: 10.1.1.6 MTU: 0 Dead timer due in 38 sec Retrans timer interval: 5 Neighbor is up for 00:02:45 Authentication Sequence: [ 0 ]
[r1]dis ip routing-table protocol ospf Route Flags: R - relay, D - download to fib ------------------------------------------------------------------------------ Public routing table : OSPF Destinations : 3 Routes : 4 OSPF routing table status : <Active> Destinations : 3 Routes : 4 Destination/Mask Proto Pre Cost Flags NextHop Interface 10.0.20.0/24 OSPF 10 2 D 10.1.1.2 GigabitEthernet 0/0/0 10.0.30.0/24 OSPF 10 2 D 10.1.1.6 GigabitEthernet 0/0/2 10.1.1.8/30 OSPF 10 2 D 10.1.1.2 GigabitEthernet 0/0/0 OSPF 10 2 D 10.1.1.6 GigabitEthernet 0/0/2 OSPF routing table status : <Inactive> Destinations : 0 Routes : 0
OSPF協議能夠將整個自治系統化爲不一樣的區域(Area)。鏈路狀態信息只在區域內部泛洪,區域之間傳遞的只是路由條目而非鏈路狀態信息;區域0是骨幹區域,骨幹區域負責在非骨幹區域之間發佈區域間的路由信息。在一個OSPF區域中有且只有一個骨幹區域,非骨幹區域必須和骨幹區域相連,且非骨幹區域之間不能直接進行路由信息交互。
code
Al-backbone與As-backbone爲區域邊界路由器router
Al-backboneblog
vlan batch 901 to 902 interface Vlanif901 description to_al-core1 ip address 10.1.1.2 255.255.255.252 interface Vlanif902 description to_as-backbone ip address 10.0.1.2 255.255.255.252 interface GigabitEthernet0/0/1 description to_al-core1 port link-type trunk port trunk allow-pass vlan 2 to 4094 interface GigabitEthernet0/0/2 description to_as-backbone port link-type trunk port trunk allow-pass vlan 2 to 4094 ospf 1 area 0.0.0.0 network 10.0.1.0 0.0.0.3 description to_as-backbone area 0.0.0.1 network 10.1.1.0 0.0.0.3 description to_al-core1
As-backbone接口
vlan batch 902 to 903 interface Vlanif902 description to_al-backbone ip address 10.0.1.1 255.255.255.252 interface Vlanif903 description to_as-core1 ip address 10.2.1.1 255.255.255.252 interface GigabitEthernet0/0/1 description to_as-core1 port link-type trunk port trunk allow-pass vlan 2 to 4094 interface GigabitEthernet0/0/2 description to_al-backbone port link-type trunk port trunk allow-pass vlan 2 to 4094 ospf 1 area 0.0.0.0 network 10.0.1.0 0.0.0.3 description to_al-backbone area 0.0.0.2 network 10.2.1.0 0.0.0.3 description to_as-core1
Al-core1ip
vlan batch 4 901 interface Vlanif4 description to_pc1 ip address 10.1.4.254 255.255.255.0 interface Vlanif901 description to_al-backbone ip address 10.1.1.1 255.255.255.252 interface GigabitEthernet0/0/1 description to_al-backbone port link-type trunk port trunk allow-pass vlan 2 to 4094 interface GigabitEthernet0/0/2 description to_pc1 port link-type access port default vlan 4 ospf 1 area 0.0.0.1 network 10.1.1.0 0.0.0.3 description to_al-backbone network 10.1.4.0 0.0.0.255 description to_pc1
As-core1
vlan batch 4 903 interface Vlanif4 description to_pc2 ip address 10.2.4.254 255.255.255.0 interface Vlanif903 description to_as-backbone ip address 10.2.1.2 255.255.255.252 interface GigabitEthernet0/0/1 description to_as-backbone port link-type trunk port trunk allow-pass vlan 2 to 4094 interface GigabitEthernet0/0/2 description to_pc2 port link-type access port default vlan 4 ospf 1 area 0.0.0.2 network 10.2.1.0 0.0.0.3 description to_as-backbone network 10.2.4.0 0.0.0.255 description to_pc1
<al-backbone>dis ip routing-table Route Flags: R - relay, D - download to fib ------------------------------------------------------------------------------ Routing Tables: Public Destinations : 9 Routes : 9 Destination/Mask Proto Pre Cost Flags NextHop Interface 10.0.1.0/30 Direct 0 0 D 10.0.1.2 Vlanif902 10.0.1.2/32 Direct 0 0 D 127.0.0.1 Vlanif902 10.1.1.0/30 Direct 0 0 D 10.1.1.2 Vlanif901 10.1.1.2/32 Direct 0 0 D 127.0.0.1 Vlanif901 10.1.4.0/24 OSPF 10 2 D 10.1.1.1 Vlanif901 10.2.1.0/30 OSPF 10 2 D 10.0.1.1 Vlanif902 10.2.4.0/24 OSPF 10 3 D 10.0.1.1 Vlanif902 127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0 127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
OSPF報文驗證功能,經過驗證的報文才能接受
1. 區域認證:區域內全部路由器(三層交換機)的認證模式和口令必須一致 2. 鏈路認證:針對某個鄰居設置單獨的認證模式和密碼。同時配置了兩種,則鏈路認證優先
1. 簡單驗證模式:明文傳輸 2. MD5驗證模式:md5加密傳輸 3. Key chain驗證模式:同時配置多個密鑰,不一樣密鑰單獨設置生效週期
Al-backbone區域1配置 [al-backbone-ospf-1-area-0.0.0.1]authentication-mode simple plain 111111 Al-core1區域1配置 [al-core1-ospf-1-area-0.0.0.1]authentication-mode simple plain 111111
As-backbone區域2配置 [as-backbone-ospf-1-area-0.0.0.2]authentication-mode simple cipher 111111 As-core1區域2配置 [as-backbone-ospf-1-area-0.0.0.2]authentication-mode simple cipher 111111
Al-backbone區域1配置 [al-backbone-ospf-1-area-0.0.0.1]undo authentication-mode [al-backbone-ospf-1-area-0.0.0.1]authentication-mode md5 1 huawei
標識符爲1,配置口令爲huawei
Al-core1區域1配置 [al-core1-ospf-1-area-0.0.0.1]undo authentication-mode [al-core1-ospf-1-area-0.0.0.1]authentication-mode md5 1 huawei
[Huawei]interface GigabitEthernet0/0/1 三層交換機需先清除以太網接口二層配置並開啓三層功能 [Huawei-GigabitEthernet0/0/1]undo portswitch [Huawei-GigabitEthernet0/0/1]ospf authentication-mode md5 1 huawei
被動接口也稱抑制接口,成爲被動接口後,將不會接收和發送OSPF報文。通常配置鏈接pc的接口爲被動接口
[al-core1-ospf-1]silent-interface GigabitEthernet 0/0/2
OSPF使用Router-ID做爲路由器的身份標識,若是在啓動ospf時沒有指定Router-ID,則OSPF沒法正常啓動。例如,路由器(三層交換機)上未配置任何ip地址。
Router-ID命令配置 --> 最大的loopback接口ip地址 --> 最大的其餘接口ip地址(不考慮up/down狀況)
只有配選爲Router-ID的接口ip地址被刪除/修改,才觸發從新選擇過程。Router-ID改變以後,需手動reset ospf協議。
[al-backbone]router id 1.1.1.1 <al-backbone>reset ospf process
<al-backbone>dis router id RouterID:1.1.1.1
DR(指定路由器):全部的路由器都只將各自的鏈路狀態信息發送給DR,再由DR以組播方式發送至全部路由器。
BDR(備份指定路由器):當DR因爲故障失效時,BDR成爲DR,並在選擇新的BDR路由器。
DR Other(其餘路由器):非DR與BDR的路由器
比較優先級,優先級高的爲DR,次高的爲BDR
比較Router-ID,數值高的爲DR,次高的爲BDR
Notes:DR選舉針對接口,DR選舉是非搶佔的。
三層交換機需先清除以太網接口二層配置並開啓三層功能
[Huawei-GigabitEthernet0/0/1]undo portswitch [Huawei-GigabitEthernet0/0/1]ospf dr-priority 10
改變優先級後,能夠利用下面兩種方法從新進行DR/BDR的選擇,可是這會致使路由器(三層交換機)之間的OSPF鄰接關係中斷,通常狀況下不推薦使用
當路由器或交換機上同時存在多種路由協議的時候,系統爲每一種路由協議設置了不一樣的默認優先級,當在不一樣協議中發現同一條路由時,協議優先級高的將被優選。
[al-backbone]ospf [al-backbone-ospf-1]preference 200
ospf接口的開銷值若是沒配置,ospf會根據該接口的帶寬自動計算其開銷值。計算公式爲:接口開銷=帶寬參考值/接口帶寬,取計算結果的整數部分做爲接口開銷值,經過改變帶寬參考值能夠間接改變接口的開銷值。
三層交換機需先清除以太網接口二層配置並開啓三層功能
[Huawei-GigabitEthernet0/0/1]undo portswitch [Huawei-GigabitEthernet0/0/1]ospf cost 1000
[al-backbone]ospf [al-backbone-ospf-1]bandwidth-reference 10000
在OSPF協議中的網絡類型爲廣播網絡類型,其默認hello計時器爲10秒,dead計時器爲40秒
三層交換機需先清除以太網接口二層配置並開啓三層功能
[Huawei-GigabitEthernet0/0/1]undo portswitch [Huawei-GigabitEthernet0/0/1]ospf timer hello 20 [Huawei-GigabitEthernet0/0/1]ospf timer dead 80