本文档介绍思科路由器上开放最短路径优先 (OSPF) 到边界网关协议 (BGP) 的重分发行为。
Cisco建议您先了解OSPF路由类型、BGP基础知识和路由重分布,然后再使用本文档。
本文档不限于特定的软件和硬件版本。
注意:本文档中的示例使用Cisco IOS软件命令语法。
本文档中的信息都是基于特定实验室环境中的设备编写的。本文档中使用的所有设备最初均采用原始(默认)配置。如果您的网络处于活动状态,请确保您了解所有命令的潜在影响。
本技术说明解释了Cisco路由器上OSPF到BGP重分发的行为。RFC 1403记录了有关OSPF和BGP之间交互的历史指南。
有几种类型的 OSPF 路由:
区域内路由 — 在多区域OSPF网络中,同一区域内的路由器将源自某一区域的路由视为区域内路由。这些路由在show ip route命令输出中标记为O。
域间——当路由穿过OSPF局域边界路由器(ABR)时,路由一般被称为OSPF区域间路由。在 show ip route 命令输出中,这些路由标记为 O IA。
区域内路由和区域间路由也称为OSPF内部路由,因为它们代表源自OSPF域中的前缀,而不是重分发到OSPF的路由。
External Type-2或External Type-1 — 重分发到OSPF中的路由(例如Connected、Static或其他路由协议)称为External Type-2或External Type-1。在show ip route命令输出中,这些路由标记为O E2或O E1。
NSSA External Type-2或NSSA External Type 1 — 将某个区域配置为次末节区域(NSSA)并将路由重分发到OSPF时,这些路由称为NSSA External Type-2或NSSA External Type-1。在show ip route命令输出中,这些路由标记为O N2或O N1。
关于外部 2 类或 1 类和 NSSA 2 类或 1 类之间的区别不在本文档讨论范围内。有关详细信息,请参阅《OSPF 设计指南》。
默认行为是不将任何路由从 OSPF 重分配到 BGP 中。必须配置重分配。您可以使用route-map命令在OSPF到BGP重分发期间过滤路由。要明确选择路由类型,请使用适用的match internal、match external或match nssa-external选项。如果没有匹配选项,则默认情况下redistribute ospf会重分发OSPF内部路由。
本文档讨论五个主要的OSPF到BGP重分发案例。拓扑A适用于前四个情况。拓扑B适用于“将OSPF NSSA外部路由重分配到BGP”部分中描述的NSSA外部路由案例。
将 OSPF 重新分发到 BGP 拓扑 A
如果配置了将 OSPF 重新分发到不带关键字的 BGP 中,则默认情况下,仅 OSPF 区域内和区域间路由会重新分发到 BGP 中。要重分布OSPF Intra-area和Inter-area路由,可以在router bgp下将internal关键字与redistribute命令一起使用。
此路由器B(RTB)配置将OSPF内部前缀10.2.2.0/24、10.108.1.0/24和10.108.2.0/24重分发到BGP:
| RTB |
|---|
hostname RTB ! |
路由器 B 只会重分配 OSPF 内部路由:
RTB#show ip bgp BGP table version is 12, local router ID is 10.3.3.1 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, t secondary path, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found Network Next Hop Metric LocPrf Weight Path *> 10.2.2.0/24 0.0.0.0 0 32768 ? *> 10.108.1.0/24 10.2.2.2 3 32768 ? *> 10.108.2.0/24 10.2.2.2 2 32768 ? RTB#
路由器 C 从 BGP 获知以下路由:
RTC#show ip route Codes: L - local, 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, H - NHRP, l - LISP a - application route + - replicated route, % - next hop override, p - overrides from PfR Gateway of last resort is not set 10.0.0.0/8 is variably subnetted, 5 subnets, 2 masks B 10.2.2.0/24 [20/0] via 10.3.3.1, 00:07:07 C 10.3.3.0/24 is directly connected, GigabitEthernet0/0 L 10.3.3.2/32 is directly connected, GigabitEthernet0/0 B 10.108.1.0/24 [20/3] via 10.3.3.1, 00:07:07 B 10.108.2.0/24 [20/2] via 10.3.3.1, 00:07:07 RTC# RTC#show ip bgp BGP table version is 8, local router ID is 10.3.3.2 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, t secondary path, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found Network Next Hop Metric LocPrf Weight Path *> 10.2.2.0/24 10.3.3.1 0 0 100 ? *> 10.108.1.0/24 10.3.3.1 3 0 100 ? *> 10.108.2.0/24 10.3.3.1 2 0 100 ? RTC#
要将OSPF外部路由重分布到BGP,请在router bgp下将external关键字与redistribute命令一起使用。使用 external 关键字时有三种选择:
重新分发外部 1 类和 2 类(默认)
重新分发外部 1 类
重新分发外部 2 类
在配置模式下输入命令,如下所示:
RTB(config)#router bgp 100 RTB(config-router)#redistribute ospf 1 match external
此路由器B配置仅重分发OSPF外部路由,包括类型1和类型2路由:
| RTB |
|---|
|
路由器 B 仅重新分发 OSPF 外部路由:
RTB#show ip bgp BGP table version is 25, local router ID is 10.3.3.1 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, t secondary path, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found Network Next Hop Metric LocPrf Weight Path *> 192.168.1.0 10.2.2.2 20 32768 ? *> 192.168.2.0 10.2.2.2 23 32768 ? RTB#
路由器 C 从 BGP 了解这两个 OSPF 外部路由:
RTC#show ip route Codes: L - local, 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, H - NHRP, l - LISP a - application route + - replicated route, % - next hop override, p - overrides from PfR Gateway of last resort is not set 10.0.0.0/8 is variably subnetted, 2 subnets, 2 masks C 10.3.3.0/24 is directly connected, GigabitEthernet0/0 L 10.3.3.2/32 is directly connected, GigabitEthernet0/0 B 192.168.1.0/24 [20/20] via 10.3.3.1, 00:02:16 B 192.168.2.0/24 [20/23] via 10.3.3.1, 00:02:16
RTC#show ip bgp BGP table version is 21, local router ID is 10.3.3.2 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, t secondary path, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found Network Next Hop Metric LocPrf Weight Path *> 192.168.1.0 10.3.3.1 20 0 100 ? *> 192.168.2.0 10.3.3.1 23 0 100 ? RTC#
要仅重分布第1类OSPF外部路由,请在路由器B的router bgp 100下输入以下命令:
RTB(config)#router bgp 100RTB(config-router)#redistribute ospf 1 match external 1
使用之前的配置,路由器B(RTB)BGP表显示路由器B仅将外部类型1路由重分发到BGP,而所有其他OSPF路由未重分发到BGP:
RTB#show ip bgp BGP table version is 28, local router ID is 10.3.3.1 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, t secondary path, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found Network Next Hop Metric LocPrf Weight Path *> 192.168.2.0 10.2.2.2 23 32768 ? RTB#
要仅重分布第2类OSPF外部路由,请在路由器B的router bgp 100下输入以下命令:
RTB(config)#router bgp 100RTB(config-router)#redistribute ospf 1 match external 2
在这种情况下,OSPF内部路由以及OSPF第1类和第2类外部路由使用内部和外部匹配选项重新分发到BGP,如以下路由器B配置所示:
| RTB |
|---|
|
同样,配置中的 external 替换为 external 1 external 2 。这是正常的,只有您指定希望重新分配到BGP的那些特定外部路由时除外。更改配置后,路由器B重新分发选定的内部和外部路由,而路由器C通过BGP获取这些路由:
RTB#show ip bgp BGP table version is 6, local router ID is 10.3.3.1 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, t secondary path, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found Network Next Hop Metric LocPrf Weight Path *> 10.2.2.0/24 0.0.0.0 0 32768 ? *> 10.108.1.0/24 10.2.2.2 3 32768 ? *> 10.108.2.0/24 10.2.2.2 2 32768 ? *> 192.168.1.0 10.2.2.2 20 32768 ? *> 192.168.2.0 10.2.2.2 23 32768 ? RTB#RTC#show ip route Codes: L - local, 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, H - NHRP, l - LISP a - application route + - replicated route, % - next hop override, p - overrides from PfR Gateway of last resort is not set 10.0.0.0/8 is variably subnetted, 5 subnets, 2 masks B 10.2.2.0/24 [20/0] via 10.3.3.1, 00:03:27 C 10.3.3.0/24 is directly connected, GigabitEthernet0/0 L 10.3.3.2/32 is directly connected, GigabitEthernet0/0 B 10.108.1.0/24 [20/3] via 10.3.3.1, 00:03:27 B 10.108.2.0/24 [20/2] via 10.3.3.1, 00:03:27 B 192.168.1.0/24 [20/20] via 10.3.3.1, 00:03:27 B 192.168.2.0/24 [20/23] via 10.3.3.1, 00:03:27 RTC#
这是一种特殊情况,只会将 NSSA 路由重新分发到 BGP 中。此案例与仅重分发到BGP的OSPF外部(类型1和2)路由部分中描述的情况非常相似。唯一的区别是,OSPF 现在会匹配 NSSA 外部路由,而不仅仅是外部路由。路由器 B 的路由表显示了这些 OSPF NSSA 外部路由:
RTB#show ip route Codes: L - local, 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, H - NHRP, l - LISP a - application route + - replicated route, % - next hop override, p - overrides from PfR Gateway of last resort is not set 10.0.0.0/8 is variably subnetted, 6 subnets, 2 masks C 10.2.2.0/24 is directly connected, GigabitEthernet0/1 L 10.2.2.3/32 is directly connected, GigabitEthernet0/1 C 10.3.3.0/24 is directly connected, GigabitEthernet0/0 L 10.3.3.1/32 is directly connected, GigabitEthernet0/0 O IA 10.108.1.0/24 [110/3] via 10.2.2.2, 00:05:00, GigabitEthernet0/1 O 10.108.2.0/24 [110/2] via 10.2.2.2, 00:05:00, GigabitEthernet0/1 O N2 192.168.1.0/24 [110/20] via 10.2.2.1, 00:10:14, GigabitEthernet0/1 O N1 192.168.2.0/24 [110/22] via 10.2.2.2, 00:03:43, GigabitEthernet0/1 RTB#
此网络图用于此案例:
将 OSPF 重新分发到 BGP 拓扑 B
网络图显示路由器 B 同时接收 OSPF N1 和 N2 路由。如果仅使用nssa-external关键字,则默认行为是重新分配N1和N2路由。此配置允许RTB将OSPF N2(192.168.1.0/24)和OSPF N1(192.168.2.0/24)路由重分发到BGP:
| RTB |
|---|
|
在路由器 B 上进行配置更改后,它会重新分发 OSPF NSSA 外部路由,路由器 C 会从 BGP 获知 OSPF NSSA 外部路由:
RTB#show ip route Codes: L - local, 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, H - NHRP, l - LISP a - application route + - replicated route, % - next hop override, p - overrides from PfR Gateway of last resort is not set 10.0.0.0/8 is variably subnetted, 6 subnets, 2 masks C 10.2.2.0/24 is directly connected, GigabitEthernet0/1 L 10.2.2.3/32 is directly connected, GigabitEthernet0/1 C 10.3.3.0/24 is directly connected, GigabitEthernet0/0 L 10.3.3.1/32 is directly connected, GigabitEthernet0/0 O IA 10.108.1.0/24 [110/3] via 10.2.2.2, 00:09:40, GigabitEthernet0/1 O 10.108.2.0/24 [110/2] via 10.2.2.2, 00:09:40, GigabitEthernet0/1 O N2 192.168.1.0/24 [110/20] via 10.2.2.1, 00:14:54, GigabitEthernet0/1 O N1 192.168.2.0/24 [110/22] via 10.2.2.2, 00:08:23, GigabitEthernet0/1 RTB# RTB#show ip bgp BGP table version is 17, local router ID is 10.3.3.1 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, t secondary path, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found Network Next Hop Metric LocPrf Weight Path *> 192.168.1.0 10.2.2.1 20 32768 ? *> 192.168.2.0 10.2.2.2 22 32768 ? RTB#RTC#show ip route Codes: L - local, 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, H - NHRP, l - LISP a - application route + - replicated route, % - next hop override, p - overrides from PfR Gateway of last resort is not set 10.0.0.0/8 is variably subnetted, 2 subnets, 2 masks C 10.3.3.0/24 is directly connected, GigabitEthernet0/0 L 10.3.3.2/32 is directly connected, GigabitEthernet0/0 B 192.168.1.0/24 [20/20] via 10.3.3.1, 00:01:29 B 192.168.2.0/24 [20/22] via 10.3.3.1, 00:01:29 RTC# RTC#show ip bgp BGP table version is 41, local router ID is 10.3.3.2 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, t secondary path, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found Network Next Hop Metric LocPrf Weight Path *> 192.168.1.0 10.3.3.1 20 0 100 ? *> 192.168.2.0 10.3.3.1 22 0 100 ? RTC#
与 OSPF 外部路由相同,要仅重新分发 OSPF N1 路由,请在路由器 B 上的路由器 BGP 100 下输入以下命令:
RTB(config)#router bgp 100
RTB(config-router)#redistribute ospf 1 match nssa-external 1
!--- This redistributes only OSPF NSSA-external Type-1 routes into BGP.
要仅重分布OSPF N2路由,请在路由器B的router bgp 100下输入以下命令:
RTB(config)#router bgp 100redistribute ospf 1 match nssa-external 2
RTB(config-router)#
!--- This redistributes only OSPF NSSA-external Type-2 routes into BGP.
请务必了解连续的配置更改会给配置带来哪些改变。带有 match 选项的新命令不会覆盖之前的命令,而是会添加到之前的命令中。下一个示例说明配置命令序列如何影响重新分发:
R4#configure terminal R4(config)#router bgp 100 R4(config-router)#redistribute ospf 1 match internal R4(config-router)#^Z !--- Initially, you redistribute internal OSPF routes into BGP 100. R4#show run | include redistribute ospf redistribute ospf 1 match internal R4#configure terminal R4(config)#router bgp 100 R4(config-router)#redistribute ospf 1 match external R4(config-router)#^Z !--- With this second command, you tell BGP to also redistribute external OSPF routes. R4#show run | include redistribute ospf redistribute ospf 1 match internal external 1 external 2 R4# R4#configure terminal R4(config)#router bgp 100 R4(config-router)#no redistribute ospf 1 match external 2 R4(config-router)#^Z !--- With this no command, you only disable the redistribution of external type 2 into BGP.
!--- All other types of routes previously configured remain. R4#show run | include redistribute ospf redistribute ospf 1 match internal external 1 !--- As you can see, internal and external type 1 remain. R4#configure terminal R4(config)#router bgp 100 R4(config-router)#no redistribute ospf 1 match internal external 1 R4(config-router)#^Z !--- This no command removes all explicit match options but does not disable redistribution
!--- The remaining redistribute ospf 1 command uses the default behavior and redistributes
!--- internal routes only. R4#show run | include redistribute ospf redistribute ospf 1 R4#configure terminal R4(config)#router bgp 100 R4(config-router)#no redistribute ospf 1 !--- Use no redistribute ospfcommand in order to disable OSPF-to-BGP redistribution completely. R4(config-router)# ^Z R4#show run | include redistribute ospf R4#
路由重分布将通过一种协议获知的路由传播到另一种路由协议。将BGP重分发到IGP中时,默认情况下仅重分发eBGP获知的路由。路由器上已知的iBGP获知的路由不会引入IGP中,以防止创建路由环路。
默认情况下,已禁用将 iBGP 重新分发到 IGP 中。运行bgp redistribute-internal命令以启用iBGP路由到IGP的重分发。需要采取预防措施以便将使用路由映射的特定路由重新分发到 IGP 中。
将 iBGP 路由重新分发到 OSPF 中的示例配置如下所示:
Router(config)#router bgp 65345 Router(config-router)#bgp redistribute-internal ! Router(config)#router ospf 100 Router(config-router)#redistribute bgp 65345 subnets
警告:将iBGP获知的路由重分配到IGP中会在自治系统内创建路由环路。仅使用路由映射重新分发明确批准的前缀。
要将OSPF获取的默认路由重分发到BGP,请在路由映射中仅允许0.0.0.0/0,将路由映射应用到OSPF重分发,并启用使用default-information originate重分发默认路由:
! route-map map_default_only permit 10 match ip address acl_default_only ! ip access-list standard acl_default_only permit 0.0.0.0 ! router bgp 64601 redistribute ospf 1 route-map map_default_only default-information originate !
!--- Distributes the default route in bgp
network 0.0.0.0命令是单独的发起方法,需要在路由表中提供准确的默认路由:
!
router bgp 64601
network 0.0.0.0
!
配置后,使用show ip bgp 0.0.0.0检验默认路由,并使用show ip bgp neighbors <neighbor-address> advertised-routes检验其通告。
注意:BGP通常不重置会话就通告新路由。要请求即时、无中断的更新交换,请运行clear ip bgp * soft命令。此命令用于保持BGP会话的建立。
| 版本 | 发布日期 | 备注 |
|---|---|---|
3.0 |
08-Sep-2026
|
重新认证 — 更新的SEO和格式 |
2.0 |
22-Apr-2025
|
标题、标题和格式。 |
1.0 |
11-Oct-2001
|
初始版本 |