VRF间流量转发

本文详细记录了三种实现VRF间路由泄露的方法及其测试效果。通过静态泄露、RT相互导入和策略路由的方式,实现了不同VRF间的网络可达性,为多VRF环境下网络通信提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验拓扑:

在这里插入图片描述

初始配置

R1:

interface Ethernet0/0
 ip vrf forwarding vrf1
 ip address 192.168.1.254 255.255.255.0
!
interface Ethernet0/1
 ip vrf forwarding vrf2
 ip address 192.168.2.254 255.255.255.0
!

PC1:

interface Ethernet0/0
 ip address 192.168.1.1 255.255.255.0
!
ip route 0.0.0.0 0.0.0.0 192.168.1.254

PC2:

interface Ethernet0/0
 ip address 192.168.2.1 255.255.255.0
!
ip route 0.0.0.0 0.0.0.0 192.168.2.254

方式一:静态泄漏

R1:

ip route vrf vrf1 192.168.2.0 255.255.255.0 Ethernet0/1 192.168.2.1
ip route vrf vrf2 192.168.1.0 255.255.255.0 Ethernet0/0 192.168.1.1

方式一测试

R1#show ip route vrf vrf1

Routing Table: vrf1
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
       + - replicated route, % - next hop override

Gateway of last resort is not set

      192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks
C        192.168.1.0/24 is directly connected, Ethernet0/0
L        192.168.1.254/32 is directly connected, Ethernet0/0
S     192.168.2.0/24 [1/0] via 192.168.2.1, Ethernet0/1
R1#show ip route vrf vrf2

Routing Table: vrf2
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
       + - replicated route, % - next hop override

Gateway of last resort is not set

S     192.168.1.0/24 [1/0] via 192.168.1.1, Ethernet0/0
      192.168.2.0/24 is variably subnetted, 2 subnets, 2 masks
C        192.168.2.0/24 is directly connected, Ethernet0/1
L        192.168.2.254/32 is directly connected, Ethernet0/1
R1#
PC1#ping 192.168.2.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.2.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
PC1#ping 192.168.2.254
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.2.254, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
PC1#traceroute 192.168.2.254
Type escape sequence to abort.
Tracing the route to 192.168.2.254
VRF info: (vrf in name/id, vrf out name/id)
  1 192.168.1.254 1 msec 0 msec 0 msec
  2 192.168.2.1 1 msec 0 msec 0 msec
  3 192.168.2.254 1 msec *  1 msec
PC1#

方式二:RT相互导入

R1:

ip vrf vrf1
 rd 1:1
 route-target export 1:1
 route-target import 1:1
 route-target import 2:1
!
ip vrf vrf2
 rd 2:1
 route-target export 2:1
 route-target import 2:1
 route-target import 1:1
!
router bgp 1
 bgp router-id 1.1.1.1
 bgp log-neighbor-changes
 !
 address-family ipv4 vrf vrf1
  redistribute connected
 exit-address-family
 !        
 address-family ipv4 vrf vrf2
  redistribute connected
 exit-address-family
!

查看

R1#show ip route vrf vrf1
Routing Table: vrf1
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
       + - replicated route, % - next hop override

Gateway of last resort is not set

      192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks
C        192.168.1.0/24 is directly connected, Ethernet0/0
L        192.168.1.254/32 is directly connected, Ethernet0/0
      192.168.2.0/24 is variably subnetted, 2 subnets, 2 masks
B        192.168.2.0/24 is directly connected (vrf2), 00:00:17, Ethernet0/1
L        192.168.2.254/32 is directly connected, Ethernet0/1
R1#show ip route vrf vrf2
Routing Table: vrf2
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
       + - replicated route, % - next hop override

Gateway of last resort is not set

      192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks
B        192.168.1.0/24 is directly connected (vrf1), 00:00:34, Ethernet0/0
L        192.168.1.254/32 is directly connected, Ethernet0/0
      192.168.2.0/24 is variably subnetted, 2 subnets, 2 masks
C        192.168.2.0/24 is directly connected, Ethernet0/1
L        192.168.2.254/32 is directly connected, Ethernet0/1
R1#
R1#show ip bgp vpnv4 all 
BGP table version is 5, local router ID is 1.1.1.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, 
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop            Metric LocPrf Weight Path
Route Distinguisher: 1:1 (default for vrf vrf1)
 *>  192.168.1.0      0.0.0.0                  0         32768 ?
 *>  192.168.2.0      0.0.0.0                  0         32768 ?
