技术教程 · 2026年5月24日 · VirtLab

路由协议实战:OSPF、BGP、MPLS 在仿真平台中的配置

深入讲解 OSPF、BGP、MPLS 等核心路由协议在 EVE-NG 和 GNS3 中的配置方法与实验设计。

路由协议拓扑示意

路由协议实战:OSPF、BGP、MPLS 在仿真平台中的配置

路由协议是网络技术的核心,掌握主流路由协议的配置对于网络工程师至关重要。本文将详细介绍 OSPF、BGP、MPLS 在主流仿真平台中的配置方法。

路由协议概述

路由协议分类

                    路由协议

         ┌─────────────┼─────────────┐
         │             │             │
      有类路由      无类路由      特殊协议
    (RIPv1)      (RIPv2/OSPF/     (MPLS/
                  IS-IS/BGP)       Segment Routing)
         │             │             │
    ┌────┴────┐   ┌────┴────┐    ┌───┴───┐
    │         │   │         │    │       │
  距离矢量  链路状态  路径矢量  标签交换
  (RIP)     (OSPF/    (BGP)   
            IS-IS)

OSPF 配置实战

OSPF 基本概念

OSPF(Open Shortest Path First)是一种基于链路状态的内部网关协议,广泛应用于企业网络。

实验拓扑

          Area 0 (Backbone)
    ┌─────────────────────────────┐
    │                             │
[R1]──────────────[R2]──────────[R3]
    │             │               │
    │ Area 1      │ Area 0        │ Area 2
    │             │               │
[SW1]          [SW2]            [SW3]
    │             │               │
  VLAN 10      VLAN 20          VLAN 30

EVE-NG 配置步骤

R1 配置:

! 基本配置
hostname R1
!
interface GigabitEthernet0/0
 description to-R2
 ip address 10.1.12.1 255.255.255.0
 no shutdown
!
interface GigabitEthernet0/1
 description to-SW1
 ip address 192.168.10.1 255.255.255.0
 no shutdown
!
! OSPF 配置
router ospf 1
 router-id 1.1.1.1
 log-adjacency-changes
 network 10.1.12.0 0.0.0.255 area 0
 network 192.168.10.0 0.0.0.255 area 1
!
line con 0
 logging synchronous
 login
line vty 0 4
 login

R2 配置:

hostname R2
!
interface GigabitEthernet0/0
 description to-R1
 ip address 10.1.12.2 255.255.255.0
 no shutdown
!
interface GigabitEthernet0/1
 description to-R3
 ip address 10.1.23.2 255.255.255.0
 no shutdown
!
router ospf 1
 router-id 2.2.2.2
 log-adjacency-changes
 network 10.1.12.0 0.0.0.255 area 0
 network 10.1.23.0 0.0.0.255 area 0
!
line con 0
 logging synchronous
 login

R3 配置:

hostname R3
!
interface GigabitEthernet0/0
 description to-R2
 ip address 10.1.23.3 255.255.255.0
 no shutdown
!
interface GigabitEthernet0/1
 description to-SW3
 ip address 192.168.30.1 255.255.255.0
 no shutdown
!
router ospf 1
 router-id 3.3.3.3
 log-adjacency-changes
 network 10.1.23.0 0.0.0.255 area 0
 network 192.168.30.0 0.0.0.255 area 2
!
line con 0
 logging synchronous
 login

OSPF 高级特性

多区域 OSPF 特殊区域

Stub 区域配置:

! R1 配置为 Stub 区域
router ospf 1
 area 1 stub
!

! R2(ABR)配置 Stub 区域
router ospf 1
 area 1 stub

NSSA 区域配置:

! R1 配置为 NSSA
router ospf 1
 area 1 nssa
!

! R2 配置 NSSA
router ospf 1
 area 1 nssa

OSPF 路由汇总

! 在 ABR 上汇总区域路由
router ospf 1
 area 1 range 192.168.0.0 255.255.0.0

! 在 ASBR 上汇总外部路由
summary-address 172.16.0.0 255.255.0.0

OSPF 认证

! 接口级别认证
interface GigabitEthernet0/0
 ip ospf authentication message-digest
 ip ospf message-digest-key 1 md5 cisco123

验证命令

! 查看 OSPF 邻居关系
show ip ospf neighbor

