动态路由协议OSPF单区域基础实战详解,带你一分钟学会

2023-05-18 14:00:59

一、OSPF概念

1、OSPF(Open Shortest Path First,开放最短链路优先)路由协议是典型的链路状态路由协议。OSPF 由 IETF 在 20 世纪 80 年代末期开发,OSPF 是 SPF 类路由协议中的开放式版本。最初的 OSPF 规范体现在 RFC1131 中,被称为 OSPF 版本 1,但是版本 1 很快被进行了重大改进的版本所代替,这个新版本体现在 RFC1247 文档中。RFC1247 被称为 OSPF 版本 2,而 OSPF 版本 3 是关于 IPv6 的。

2、OSPF 作为一种内部网关协议(Interior Gateway Protocol,IGP),用于在同一个自治

系统(AS)中的路由器之间交换路由信息。OSPF 的特性如下:

1. 可适应大规模网络;

2. 收敛速度快;

3. 无路由环路;

4. 支持 VLSM 和 CIDR;

5. 支持等价路由;

6. 支持区域划分, 构成结构化的网络;

7. 提供路由分级管理;

8. 支持简单口令和 MD5 认证;

9. 以组播方式传送协议报文;

10. OSPF 路由协议的管理距离是 110;

11. OSPF 路由协议采用 cost 作为度量标准;

12. OSPF 维护邻居表、拓扑表和路由表。

另外,OSPF 将网络划分为四种类型:广播多路访问型(BMA)、非广播多路访问型(NBMA)、点到点型(Point-to-Point)、点到多点型(Point-to-MultiPoint)。不同的二层链路的类型需要 OSPF 不同的网络类型来适应。

下面的几个术语是学习 OSPF 要掌握的:

1. 链路:链路就是路由器用来连接网络的接口;

2. 链路状态:用来描述路由器接口及其与邻居路由器的关系。所有链路状态信息构成

链路状态数据库;

3. 区域:有相同的区域标志的一组路由器和网络的集合。在同一个区域内的路由器有

相同的链路状态数据库;

4. 自治系统:采用同一种路由协议交换路由信息的路由器及其网络构成一个自治系统;

5. 链路状态通告(LSA):LSA 用来描述路由器的本地状态,LSA 包括的信息有关于路由

器接口的状态和所形成的邻接状态;

6. 最短路经优先(SPF)算法:是 OSPF 路由协议的基础。SPF 算法有时也被称为 Dijkstra

算法,这是因为最短路径优先算法(SPF)是 Dijkstra 发明的。OSPF 路由器利用 SPF,独立

地计算出到达任意目的地的最佳路由。

二、 点到点链路上的 OSPF

1、实验拓扑

2、实验步骤

(1)步骤 1:配置路由器 R1

R1(config)# router ospf 1

R1(config-router)# router-id 1.1.1.1

R1(config-router)# network 1.1.1.0 255.255.255.0 area 0

R1(config-router)# network 192.168.12.0 255.255.255.0 area 0

(2)步骤 2:配置路由器 R2

R2(config)# router ospf 1

R2(config-router)# router-id 2.2.2.2

R2(config-router)# network 192.168.12.0 255.255.255.0 area 0

R2(config-router)# network 192.168.23.0 255.255.255.0 area 0

R2(config-router)# network 2.2.2.0 255.255.255.0 area 0

(3)步骤 3:配置路由器 R3

R3(config)# router ospf 1

R3(config-router)# router-id 3.3.3.3

R3(config-router)# network 192.168.23.0 255.255.255.0 area 0

R3(config-router)# network 192.168.34.0 255.255.255.0 area 0

R3(config-router)# network 3.3.3.0 255.255.255.0 area 0

(4)步骤 4:配置路由器 R4

R4(config)# router ospf 1

R4(config-router)# router-id 4.4.4.4

R4(config-router)# network 4.4.4.0 0.0.0.255 area 0

3、【技术要点 】

(1)OSPF 路由进程 ID 的范围必须在 1-65535 之间,而且只有本地含义,不同路由器

的路由进程 ID 可以不同。如果要想启动 OSPF 路由进程,至少确保有一个接口是 up 的;

(2)区域 ID 是在 0-4294967295 内的十进制数,也可以是 IP 地址的格式 A.B.C.D。当

网络区域 ID 为 0 或 0.0.0.0 时称为主干区域;

(3)在高版本的 IOS 中通告 OSPF 网络的时候,网络号的后面可以跟网络掩码,也可以

跟反掩码,都是可以的;

(4)确定 Router ID 遵循如下顺序:

