OSPF 協議
開放最短路徑優先(OSPF)是一個開放標准的路由選擇協議,也就是說無論是什么廠商的路由器都可以使用 OSPF 協議配置動態路由。OSPF 基於 Dijkstra 算法進行工作的,OSPF 要構建一個最短路徑樹,然后使用最佳路徑的計算結果來組建路由選擇表。OSPF 也支持對相同目標的等價多路徑路由,也支持 IP 和 IPv6 被路由協議。
OSPF 運行在某個自治系統內部,同時通過 OSPF 協議也可以將多個自治系統連接起來,用於連接 AS 的路由器被稱為自治系統邊界路由器(ASBR)。可以創建網絡的多個不同的區域來保持路由更新的最小化,並阻止故障在整個網絡中傳播,基本思路就是要將更新限定在單一區域內。

OSPF 組件
數據庫和表
OSPF 使用路由協議消息來交換路由信息,OSPF 路由協議的 3 個主要組件包括數據結構、路由協議消息、算法。數據結構是 OSPF 為了運作而建立的表或數據庫,包括以下 3 種:
| 數據庫 | 表 | 說明 |
|---|---|---|
| 鄰接數據庫 | 鄰居表 | 路由器已建立雙向通信的所有鄰居路由器的列表 |
| 鏈路狀態數據庫 | 拓撲表 | 列出網絡中所有其他路由器的相關信息,顯示網絡拓撲 |
| 轉發數據庫 | 路由表 | 在鏈路狀態數據庫上運行算法時生成的路由列表 |
分組類型
OSPF 使用路由協議消息來傳遞路由信息,這些數據包用於發現相鄰路由器,並交換路由信息以保持相關網絡的准確信息。
- Hello:發現、維持鄰居路由器的可達性;
- 數據庫描述:向鄰居給出自己的鏈路狀態數據庫中,所有鏈路狀態項目的摘要信息;
- 鏈路狀態請求:向鄰居請求發送某些鏈路狀態的詳細信息;
- 鏈路狀態更新:使用洪泛法對全網更新鏈路狀態;
- ASK:對更新分組的確認。

SPF 樹
在一個區域的內部,每個路由器都要為同一區域中的每個網絡計算最佳/最短路徑。這個計算是基於拓撲數據庫中收集的信息,並且還需要使用最短路徑優先(SPF)算法。在 SPF 樹中進行計算的路由器就是樹根,而所有其他的網絡則會被編排為樹枝和樹葉,運算出 OSPF 最佳路由插入到路由選擇表中。如果某個路由器的接口存在於多個區域中,那么就需要為每個區域都構建一棵單獨的樹。
OSPF 使用開銷作為度量,完整路徑開銷是沿這條路徑的所有輸出接口開銷的總和,也被稱之為累計開銷,開銷越低表示路徑越好。接口的開銷與接口的帶寬成反比。因此帶寬越高,開銷就越低。負載和延時越多,開銷越高。計算 OSPF 開銷的公式為:
開銷 = 參考帶寬 / 接口帶寬
默認的參考帶寬為 10^8(100 000 000),因此公式為:
開銷 = 100000000 bit/s / 接口帶寬
利用這個規則,100Mbit/s 的快速以太網接口將有一個默認為 1 的 OSPF 開銷,而 10Mbit/s 的以太網接口將有一個取值為 10 的開銷。
IPv4 網絡配置 OSPF
實驗拓撲

