一、背景介绍
提到路由,就一定会涉及选路,ospf与其他路由协议一样,同样存在选路,除了对比cost(metric)值,ospf协议还会对比表项,并且表项的优先级高于cost值,本文详细说明ospf协议的路径选取原则。
二、实验拓扑
本次实验拓扑如上图所示,R2,R3环回接口模拟外部网络,通过import-route导入,每条路径的cost如标注所示
三、ospf选路详解
1.cost值比较
此时将基础配置按照上图配置好,外部路由直接使用import-route direct导入,未设置接口cost值时,此时在R1上看到去往192.168.0.0/24网段的路由如下所示:
[R1]display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 11 Routes : 12
Destination/Mask Proto Pre Cost Flags NextHop Interface
12.0.0.0/24 Direct 0 0 D 12.0.0.1 GigabitEthernet
0/0/0
12.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
12.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
13.0.0.0/24 Direct 0 0 D 13.0.0.1 GigabitEthernet
0/0/1
13.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
13.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
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
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.0.0/24 O_ASE 150 1 D 13.0.0.3 GigabitEthernet
0/0/1
O_ASE 150 1 D 12.0.0.2 GigabitEthernet
0/0/0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
通过路由表能够看到,R1认为13.0.0.3与12.0.0.2是等价路由,都可以去往192.168.0.0/24,且cost为1(环回接口cost默认为1),但如果只是将R2的g0/0/0与R3的g0/0/1端口开销值进行更,R1去往192.168.0.0/24还是负载均衡
[R2-GigabitEthernet0/0/0]display this
[V200R003C00]
#
interface GigabitEthernet0/0/0
ip address 12.0.0.2 255.255.255.0
ospf cost 100
#
return
[R3-GigabitEthernet0/0/1]display this
[V200R003C00]
#
interface GigabitEthernet0/0/1
ip address 13.0.0.3 255.255.255.0
ospf cost 10
#
return
只有将R1上的g0/0/0与g0/0/1端口开销修改后才能看到选路后的效果,此时R1路由表去往192.168.0.0/24网段只会保留去往R3的条目
[R1]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 11 Routes : 11
Destination/Mask Proto Pre Cost Flags NextHop Interface
12.0.0.0/24 Direct 0 0 D 12.0.0.1 GigabitEthernet
0/0/0
12.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
12.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
13.0.0.0/24 Direct 0 0 D 13.0.0.1 GigabitEthernet
0/0/1
13.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
13.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
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
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.0.0/24 O_ASE 150 1 D 13.0.0.3 GigabitEthernet
0/0/1
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
原因就是R2与R3跟R1都是直连,cost为0,g0/0/1的端口开销为10,小于g0/0/0的100,所以优选从R3去往192.168.0.0/24网段。由此可以得知路径开销计算的方法:本地端口开销+到达目的地址经过所有网段的开销,以R2上12.0.0.0/24去往R3上13.0.0.0/24为例
此时开销为R2本地g0/0/0接口开销(100)加R1上13.0.0.0/24网段端口(g0/0/1)开销(10),所以此时总的开销为110
display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 11 Routes : 11
Destination/Mask Proto Pre Cost Flags NextHop Interface
12.0.0.0/24 Direct 0 0 D 12.0.0.2 GigabitEthernet
0/0/0
12.0.0.2/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
12.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
13.0.0.0/24 OSPF 10 110 D 12.0.0.1 GigabitEthernet
0/0/0
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
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.0.0/24 Direct 0 0 D 192.168.0.1 LoopBack0
192.168.0.1/32 Direct 0 0 D 127.0.0.1 LoopBack0
192.168.0.255/32 Direct 0 0 D 127.0.0.1 LoopBack0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
2.外部表项类型1与类型2
上述在将外部路由使用import-route引入时,没有指定类型,此时默认值2,外部表项类型有两种:分为类型1与类型2
[R3-ospf-1]import-route direct ?
cost Set cost
route-policy Route policy
tag Specify route tag
type Metric type of the imported external routes
Please press ENTER to execute command
[R3-ospf-1]import-route direct type ?
INTEGER<1-2> Type value
其中类型2为开销值不累加,即外部端口开销值为多少,引入ospf后,在ospf域内始终为多少,上文中等价路由的cost值为1,原因就是开销不累加,只按照环回接口本身的开销值计算,如果在导入时将R2的外部表项类型改为1,此时尽管R3去往192.168.0.0/24网段的开销为101,小于R2的开销1,但在R1的路由表中,存放的依然是R2的路由
display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 11 Routes : 11
Destination/Mask Proto Pre Cost Flags NextHop Interface
12.0.0.0/24 Direct 0 0 D 12.0.0.1 GigabitEthernet
0/0/0
12.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
12.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
13.0.0.0/24 Direct 0 0 D 13.0.0.1 GigabitEthernet
0/0/1
13.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
13.0.0.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
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
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.0.0/24 O_ASE 150 101 D 12.0.0.2 GigabitEthernet
0/0/0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
说明在ospf路径选取时:外部表项类型1>外部表项类型2>cost值
3.区域内与区域间
此时拓扑如下图所示,首先让所有路由器在同一区域内,按照标注建立好ospf邻居,并设置好端口开销值
此时R1如果到24.0.0.0/24网段有2条路径:
- R1--R3--R4,此时开销值为120
- R1--R3--R2--R4,此时开销值为30
所以,R1会选取第2条路径放在自己的路由表里
[R1]tracert 24.0.0.4
traceroute to 24.0.0.4(24.0.0.4), max hops: 30 ,packet length: 40,press CTRL_C
to break
1 13.0.0.3 20 ms 20 ms 20 ms
2 23.0.0.2 30 ms 30 ms 40 ms
3 24.0.0.4 30 ms 30 ms 30 ms
说明在同一区域中,路径取cost值小的,如果此时将拓扑变为如下所示:
R2跟R3此时成为2个abr,还是以R1到24.0.0.0/24网段为例,尽管这时 R1--R3--R2--R4开销值仍为30
[R1]display ospf routing
OSPF Process 1 with Router ID 1.1.1.1
Routing Tables
Routing for Network
Destination Cost Type NextHop AdvRouter Area
12.0.0.0/24 50 Stub 12.0.0.1 1.1.1.1 0.0.0.0
13.0.0.0/24 10 Stub 13.0.0.1 1.1.1.1 0.0.0.0
23.0.0.0/24 20 Stub 13.0.0.3 3.3.3.3 0.0.0.0
24.0.0.0/24 30 Inter-area 13.0.0.3 2.2.2.2 0.0.0.0
34.0.0.0/24 110 Inter-area 13.0.0.3 3.3.3.3 0.0.0.0
Total Nets: 5
Intra Area: 3 Inter Area: 2 ASE: 0 NSSA: 0
但此时他会选择 R1--R3--R4,原因就是从R1--R3后,他会认为R2是区域间路由(即上图中的Inter-area),尽管开销更低,他依然会选择同区域内的R4,虽然此时路径总开销为110
[R1]display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 15 Routes : 15
Destination/Mask Proto Pre Cost Flags NextHop Interface
12.0.0.0/24 Direct 0 0 D 12.0.0.1 Serial1/0/0
12.0.0.1/32 Direct 0 0 D 127.0.0.1 Serial1/0/0
12.0.0.2/32 Direct 0 0 D 12.0.0.2 Serial1/0/0
12.0.0.255/32 Direct 0 0 D 127.0.0.1 Serial1/0/0
13.0.0.0/24 Direct 0 0 D 13.0.0.1 Serial2/0/1
13.0.0.1/32 Direct 0 0 D 127.0.0.1 Serial2/0/1
13.0.0.3/32 Direct 0 0 D 13.0.0.3 Serial2/0/1
13.0.0.255/32 Direct 0 0 D 127.0.0.1 Serial2/0/1
23.0.0.0/24 OSPF 10 20 D 13.0.0.3 Serial2/0/1
24.0.0.0/24 OSPF 10 30 D 13.0.0.3 Serial2/0/1
34.0.0.0/24 OSPF 10 110 D 13.0.0.3 Serial2/0/1
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
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
这样就会产生一个问题:即流量从R1到R4的时候路径为R1--R3--R4
tracert -a 13.0.0.1 34.0.0.4
traceroute to 34.0.0.4(34.0.0.4), max hops: 30 ,packet length: 40,press CTRL_C
to break
1 13.0.0.3 80 ms 20 ms 20 ms
2 34.0.0.4 60 ms 40 ms 30 ms
返回时路径为R4--R2--R3--R1
tracert -a 34.0.0.4 13.0.0.1
traceroute to 13.0.0.1(13.0.0.1), max hops: 30 ,packet length: 40,press CTRL_C
to break
1 24.0.0.2 30 ms 20 ms 10 ms
2 23.0.0.3 10 ms 10 ms 40 ms
3 13.0.0.1 20 ms 40 ms 20 ms
造成来回路径不一致,如果R2,R3是2台安全设备,比如防火墙,在一些厂家(如华为)的策略中默认是不允许的,所以必须关闭原进原出的检查机制
undo firewall session link-state check
四、总结
通过上述实验得知,ospf在进行路径选取时,优先级会按照以下方式进行(1优先级高于2):
- 表项:区域内>区域间>外部路由类型1>外部路由类型2
- cost值