① 最优先的是在 OSPF 进程中用命令“ router-id”指定了路由器 ID;

② 如果没有在 OSPF 进程中指定路由器 ID,那么选择 IP 地址最大的环回接口的 IP 地

址为 Router ID;

③ 如果没有环回接口,就选择最大的活动的物理接口的 IP 地址为 Router ID。

建议用命令“ router-id”来指定路由器 ID,这样可控性比较好。

4.实验调试

(1) show ip route

R2# show ip route

Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP

D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area

N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2

E1 - OSPF external type 1, E2 - OSPF external type 2

i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2

ia - IS-IS inter area, * - candidate default, U - per-user static route

o - ODR, P - periodic downloaded static route

Gateway of last resort is not set

C 192.168.12.0/24 is directly connected, Serial0/0/0

1.0.0.0/32 is subnetted, 1 subnets

O 1.1.1.1 [110/782] via 192.168.12.1, 00:18:40, Serial0/0/0

2.0.0.0/24 is subnetted, 1 subnets

C 2.2.2.0 is directly connected, Loopback0

3.0.0.0/32 is subnetted, 1 subnets

O 3.3.3.3 [110/782] via 192.168.23.3, 00:18:40, Serial0/0/1

4.0.0.0/32 is subnetted, 1 subnets

O 4.4.4.4 [110/1563] via 192.168.23.3, 00:18:40, Serial0/0/1

O 3.3.3.3 [110/782] via 192.168.23.3, 00:18:40, Serial0/0/1

4.0.0.0/32 is subnetted, 1 subnets

O 4.4.4.4 [110/1563] via 192.168.23.3, 00:18:40, Serial0/0/1

C 192.168.23.0/24 is directly connected, Serial0/0/1

O 192.168.34.0/24 [110/1562] via 192.168.23.3, 00:18:41, Serial0/0/1

输出结果表明同一个区域内通过 OSPF 路由协议学习的路由条目用代码“O O”表示。

【说明 】

① 环回接口 OSPF 路由条目的掩码长度都是 32 位,这是环回接口的特性,尽管通告了

24 位,解决的办法是在环回接口下修改网络类型为“ Point-to-Point”,操作如下:

R2(config)# interface loopback 0

R2(config-if)# ip ospf network point-to-point

这样收到的路由条目的掩码长度和通告的一致。

② 路由条目“ 4.4.4.4”的度量值为 1563

(2) show ip protocols

R2# show ip protocols

Routing Protocol is "ospf 1"

//当前路由器运行的 OSPF 进程 ID

Outgoing update filter list for all interfaces is not set

Incoming update filter list for all interfaces is not set

Router ID 2.2.2.2

//本路由器 ID

Number of areas in this router is 1. 1 normal 0 stub 0 nssa

//本路由器参与的区域数量和类型

Maximum path: 4

//支持等价路径最大数目

Routing for Networks:

2.2.2.0 0.0.0.255 area 0

192.168.12.0 0.0.0.255 area 0

192.168.23.0 0.0.0.255 area 0

//以上四行表明 OSPF 通告的网络以及这些网络所在的区域

Reference bandwidth unit is 100 mbps

//参考带宽为 10的8次方

Routing Information Sources:

Gateway Distance Last Update

4.4.4.4 110 00:08:36

3.3.3.3 110 00:08:36

1.1.1.1 110 00:08:36

//以上 5 行表明路由信息源

Distance: (default is 110)

//OSPF 路由协议默认的管理距离

(3) show ip ospf

该命令显示 OSPF 进程及区域的细节,如路由器运行 SPF 算法的次数等。

R2# show ip ospf 1

Routing Process "ospf 1" with ID 2.2.2.2

Start time: 00:50:57.156, Time elapsed: 00:42:41.880

Supports only single TOS(TOS0) routes

Supports opaque LSA

Supports Link-local Signaling (LLS)

Supports area transit capability

Router is not originating router-LSAs with maximum metric

Initial SPF schedule delay 5000 msecs

Minimum hold time between two consecutive SPFs 10000 msecs

Maximum wait time between two consecutive SPFs 10000 msecs

Incremental-SPF disabled

Minimum LSA interval 5 secs

Minimum LSA arrival 1000 msecs

LSA group pacing timer 240 secs

Interface flood pacing timer 33 msecs

Retransmission pacing timer 66 msecs

Number of external LSA 0. Checksum Sum 0x000000

Number of opaque AS LSA 0. Checksum Sum 0x000000

Number of DCbitless external and opaque AS LSA 0

Number of DoNotAge external and opaque AS LSA 0