| 設備 | 接口 | IP 地址 | 子網掩碼 | 默認網關 |
|---|---|---|---|---|
| R1 | G0/0 | 172.16.1.1 | 255.255.255.0 | 不適用 |
| S0/0/0 | 172.16.3.1 | 255.255.255.252 | 不適用 | |
| S0/0/1 | 192.168.10.5 | 255.255.255.252 | 不適用 | |
| R2 | G0/0 | 172.16.2.1 | 255.255.255.0 | 不適用 |
| R2 | G0/1 | 209.165.200.225 | 255.255.255.224 | 不適用 |
| S0/0/0 | 172.16.3.2 | 255.255.255.252 | 不適用 | |
| S0/0/1 | 192.168.10.9 | 255.255.255.252 | 不適用 | |
| R3 | G0/0 | 192.168.1.1 | 255.255.255.0 | 不適用 |
| S0/0/0 | 192.168.10.6 | 255.255.255.252 | 不適用 | |
| S0/0/1 | 192.168.10.10 | 255.255.255.252 | 不適用 | |
| PC0 | NIC | 209.165.200.225 | 255.255.255.224 | 209.165.200.225 |
| PC1 | NIC | 172.16.1.2 | 255.255.255.0 | 172.16.1.1 |
| PC2 | NIC | 172.16.2.2 | 255.255.255.0 | 172.16.2.1 |
| PC3 | NIC | 192.168.1.2 | 255.255.255.0 | 192.168.1.1 |
配置 OSPFv2 路由
使用進程 ID 10,每台路由器的路由器 ID:R1 = 1.1.1.1、R2 = 2.2.2.2、R3 = 3.3.3.3,LAN 接口設置為被動接口在所有三台路由器上配置 OSPF 路由。
R1(config)#router ospf 10
R1(config-router)#router-id 1.1.1.1
R1(config-router)#network 172.16.1.0 0.0.0.255 area 0
R1(config-router)#network 172.16.3.0 0.0.0.3 area 0
R1(config-router)#network 192.168.10.4 0.0.0.3 area 0
R1(config-router)#passive-interface GigabitEthernet0/0
R2(config)#router ospf 10
R2(config-router)#router-id 2.2.2.2
R2(config-router)#network 172.16.2.0 0.0.0.255 area 0
R2(config-router)#network 172.16.3.0 0.0.0.3 area 0
R2(config-router)#network 192.168.10.8 0.0.0.3 area 0
R2(config-router)#passive-interface GigabitEthernet0/0
R3(config)#router ospf 10
R3(config-router)#router-id 3.3.3.3
R3(config-router)#network 192.168.1.0 0.0.0.255 area 0
R3(config-router)#network 192.168.10.4 0.0.0.3 area 0
R3(config-router)#network 192.168.10.8 0.0.0.3 area 0
R3(config-router)#passive-interface GigabitEthernet0/0
驗證 OSPF 路由
在每台路由器上,路由表應具有至拓撲中每個網絡的路由。
R1#show ip route
R2#show ip route
R3#show ip route



每台 PC 都應能 ping 通其他兩台 PC。

修改 OSPF 默認設置
調整 R1 和 R2 之間的 Hello 計時器和 Dead 計時器。
R1(config)# interface s0/0/0
R1(config-if)# ip ospf hello-interval 15
R1(config-if)# ip ospf dead-interval 60
在一小段時間后,與 R2 的 OSPF 連接將失敗,連接的雙方需要有相同的計時器才能保持鄰接關系。


R2(config)# interface s0/0/0
R2(config-if)# ip ospf hello-interval 15
R2(config-if)# ip ospf dead-interval 60
調整 R1 上的帶寬設置,首先配置下 R2 的 G0/1 所在的網絡加入 OSPF 協議。
R2(config)#router ospf 10
R2(config-router)#network 209.165.200.224 0.0.0.31 area 0
此時從 R1 到 209.165.200.225 的路徑通過 R2 路由。

在 R1 S0/0/0 接口上將帶寬設置為 64 Kb/s,這不會改變實際端口速度,只是 OSPF 在 R1 上的處理的度量將用於計算最佳路由。
R1(config)# int s0/0/0
R1(config-if)# bandwidth 64
此時從 R1 到 209.165.200.225 的路徑通過 R3 路由,這是因為 OSPF 首選開銷較低的路徑。

IPv6 網絡配置 OSPF
實驗拓撲

