這篇文章的資料來源都是來自於Cisco academy裡面的內容,這篇相當於我自己做的筆記
1. OSPF 的組成要素
其實大部分的路由協定都差不多,也就是交換一些路由資訊,宣告自己的網路
OSPF的 routing protocol message:
- Hello packet
- Database description packet
- Link-state request packet
- Link-state update packet
- Link-state acknowledgment packet
OSPF Database
這個部分考試還蠻會考的,同時也很令人困惑
- Adjacency database — This creates the neighbor table.
- Link-state database (LSDB) — This creates the topology table.
- Forwarding database — This creates the routing table.
OSPF algorithm:
OSPF使用Dijkstra shortest-path first (SPF) algorithm來計算最佳路徑,這種演算法使用累計的cost來算出最佳路徑。
2. Link-State 運作原理:
- Establish Neighbor Adjacencies: 像有開OSPF的介面送出Hello封包,藉此來了解是否有鄰居的存在
- Exchange Link-State Advertisements: 當鄰居建立起來後,Router會互相向新鄰居flood自己擁有的LSAs,並移植持續,直到大家的LSA都一樣為止
- Build the Link State Database: 當Router收到LSA之後,就會利用剛剛收到的LSA建立LSDB
- Execute the SPF Algorithm: Router會把LSDB的內容餵給SPF演算法吃,並計算出SPF tree
- Choose the Best Route: 當SPF tree建立好了之後,就可以找出每一筆遠端網路的最佳路徑,並建立起Forwardind Database。
3. 單區OSPF與多區的OSPF
Single-Area:
也就是只有一個OSPF area。
Multiarea OSPF:
也就是一個以上的OSPF area,中間會有ABR擔任轉接服務,對於大型網路的話好處有很多
- 減少路由表的數量
- 減少LSA更新的沖刷面積
- 減少SPF計算,比如說更動area 10的路由,就只會讓Area 10 裡面的路由啟動SPF演算法,Area 0裡面的路由只需要等答案而已。
4. OSPF封包種類(Link-state packets, LSP)
OSPF利用LSP去執行尋找最佳路徑,鄰居的狀態或路由表更新等任務
Type 1: Hello packet:
Hello 封包是一種具有建立鄰居關係與選舉DR與BDR能力的封包,兩個路由器必須互相同意Hello封包內的資料才可以建立鄰居關係。
OSPFv2使用 224.0.0.5當群播地址,有開啟OSPF才會收到Hello封包。
Type 2: Database Description (DBD) packet:
這種封包會交換LSDB裡面的資訊目錄,以利同一個Area裡的各個路由器能有完全相同的LSDB,這樣計算出SPF tree才會精準。
Type 3: Link-State Request (LSR) packet
如果剛剛用DBD檢查到自己的LSDB不是最新的,那就送出LSR來獲取最新的LSA
其他路由器的 (DBDs) —我自己的 (LADB) = 我要去要求的 (LSR)
Type 4: Link-State Update (LSU) packet
回送給LSR的sender的更新訊息。
Type 5: Link-State Acknowledgment (LSAck) packet
給LSU sender的確認訊息。
5. OSPF 運作狀態
剛剛講了封包的運用,這邊要講OSPF形成的過程
1.Down State:
- 沒有Hello 封包
- 寄出去Hello封包,還沒有回音
2. Init State:
- 收到來自鄰居的Hello 封包,並從Hello封包內找到Router ID
3.Two-Way State:兩個路由器正在喬事情
- 選出DR,BDR
4. ExStart State: On point-to-point networks, the two routers decide which router will initiate the DBD packet exchange and decide upon the initial DBD packet sequence number.
在這邊兩個Router會用higher router ID來決定誰先送DBD的資訊
5.Exchange State:
- 交換DBD的資訊
當R1的Router比較高時,R1先送給R2(這個範例R1跟R2的DBD剛好一樣,所以不需要送出LSA,LSU)
R1: (LSDB)R1的DBD →R2
R2: (LSAck) →R1
R2:(LSDB)R2的DBD →R1
R1:(LSAck) →R2
6.Loading State:
- 如果發現兩邊的DBD不一致,則交換LAS,LAU封包
- 因為有更新,所以啟動SPF 演算法
7. Full State:
- 大功告成
6. DR的重要性:
DR(Designated Router)跟BDR(Backup Designated Router)是一個協助發送LSA的路由器,試想,如果今天有十台路由器戶相連接,並且同時發送LSA訊息,那會導致正個網路被LSA塞爆,所以當路由器戶相連接,他們就會選出一台DR與一台BDR來作為發送LSA的路由器,就是統一由他們來處理更新資訊的部分就是了。
7 .實際操作 — Signal OSPF v2
- enabled OSPF: router ospf process-id
- Router ID
- 宣告網路
- Configure Passive Interfaces
(1). enabled OSPF: router ospf process-id
這個process-id的數字跟整個ospf運作沒有關係,所以想設多少就設多少
Router(config)#router ospf 1
Router(config-router)#?
area OSPF area parameters
auto-cost Calculate OSPF interface cost according to bandwidth
default-information Control distribution of default information
distance Define an administrative distance
exit Exit from routing protocol configuration mode
log-adjacency-changes Log changes in adjacency state
neighbor Specify a neighbor router
network Enable routing on an IP network
no Negate a command or set its defaults
passive-interface Suppress routing updates on an interface
redistribute Redistribute information from another routing protocol
router-id router-id for this OSPF process
(2). Router ID
Router ID就是OSPF唯一的識別碼了,所以一定要謹慎小心的設定
- Router ID可以決定Router在Exstart state裡面誰先寄出DBD的順序
- 具有較高的Router ID可以當選DR的身分
- Router ID可以手動設定,如果沒有設定,Router會先選用Loopback的IP,如果沒有,就用最大的ip地址
- 設定方法
Router(config-router)#router-id 1.1.1.1
- 如果必須修改Router-id,必須輸入下列指令
R1# clear ip ospf process
(3). 宣告網路
Router(config-router)# network network-address wildcard-mask area area-id
Router(config)#router ospf 1
Router(config-router)#network 192.168.1.0 0.0.0.255 area 0
Router(config-router)#network 172.32.108.0 0.0.255.255 area 0
(4). Configure Passive Interfaces:
當我的介面沒有連接上任何路由器的時候,我其實是不需要像這個介面送出 OSPF Package的,但是我仍需要再OSPF裡面宣告我擁有這段網路。
比如說拓樸圖-1裡面,192.168.1.0那邊是沒有連接到其他路由器,那就不需要朝那裏發送OSPF封包。
Router(config-router)#passive-interface f0/0
8. 實際操作 — Multiaccess OSPF
- OSPF Designated Router
(1). OSPF Designated Router
在OSPF得multiaccess networks裡面,路由器們有三種腳色
- DR(designated Router)
- BDR(backup designated Router)
- DROTHER
當路DROTHER要通知自己得LSA給其他Router時,他不會直接沖刷整個網路,他會把LSA送給224.0.0.6這個群播ip,當然,這個ip只有DR跟BDR在監聽,DR收到之後會把LSA送到 224.0.0.5給其他路由
(2). OSPF neighbor
這邊就是看adjacency database
Router#show ip ospf neighborR1# show ip ospf neighbor
Neighbor ID Pri State Dead Time Address Interface
2.2.2.2 1 FULL/BDR 00:00:31 192.168.1.2 GigabitEthernet0/0/0
3.3.3.3 1 FULL/DR 00:00:39 192.168.1.3 GigabitEthernet0/0/0
完成收斂: FULL/BDR,FULL/DR,FULL/DROTHER
正在交換hello封包: 2-WAY/DROTHER
(3). DR/BDR Election
前面講過DR可以用Router-id來選,這邊補充完整過程
- 先用 highest interface priority來選
- 如果都一樣, 就用router ID最高的來選(前提是要手動設定)
- 如果沒有手動設定,就先用最大的loopback
- 如果沒有loopback,就用最大的ipv4
改OSPF Priority(拓樸圖2)
R1 route-id:1.1.1.1
R2 route-id: 2.2.2.2
R2#show ip ospf interface f0/0FastEthernet0/0 is up, line protocol is up
Internet address is 192.168.0.2/24, Area 0
Process ID 10, Router ID 2.2.2.2, Network Type BROADCAST, Cost: 1
Transmit Delay is 1 sec, State DR, Priority 1
Designated Router (ID) 2.2.2.2, Interface address 192.168.0.2
Backup Designated Router (ID) 1.1.1.1, Interface address 192.168.0.1
Timer intervals configured, Hello 10, Dead 40, Wait 40, Retransmit 5
Hello due in 00:00:06
Index 1/1, flood queue length 0
Next 0x0(0)/0x0(0)
Last flood scan length is 1, maximum is 1
Last flood scan time is 0 msec, maximum is 0 msec
Neighbor Count is 1, Adjacent neighbor count is 1
Adjacent with neighbor 1.1.1.1 (Backup Designated Router)
Suppress hello for 0 neighbor(s)
R2的Priority是預設的 1,但因為router-id比較大,所以是RD
現在我們試著更改Priority來改變DR
R1(config)#int f0/0
R1(config-if)#ip os
R1(config-if)#ip ospf pr
R1(config-if)#ip ospf priority 100
R1(config-if)#endR1#clear ip ospf process
Reset ALL OSPF processes? [no]: yR1#show ip ospf interface f0/0FastEthernet0/0 is up, line protocol is up
Internet address is 192.168.0.1/24, Area 0
Process ID 10, Router ID 1.1.1.1, Network Type BROADCAST, Cost: 1
Transmit Delay is 1 sec, State DR, Priority 100
Designated Router (ID) 1.1.1.1, Interface address 192.168.0.1
Backup Designated Router (ID) 2.2.2.2, Interface address 192.168.0.2
Timer intervals configured, Hello 10, Dead 40, Wait 40, Retransmit 5
Hello due in 00:00:02
Index 1/1, flood queue length 0
Next 0x0(0)/0x0(0)
Last flood scan length is 1, maximum is 1
Last flood scan time is 0 msec, maximum is 0 msec
Neighbor Count is 1, Adjacent neighbor count is 1
Adjacent with neighbor 2.2.2.2 (Backup Designated Router)
Suppress hello for 0 neighbor(s)
接著再看一次R2,已經變成BDR了
R2#show ip ospf interface f0/0FastEthernet0/0 is up, line protocol is up
Internet address is 192.168.0.2/24, Area 0
Process ID 10, Router ID 2.2.2.2, Network Type BROADCAST, Cost: 1
Transmit Delay is 1 sec, State BDR, Priority 1
Designated Router (ID) 1.1.1.1, Interface address 192.168.0.1
Backup Designated Router (ID) 2.2.2.2, Interface address 192.168.0.2
Timer intervals configured, Hello 10, Dead 40, Wait 40, Retransmit 5
Hello due in 00:00:07
Index 1/1, flood queue length 0
Next 0x0(0)/0x0(0)
Last flood scan length is 1, maximum is 1
Last flood scan time is 0 msec, maximum is 0 msec
Neighbor Count is 1, Adjacent neighbor count is 1
Adjacent with neighbor 1.1.1.1 (Designated Router)
Suppress hello for 0 neighbor(s)
9 .Metric 與 Cost
之前有提到說要OSPF是用cost來決定最佳路徑,這邊要來說明如何更改最佳路徑,或是利用調整cost來達到
先說如何決定cost
(1). OSPF Cost
Cost = reference bandwidth / interface bandwidth
reference bandwidth: 10⁸ (100,000,000);
interface bandwidth: 跟port有關
R1#show interfaces f0/0
FastEthernet0/0 is up, line protocol is up (connected)
Hardware is Lance, address is 00d0.ff66.d701 (bia 00d0.ff66.d701)
Internet address is 192.168.0.1/24
MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec,
FastEthernet0/0 is up, line protocol is up
Internet address is 192.168.0.1/24, Area 0
Process ID 10, Router ID 1.1.1.1, Network Type BROADCAST, Cost: 1
BW: 100000Kbit = 100,000,000bps
cost = 100,000,000 / 100,000,000 = 1
知道怎麼算之後,那要怎麼改變cost呢?
有兩種方式
- 直接改cost
R1(config)#int f0/0
R1(config-if)#ip os
R1(config-if)#ip ospf c
R1(config-if)#ip ospf cost 50
R1(config-if)#end
R1#
%SYS-5-CONFIG_I: Configured from console by consoleR1#show ip ospf interface f0/0FastEthernet0/0 is up, line protocol is up
Internet address is 192.168.0.1/24, Area 0
Process ID 10, Router ID 1.1.1.1, Network Type BROADCAST, Cost: 50
Transmit Delay is 1 sec, State DR, Priority 100
Designated Router (ID) 1.1.1.1, Interface address 192.168.0.1
Backup Designated Router (ID) 2.2.2.2, Interface address 192.168.0.2
- 改reference bandwidth
interface bandwidth是固定的,但是reference bandwidth可以改。
Router(config-router)# auto-cost reference-bandwidth Mbps
這邊我們嘗試讓cost改成10,因為interface bandwidth不能動,所以把reference bandwidth改成1000Mbps(原本是100Mbps)
R1(config)#router ospf 10
R1(config-router)#auto-cost reference-bandwidth 1000
% OSPF: Reference bandwidth is changed.
Please ensure reference bandwidth is consistent across all routers.
R1(config-router)#end
R1#
%SYS-5-CONFIG_I: Configured from console by consoleR1#show ip ospf int f0/0FastEthernet0/0 is up, line protocol is up
Internet address is 192.168.0.1/24, Area 0
Process ID 10, Router ID 1.1.1.1, Network Type BROADCAST, Cost: 10
Transmit Delay is 1 sec, State DR, Priority 100
Designated Router (ID) 1.1.1.1, Interface address 192.168.0.1
(2). Cost 計算
Cost的計算只要計算出去的孔就好了,比如說我們要從R1到10.0.0.0這個遠端網路,我們會經過R1的Fa0/0,R2的loopback
R1的Fa0/0:(剛剛有調整)
FastEthernet0/0 is up, line protocol is up
Internet address is 192.168.0.1/24, Area 0
Process ID 10, Router ID 1.1.1.1, Network Type BROADCAST, Cost: 10
R2的loopback:(為預設值)
Loopback0 is up, line protocol is up
Internet address is 10.0.0.1/8, Area 0
Process ID 10, Router ID 2.2.2.2, Network Type LOOPBACK, Cost: 1
Loopback interface is treated as a stub Host
R1到10.0.0.0的Metric就是10+1 = 11
R1#show ip route 10.0.0.1
Routing entry for 10.0.0.1/32
Known via "ospf 10", distance 110, metric 11, type intra area
Last update from 192.168.0.2 on FastEthernet0/0, 00:04:11 ago
Routing Descriptor Blocks:
* 192.168.0.2, from 2.2.2.2, 00:04:11 ago, via FastEthernet0/0
Route metric is 11, traffic share count is 1
10 . Hello 封包的 Intervals
Hello封包有兩種主要的參數
- Hello (預設10秒一次,使用 multicast address 224.0.0.5)
- Dead (預設40秒一次)
Dead interval是用來管制LSDB裡面的內容,一般情況各Router都會利用Hello封包來確認對方的死活,如果傳了4(預設40秒一次)次給對方,對方都沒有回應,那我們就認定對方已經死亡了。
確認死亡後,我方會先更新自己的LSDB,把該路由刪除,並把LSDB的資訊沖刷出去。
R1#show ip ospf interface f0/0FastEthernet0/0 is up, line protocol is up
Internet address is 192.168.0.1/24, Area 0
Process ID 10, Router ID 1.1.1.1, Network Type BROADCAST, Cost: 50
Transmit Delay is 1 sec, State DR, Priority 100
Designated Router (ID) 1.1.1.1, Interface address 192.168.0.1
Backup Designated Router (ID) 2.2.2.2, Interface address 192.168.0.2
Timer intervals configured, Hello 10, Dead 40, Wait 40, Retransmit 5R1#show ip ospf neighborNeighbor ID Pri State Dead Time Address Interface
2.2.2.2 1 FULL/BDR 00:00:34(管制剩餘的dead interval) 192.168.0.2 FastEthernet0/0
修改方法
Router(config-if)# ip ospf hello-interval seconds
Router(config-if)# ip ospf dead-interval seconds
R1(config)#int f0/0
R1(config-if)#ip ospf hello-interval 30
R1(config-if)#ip ospf dead-interval 60
R1(config-if)#endR1#show ip ospf interface f0/0FastEthernet0/0 is up, line protocol is up
Internet address is 192.168.0.1/24, Area 0
Process ID 10, Router ID 1.1.1.1, Network Type BROADCAST, Cost: 50
Transmit Delay is 1 sec, State DR, Priority 100
Designated Router (ID) 1.1.1.1, Interface address 192.168.0.1
No backup designated router on this network
Timer intervals configured, Hello 30, Dead 60, Wait 60, Retransmit 5
11. OSPF的default-gateway
步驟:
- 在連接isp的Router上宣告一筆靜態路由
- 使用default-information originate把它撒出去
12. OSPF常用指令
驗證Routing:
- show ip interface brief — This verifies that the desired interfaces are active with correct IP addressing.
- show ip route- This verifies that the routing table contains all the expected routes.
驗證OSPF訊息:
- show ip ospf neighbor
- show ip protocols
- show ip ospf
- show ip ospf interface