Route Distinguisher: 2:1 (default for vrf vrf2)
 *>  192.168.1.0      0.0.0.0                  0         32768 ?
 *>  192.168.2.0      0.0.0.0                  0         32768 ?
R1#

方式二测试

PC1#ping 192.168.2.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.2.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
PC1#ping 192.168.2.254
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.2.254, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
PC1#traceroute 192.168.2.1
Type escape sequence to abort.
Tracing the route to 192.168.2.1
VRF info: (vrf in name/id, vrf out name/id)
  1 192.168.1.254 1 msec 0 msec 0 msec
  2 192.168.2.1 1 msec *  1 msec
PC1#traceroute 192.168.2.254
Type escape sequence to abort.
Tracing the route to 192.168.2.254
VRF info: (vrf in name/id, vrf out name/id)
  1 192.168.1.254 0 msec *  1 msec
PC1#

方式三:策略路由

ip access-list extended vrf1-to-vrf2
 permit ip 192.168.1.0 0.0.0.255 192.168.2.0 0.0.0.255
ip access-list extended vrf2-to-vrf1
 permit ip 192.168.2.0 0.0.0.255 192.168.1.0 0.0.0.255
!
!
route-map vrf1-to-vrf2 permit 10
 match ip address vrf1-to-vrf2
 set ip vrf vrf2 next-hop 192.168.2.1
!
route-map vrf2-to-vrf1 permit 10
 match ip address vrf2-to-vrf1
 set ip vrf vrf1 next-hop 192.168.1.1
!
interface Ethernet0/0
 ip vrf forwarding vrf1
 ip address 192.168.1.254 255.255.255.0
 ip policy route-map vrf1-to-vrf2
!
interface Ethernet0/1
 ip vrf forwarding vrf2
 ip address 192.168.2.254 255.255.255.0
 ip policy route-map vrf2-to-vrf1
!

方式三测试

PC1#ping 192.168.2.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.2.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
PC1#
PC1#traceroute 192.168.2.1
Type escape sequence to abort.
Tracing the route to 192.168.2.1
VRF info: (vrf in name/id, vrf out name/id)
  1 192.168.1.254 0 msec 1 msec 0 msec
  2 192.168.2.1 0 msec *  1 msec
PC1#
### VRF (Virtual Routing and Forwarding) 配置与文档 #### Linux 内核中的 VRF 实现 VRF(虚拟路由转发)是由路由表和一组网络接口组成的逻辑实体,用于隔离不同租户或服务之流量。通过创建独立的路由空,可以有效地管理多个不同的路由环境而不互相干扰[^1]。 对于 Linux 系统而言,在内核层面支持 VRF 功能意味着可以通过特定命令来管理和操作这些虚拟化的路由实例。例如,`ip link set dev eth1 master vrf-blue` 命令能够将第三层接口绑定至指定名称为 `vrf-blue` 的 VRF 设备上[^3]。 当涉及到实际的数据传输过程中,Linux 内核会依据接收到或者发出的数据包所对应的物理网卡属性自动判断其归属哪个具体的 VRF 范围,并执行相应的处理流程。比如发送数据时,`l3mdev_ip_out()` 函数负责设置目的地址解析结构体 (`dst_entry`) 并最终调用 `vrf_xmit()` 来完成报文的实际传送工作[^4]。 #### 技术文档资源推荐 为了更深入理解如何配置以及优化基于 VRF 构建的企业级解决方案,建议查阅官方手册和其他权威资料: - **《Linux Networking Documentation》**:提供有关于整个操作系统网络子系统的全面介绍,其中包含了关于 VRF 特性的描述及其应用场景说明。 - **Red Hat Enterprise Linux Network Guide**:针对企业版发行版本编写的操作指南,详细记录了各种高级特性如 VRF 的部署方法和技术细节。 - **Cisco IOS XE Virtual Routing and Forwarding Configuration Guide**:虽然主要面向思科产品线,但对于通用概念的理解同样具有很高的参考价值。 ```bash # 创建一个新的 VRF 接口并分配给它一个 IP 地址 ip netns add blue ip -n blue link set lo up ip -n blue addr add 192.0.2.1/24 dev lo ip link add name vrf-blue type vrf table 100 ip link set dev vrf-blue up ip link set dev eth1 master vrf-blue ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值