| 設備 | 接口 | IPv6 地址/前綴 | 默認網關 |
|---|---|---|---|
| R1 | G0/0 | 2001:db8:cafe:1::1/64 | 不適用 |
| S0/0/0 | 2001:db8:cafe:a001::1/64 | 不適用 | |
| S0/0/1 | 2001:db8:cafe:a003::1/64 | 不適用 | |
| R2 | G0/0 | 2001:db8:cafe:2::1/64 | 不適用 |
| S0/0/0 | 2001:db8:cafe:a001::2/64 | 不適用 | |
| S0/0/1 | 2001:db8:cafe:a002::1/64 | 不適用 | |
| R3 | G0/0 | 2001:db8:cafe:3::1/64 | 不適用 |
| S0/0/0 | 2001:db8:cafe:a003::264 | 不適用 | |
| S0/0/1 | 2001:db8:cafe:a002::2/64 | 不適用 | |
| PC1 | NIC | 2001:db8:cafe:1::10/64 | fe80::1 |
| PC2 | NIC | 2001:db8:cafe:2::10/64 | fe80::2 |
| PC3 | NIC | 2001:db8:cafe:3::10/64 | fe80::3 |
這里給出配置的命令。
R1(config)#int g0/0
R1(config-if)#ipv6 enable
R1(config-if)#ipv6 address 2001:db8:cafe:1::1/64
R1(config-if)#no shutdown
R1(config-if)#int s0/0/0
R1(config-if)#ipv6 enable
R1(config-if)#ipv6 address 2001:db8:cafe:a001::1/64
R1(config-if)#no shutdown
R1(config-if)#int s0/0/1
R1(config-if)#ipv6 enable
R1(config-if)#ipv6 address 2001:db8:cafe:a003::1/64
R1(config-if)#no shutdown
R2(config)#int g0/0
R2(config-if)#ipv6 enable
R2(config-if)#ipv6 address 2001:db8:cafe:2::1/64
R2(config-if)#no shutdown
R2(config-if)#int s0/0/0
R2(config-if)#ipv6 enable
R2(config-if)#ipv6 address 2001:db8:cafe:a001::2/64
R2(config-if)#no shutdown
R2(config-if)#int s0/0/1
R2(config-if)#ipv6 enable
R2(config-if)#ipv6 address 2001:db8:cafe:a002::1/64
R2(config-if)#no shutdown
R3(config)#int g0/0
R3(config-if)#ipv6 enable
R3(config-if)#ipv6 address 2001:db8:cafe:3::1/64
R3(config-if)#no shutdown
R3(config-if)#int s0/0/0
R3(config-if)#ipv6 enable
R3(config-if)#ipv6 address 2001:db8:cafe:a003::2/64
R3(config-if)#no shutdown
配置 OSPFv3 路由
使用進程 ID 10,每台路由器的路由器 ID:R1 = 1.1.1.1、R2 = 2.2.2.2、R3 = 3.3.3.3,-在每個接口上啟用 OSPFv3。
R1(config)#ipv6 unicast-routing
R1(config)#ipv6 router ospf 10
R1(config-rtr)#router-id 1.1.1.1
R1(config-rtr)#passive-interface GigabitEthernet 0/0
R1(config)#interface GigabitEthernet 0/0
R1(config-if)#ipv6 ospf 10 area 0
R1(config-if)#interface Serial0/0/0
R1(config-if)#ipv6 ospf 10 area 0
R1(config-if)#interface Serial0/0/1
R1(config-if)#ipv6 ospf 10 area 0
R2(config)#ipv6 unicast-routing
R2(config)#ipv6 router ospf 10
R2(config-rtr)#router-id 2.2.2.2
R2(config-rtr)#passive-interface GigabitEthernet 0/0
R2(config)#interface GigabitEthernet 0/0
R2(config-if)#ipv6 ospf 10 area 0
R2(config-if)#interface Serial0/0/0
R2(config-if)#ipv6 ospf 10 area 0
R2(config-if)#interface Serial0/0/1
R2(config-if)#ipv6 ospf 10 area 0
R3(config)#ipv6 unicast-routing
R3(config)#ipv6 router ospf 10
R3(config-rtr)#router-id 3.3.3.3
R3(config-rtr)#passive-interface GigabitEthernet 0/0
R3(config)#interface GigabitEthernet 0/0
R3(config-if)#ipv6 ospf 10 area 0
R3(config-if)#interface Serial0/0/0
R3(config-if)#ipv6 ospf 10 area 0
R3(config-if)#interface Serial0/0/1
R3(config-if)#ipv6 ospf 10 area 0
驗證連接
每台 PC 都應能 ping 通其他兩台 PC。
R1#show ipv6 route
R2#show ipv6 route
R3#show ipv6 route




參考資料
《思科網絡技術學院教程(第6版):擴展網絡》,[加] Bob Vachon,[美] Allan Johnson 著,思科系統公司 譯,人民郵電出版社
《CCNA 學習指南(第 7 版)》,[美] Todd Lammle 著,袁國忠 徐宏 譯,人民郵電出版社