! 查看 OSPF 数据库
show ip ospf database

! 查看路由表
show ip route ospf

! 查看 OSPF 接口
show ip ospf interface

BGP 配置实战

BGP 基本概念

BGP(Border Gateway Protocol)是互联网的核心路由协议,用于 AS 之间的路由传递。

实验拓扑

        AS 65001              AS 65002
   ┌──────────────┐      ┌──────────────┐
   │              │  EBGP│              │
[R1]────────────[R2]──────[R3]────────────[R4]
   │              │      │              │
   └──────────────┘      └──────────────┘

IBGP 配置(AS 65001)

R1 配置:

hostname R1
!
interface Loopback0
 ip address 1.1.1.1 255.255.255.255
!
interface GigabitEthernet0/0
 ip address 10.1.12.1 255.255.255.0
 no shutdown
!
router bgp 65001
 bgp router-id 1.1.1.1
 neighbor 2.2.2.2 remote-as 65001
 neighbor 2.2.2.2 update-source Loopback0
 !
 address-family ipv4
  network 1.1.1.1 mask 255.255.255.255
  network 192.168.10.0 mask 255.255.255.0
 exit-address-family
!
ip route 2.2.2.2 255.255.255.255 10.1.12.2

R2 配置:

hostname R2
!
interface Loopback0
 ip address 2.2.2.2 255.255.255.255
!
interface GigabitEthernet0/0
 ip address 10.1.12.2 255.255.255.0
 no shutdown
!
interface GigabitEthernet0/1
 ip address 10.1.23.2 255.255.255.0
 no shutdown
!
router bgp 65001
 bgp router-id 2.2.2.2
 neighbor 1.1.1.1 remote-as 65001
 neighbor 1.1.1.1 update-source Loopback0
 neighbor 3.3.3.3 remote-as 65002
 neighbor 3.3.3.3 update-source Loopback0
 ebgp-multihop 2
!
ip route 1.1.1.1 255.255.255.255 10.1.12.1
ip route 3.3.3.3 255.255.255.255 10.1.23.3

EBGP 配置(AS 65002)

R3 配置:

hostname R3
!
interface Loopback0
 ip address 3.3.3.3 255.255.255.255
!
interface GigabitEthernet0/0
 ip address 10.1.23.3 255.255.255.0
 no shutdown
!
interface GigabitEthernet0/1
 ip address 10.1.34.3 255.255.255.0
 no shutdown
!
router bgp 65002
 bgp router-id 3.3.3.3
 neighbor 2.2.2.2 remote-as 65001
 neighbor 2.2.2.2 update-source Loopback0
 neighbor 2.2.2.2 ebgp-multihop 2
 neighbor 4.4.4.4 remote-as 65002
 neighbor 4.4.4.4 update-source Loopback0
 !
 address-family ipv4
  network 3.3.3.3 mask 255.255.255.255
  network 192.168.30.0 mask 255.255.255.0
 exit-address-family
!
ip route 2.2.2.2 255.255.255.255 10.1.23.2
ip route 4.4.4.4 255.255.255.255 10.1.34.4

BGP 路由策略

路由过滤

! 使用 prefix-list 过滤
ip prefix-list BLOCK_THIS seq 5 deny 192.168.100.0/24
ip prefix-list BLOCK_THIS seq 10 permit 0.0.0.0/0 le 32
!
router bgp 65001
 neighbor 2.2.2.2 prefix-list BLOCK_THIS in

路由映射(Route Map)

route-map SET_LOCAL_PREF permit 10
 match ip address prefix-list MY_ROUTES
 set local-preference 200
!
router bgp 65001
 neighbor 2.2.2.2 route-map SET_LOCAL_PREF in

BGP 验证

! 查看 BGP 邻居
show ip bgp neighbors

! 查看 BGP 路由表
show ip bgp

! 查看 BGP 汇总
show ip bgp summary

! 跟踪路由传播路径
traceroute 192.168.20.1

MPLS 配置实战

MPLS 基本概念

MPLS(Multi-Protocol Label Switching)是一种高性能的分组交换技术,通过标签实现快速转发。

MPLS 实验拓扑

    ┌─────────────────────────────────────────┐
    │               SP Network                │
    │  ┌─────┐    ┌─────┐    ┌─────┐    ┌─────┐ │