Number of areas in this router is 1. 1 normal 0 stub 0 nssa

Number of areas transit capable is 0

External flood list length 0

IETF NSF helper support enabled

Cisco NSF helper support enabled

Area BACKBONE(0)

Number of interfaces in this area is 3 3

Area has no authentication

SPF algorithm last executed 00:15:07.580 ago

SPF algorithm executed 9 times

Area ranges are

Number of LSA 4. Checksum Sum 0x02611A

Number of opaque link LSA 0. Checksum Sum 0x000000

Number of DCbitless LSA 0

Number of indication LSA 0

Number of DoNotAge LSA 0

Flood list length 0

(4) show ip ospf interface

R2# show ip ospf interface s0/0/0

Serial0/0/0 is up, line protocol is up

Internet Address 192.168.12.2/24, Area 0

//该接口的地址和运行的 OSPF 区域

Process ID 1, Router ID 2.2.2.2, Network Type POINT_TO_POINT, Cost: 781

//进程 ID,路由器 ID,网络类型,接口 Cost 值

Transmit Delay is 1 sec, State POINT_TO_POINT

//接口的延迟和状态

Timer intervals configured, Hello 10, Dead 40, Wait 40, Retransmit 5

oob-resync timeout 40

//显示几个计时器的值

Hello due in 00:00:05

//距离下次发送 Hello 包的时间

Supports Link-local Signaling (LLS)

//支持 LLS

Cisco NSF helper support enabled

IETF NSF helper support enabled

//以上两行表示启用了 IETF 和 Cisco 的 NSF 功能

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

//已经建立邻接关系的邻居路由器 ID

Suppress hello for 0 neighbor(s)

//没有进行 Hello 抑制

(5) show ip ospf neighbor

R2# show ip ospf neighbor

Neighbor ID Pri State Dead Time Address Interface

3.3.3.3 0 FULL/ - 00:00:35 192.168.23.3 Serial0/0/1

1.1.1.1 0 FULL/ - 00:00:38 192.168.12.1 Serial0/0/0

以上输出表明路由器 R2 有两个邻居,它们的路由器 ID 分别为 1.1.1.1 和 3.3.3.3,其

它参数解释如下:

① Pri:邻居路由器接口的优先级;

② State:当前邻居路由器接口的状态;

③ Dead Time:清除邻居关系前等待的最长时间;

④ Address:邻居接口的地址;

⑤ Interface:自己和邻居路由器相连接口;

⑥ “-”: 表示点到点的链路上 OSPF 不进行 DR 选举。

【技术要点 】

OSPF 邻居关系不能建立的常见原因:

① hello 间隔和 dead 间隔不同;

同一链路上的 hello 包间隔和 dead 间隔必须相同才能建立邻接关系。默认情况下,

网络类型 Hello 间隔(秒) Dead 间隔(秒)

广播多路访问 10 40

非广播多路访问 30 120

点到点 10 40

点到多点 30 120

默认时 Dead 间隔是 Hello 间隔的四倍。可以在接口下通过“ ip ospf hello-interval”

和“ ip ospf dead-interval”命令调整。

② 区域号码不一致;

③ 特殊区域(如stub,nssa等)区域类型不匹配;

④ 认证类型或密码不一致;

⑤ 路由器 ID 相同;

⑥ Hello 包被 ACL deny;

⑦ 链路上的MTU 不匹配;

⑧ 接口下OSPF网络类型不匹配。

(6) show ip ospf database

R2# show ip ospf database

OSPF Router with ID (2.2.2.2) (Process ID 1)

Router Link States (Area 0)

Link ID ADV Router Age Seq# Checksum Link count

1.1.1.1 1.1.1.1 240 0x80000005 0x00BA35 3

2.2.2.2 2.2.2.2 1308 0x80000008 0x00D7C0 5

3.3.3.3 3.3.3.3 1310 0x80000007 0x00282D 5

4.4.4.4 4.4.4.4 44 0x80000004 0x009AFE 3

以上输出是 R2 的区域 0 的拓扑结构数据库的信息,标题行的解释如下:

① Link ID:是指 Link State ID,代表整个路由器,而不是某个链路;

② ADV Router:是指通告链路状态信息的路由器 ID;

③ Age:老化时间;

④ Seq#:序列号;

⑤ Checksum:校验和;

⑥ Link count:通告路由器在本区域内的链路数目。

以下是更多华为数通路由交换方向相关技术分享,欢迎对华为网络技术感兴趣的盆友订阅。

华为新版HCIP数通路由交换

华为新版HCIE数通路由交换