CE1 │P1│  │P2│  │P3│  │P4│ CE2
    │  └──┬─┘    └─┬─┘    └─┬─┘    └──┬──┘ │
    └────│─────────┼─────────┼────────│────┘
         │         │         │         │
    ┌────┴─────────┴─────────┴─────────┴────┐
    │              Customer Network         │
    └───────────────────────────────────────┘

MPLS 基础配置

P1(Provider Router 1):

hostname P1
!
ip cef
!
mpls label protocol ldp
mpls ldp router-id Loopback0 force
!
interface Loopback0
 ip address 10.1.1.1 255.255.255.255
!
interface GigabitEthernet0/0
 description to-P2
 ip address 10.1.12.1 255.255.255.0
 mpls ip
 no shutdown
!
interface GigabitEthernet0/1
 description to-CE1
 ip address 192.168.1.1 255.255.255.252
 no shutdown

P2(Provider Router 2):

hostname P2
!
ip cef
!
mpls label protocol ldp
mpls ldp router-id Loopback0 force
!
interface Loopback0
 ip address 10.2.2.2 255.255.255.255
!
interface GigabitEthernet0/0
 description to-P1
 ip address 10.1.12.2 255.255.255.0
 mpls ip
 no shutdown
!
interface GigabitEthernet0/1
 description to-P3
 ip address 10.2.23.2 255.255.255.0
 mpls ip
 no shutdown
!
interface GigabitEthernet0/2
 description to-CE2
 ip address 192.168.2.1 255.255.255.252
 no shutdown

MPLS VPN(VRF)配置

配置 VRF:

! P1 上的 VRF 配置
ip vrf CUSTOMER_A
 rd 65001:100
 route-target export 65001:100
 route-target import 65001:100
!
interface GigabitEthernet0/1
 ip vrf forwarding CUSTOMER_A
 ip address 192.168.1.1 255.255.255.252
!
! OSPF 作为 VPN 路由协议
router ospf 2 vrf CUSTOMER_A
 network 192.168.1.0 0.0.0.3 area 0

PE-CE 路由交换:

! BGP VPNv4 地址族配置
router bgp 65001
 address-family vpnv4
  neighbor 10.3.3.3 activate
  neighbor 10.3.3.3 send-community extended
 exit-address-family

MPLS 验证

! 查看 MPLS 转发表
show mpls forwarding-table

! 查看 LDP 邻居
show mpls ldp neighbor

! 查看 MPLS 接口
show mpls interfaces

! 测试 MPLS 转发
mpls traceroute 10.4.4.4

综合实验设计

实验 1:企业网络路由综合

┌─────────────────────────────────────────────────┐
│                 Internet                          │
└─────────────────────┬───────────────────────────┘

                 ┌────┴────┐
                 │   BGP   │
                 │  AS 65000│
                 └────┬────┘

         ┌────────────┼────────────┐
         │            │            │
    ┌────┴────┐  ┌────┴────┐  ┌────┴────┐
    │  OSPF   │  │  OSPF   │  │  OSPF   │
    │ Area 0  │  │ Area 1  │  │ Area 2  │
    └────┬────┘  └────┬────┘  └────┬────┘
         │            │            │
      [Core]       [Dist1]      [Dist2]
         │            │            │
      VLANs        VLANs        VLANs

实验 2:MPLS VPN 跨域

    ┌─────────────────────────────────────────┐
    │           MPLS Core Network              │
    │                                          │
    Site A ── PE1 ── P ── PE2 ── Site B       │
    │             │       │          │         │
    │ VRF-A       │       │      VRF-B        │
    └─────────────┴───────┴──────────────────┘

常见问题与排查

问题原因解决方案
OSPF 邻居无法建立区域 ID 不匹配统一区域 ID
BGP 邻居状态 Idle路由不可达检查 IGP 或静态路由
MPLS 标签未分配LDP 未启用在接口下启用 mpls ip
路由不通ACL 阻塞检查访问列表

总结

通过本文的学习,您已经掌握了 OSPF、BGP、MPLS 在主流仿真平台中的配置方法。建议在 EVE-NG 或 GNS3 中多实践这些配置,加深对路由协议工作原理的理解。

#OSPF #BGP #MPLS #路由协议 #网络实验