Multi-Protocol Label Switching (MPLS) 多协定标签交换

前言

MPLS (Multi-Protocol Label Switching) 的概念是在一个封包之中加入 Label (标签),所谓 Label,是加在 Layer 2 Header 与 Layer3 Header 之间的一串 32 bits 标示 (所以 MPLS 又称为 Layer 2.5),其中 20bits 为 Label 值。路由器会依据这个 Label 值去判断封包的 Next Hop,用了这个方法,路由器不用再查找 Route Table 便可以决定怎样处理封包,传送效率便可大大提升,此技术於 ISP 的 Backbone Network 中被广泛使用。

传统 Routing 处理

现在使用以下的网络作例子,解释传统 Routing 的处理:

mpls

假设所有 Router 已经设定好 OSPF 作为 Routing Protocol。R1 的 L0:1.1.1.1 能够 PING 通 R6 的 L0:6.6.6.6,但究竟每只 Router 中间发生了什麽事呢?

R1#ping 6.6.6.6 source 1.1.1.1
 
 Type escape sequence to abort.
 Sending 5, 100-byte ICMP Echos to 6.6.6.6, timeout is 2 seconds:
 Packet sent with a source address of 1.1.1.1
 !!!!!
 Success rate is 100 percent (5/5), round-trip min/avg/max = 168/193/232 ms

当 R1 想 PING 6.6.6.6,它会查看自己的 Route Table,找最合适 (Longest Match) 的一条 Route 到达 6.6.6.6,发现 Next Hop 是 192.168.12.2。

R1#show ip route | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.12.0/24 is directly connected, Ethernet0/0
      1.0.0.0/24 is subnetted, 1 subnets
 C       1.1.1.0 is directly connected, Loopback0
 O    192.168.45.0/24 [110/40] via 192.168.12.2, 00:21:43, Ethernet0/0
      6.0.0.0/32 is subnetted, 1 subnets
 O       6.6.6.6 [110/51] via 192.168.12.2, 00:21:43, Ethernet0/0
 O    192.168.56.0/24 [110/50] via 192.168.12.2, 00:21:43, Ethernet0/0
 O    192.168.23.0/24 [110/20] via 192.168.12.2, 00:21:43, Ethernet0/0
 O    192.168.34.0/24 [110/30] via 192.168.12.2, 00:21:43, Ethernet0/0

这时候 R1 会再做一次查找 Route Table 的动作去找出到达 192.168.12.2 的 Route。然後把封包丢出 Ethernet0/0,即是连接 R2 的 Interface。

R1#show ip route | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.12.0/24 is directly connected, Ethernet0/0
      1.0.0.0/24 is subnetted, 1 subnets
 C       1.1.1.0 is directly connected, Loopback0
 O    192.168.45.0/24 [110/40] via 192.168.12.2, 00:21:43, Ethernet0/0
      6.0.0.0/32 is subnetted, 1 subnets
 O       6.6.6.6 [110/51] via 192.168.12.2, 00:21:43, Ethernet0/0
 O    192.168.56.0/24 [110/50] via 192.168.12.2, 00:21:43, Ethernet0/0
 O    192.168.23.0/24 [110/20] via 192.168.12.2, 00:21:43, Ethernet0/0
 O    192.168.34.0/24 [110/30] via 192.168.12.2, 00:21:43, Ethernet0/0

当 R2 收到这个封包,也做着相同的动作,查看自己的 Route Table,再丢到 R3,如此类推。

R2#show ip route | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.12.0/24 is directly connected, Ethernet0/1
      1.0.0.0/32 is subnetted, 1 subnets
 O       1.1.1.1 [110/11] via 192.168.12.1, 00:27:23, Ethernet0/1
 O    192.168.45.0/24 [110/30] via 192.168.23.3, 00:27:23, Ethernet0/0
      6.0.0.0/32 is subnetted, 1 subnets
 O       6.6.6.6 [110/41] via 192.168.23.3, 00:27:23, Ethernet0/0
 O    192.168.56.0/24 [110/40] via 192.168.23.3, 00:27:23, Ethernet0/0
 C    192.168.23.0/24 is directly connected, Ethernet0/0
 O    192.168.34.0/24 [110/20] via 192.168.23.3, 00:27:23, Ethernet0/0

就是这样,在路径上的每一个 Router 都要查看 Routing Table 不只一次,这样会耗用了 Router 的资源,而且传送速度不够快。有些聪明的人类就想到,不如预先设定封包要走的路径,即是预先在封包中加入一个 Label,每个 Router 只要看一看这个 Label 便可决定了要离开的 Interface,不需要再查 Routing Table,这就是 MPLS 的精神了。

设定 MPLS

我们可以在设定好 IGP (OSPF, EIGRP 等等) 之後,再设定 MPLS 透过 IGP 去为 Network 做 Label。但必需注意的是:

MPLS 只能用於 IGP,例如:OSPF,EIGRP 等等。

但设定 MPLS 之前,我们要先计划一下那些 Router 需要执行 MPLS。假设我们希望 R2, R3, R4, R5 使用 MPLS:

mpls

然後我们学几个名词:

LSR

即 Label Switch Router,即是执行了 MPLS 的 Router,上图中 R2, R3, R4, R5 都是 LSR。

Ingress LSR

如果按照刚才的例子,封包是由 R1 到 R6 这个方向的话,R2 是封包进入 MPLS 网络第一个碰到的 Router,称为 Ingress LSR。Ingress Router 主要是帮进入 MPLS 网络的封包加上 Label (这个动作也叫作 Push)。

Egress LSR

当封包将要离开 MPLS 网络时,它经过的最後一个 Router 就是 Egress LSR。Egress LSR 的责任是帮封包移除 Label (也叫作 Pop),让它离开时重过正常生活。从 R1 到 R6 的方向来说,R5 就是 Egress LSR。

Core LSR

除了 Ingress LSR 和 Egress LSR,其他就是 Core LSR 了。由於 Label 是由每个 Router 自己产生的,所以各 Router 对同一个网络所使用的 Label 各有不同,因此,封包每经过一只 Core LSR,都要做一次 Label 转换的动作 (也叫作 Swap)。

现在我们试试设定这个 MPLS 网络,所用的指令简单得你无法相信,就是一个 mpls ip 指令就搞定。

R2(config)#int ethernet 0/0
 R2(config-if)#mpls ip
 R2(config-if)#
R3(config)#interface range ethernet 0/0 - 1
 R3(config-if-range)#mpls ip
 *Mar  1 01:39:17.007: %LDP-5-NBRCHG: LDP Neighbor 192.168.23.2:0 (1) is UP
 R3(config-if-range)#
R4(config)#interface range ethernet 0/0 - 1
 R4(config-if-range)#mpls ip
 *Mar  1 01:40:45.439: %LDP-5-NBRCHG: LDP Neighbor 192.168.34.3:0 (1) is UP
 R4(config-if-range)#
R5(config)#int ethernet 0/1
 R5(config-if)#mpls ip
 *Mar  1 01:42:00.691: %LDP-5-NBRCHG: LDP Neighbor 192.168.45.4:0 (2) is UP
 R5(config-if)#

只要两只 Router 的 Interface 都设定好 MPLS,它们就会成为 LDP Neighbor。LDP 是 MPLS 其中一款 Protocol ,如果不做设定,Cisco Router default 使用 LDP,另一款是 TDP,稍後会再说明。刚才说过就算是同一个 Network ,Label 在每一个 Router 上都是不同的,要成为 Neighbor 是因为这些 LSR 需要交换彼此的 Label 资讯。要看到那一个 Label 代表那一个网络,我们可以用 show mpls forwarding-table ,例如在 R2 的 Label Forwarding Table 可以见到这只 Router 用 Label 16 来代表 192.168.34.0/24,用 Label 17 来代表 192.168.45.0/24。

R2#show mpls forwarding-table
 Local  Outgoing    Prefix            Bytes tag  Outgoing   Next Hop
 tag    tag or VC   or Tunnel Id      switched   interface
 16     Pop tag     192.168.34.0/24   0          Et0/0      192.168.23.3
 17     16          192.168.45.0/24   0          Et0/0      192.168.23.3
 18     18          192.168.56.0/24   0          Et0/0      192.168.23.3
 19     20          6.6.6.6/32        0          Et0/0      192.168.23.3
 20     Untagged    1.1.1.1/32        0          Et0/1      192.168.12.1

不过,每只 LSR 的 Label Forwarding Table 是不相同的,你用 Label 17 代表 192.168.45.0/24 不等於我的 Label 17 代表 192.168.45.0/24,看看 R3 便知道:

R3#show mpls forwarding-table
 Local  Outgoing    Prefix            Bytes tag  Outgoing   Next Hop
 tag    tag or VC   or Tunnel Id      switched   interface
 16     Pop tag     192.168.45.0/24   0          Et0/0      192.168.34.4
 17     Pop tag     192.168.12.0/24   0          Et0/1      192.168.23.2
 18     17          192.168.56.0/24   0          Et0/0      192.168.34.4
 19     20          1.1.1.1/32        0          Et0/1      192.168.23.2
 20     20          6.6.6.6/32        0          Et0/0      192.168.34.4

现在介绍一下每一个栏位的意思:

Local tag

刚刚说过,就是 Label 的意思。Label 长度为 20 bits,Label 0 至 15 是保留不用的,所以 16 至 1,048,575 可用。

Outgoing tag or VC

由於每个 LSR 的 Label Forwarding Table 不相同,所以当  Router 收到一个封包然後要传送出去时,有可能需要更改 Label,称为 Swap。就以上的例子,当 R2 收到 Label 17 目的地为192.168.45.0 /24 的封包时,因为它知道 R3 的 192.168.45.0/24 是 Label 16 (是 LDP 让它知道),所以它会把 Label 17 的封包改成 Label 16 才丢出去 Eth 0/0。

如果此栏是 Untagged,意思是会把所有 Label 移除,因为这个 Network 连接着非 MPLS 的 Interface。用 R2 作例子,因为我们知道 1.1.1.1/32 的 Next Hop 是 R1,而连接着 R1 的 Interface 并无执行 MPLS,所以 R2 会把 Label 移除後才丢出去 Eth0/1。

Pop 的意思也是移除 Label,不过只移除最外层的 Label,有时候比较高阶的设定,会动用到多层 Label,Pop 就是移除最外层的。在 R2 显示 192.168.34.0/24 的封包会被 Pop 後才丢给 R3,为什麽呢?因为 R2 知道 R3 正正连接着 192.168.34.0/24,当然 R2 可以把 Label Swap 成一个新的 Label ,然後传给 R3,再由 R3 查 Label Forwarding Table 後把 Label Pop 掉,最後查 Routing Table 把封包丢出去。但聪明的 R2 选择预先把 Label Pop 掉,然後传给 R3,让它直接查 Routing Table,省了一次查 Label Forwarding Table 的动作,这个聪明的机制叫做 Penultimate Hop Popping (PHP),但这个机制有利有弊,在介绍 explicit-null 时会有更详细的说明。

Prefix or Tunnel Id

在 IP Network 来说就是 Label 所代表的 Network Prefix。

Byte Tag switched

即是传送了多少个 Byte,如果数字有累加的话代表传送成功了。

Outgoing interface

做了 Label 转换动作後 (Pop, Swap, Untagged 等),应从那个 Interface 丢出去。

Next Hop

明显就是 Next Hop IP。

MPLS 的设定是很自动化的,我们甚至不能为 Network 设定 Label 的值,LSR default 会使用 16 至 1,048,575 作为 Label 值,但如你因为某些原因想把范围缩少的话,可以用 mpls label range <min> <max> 这道指令,但必需要 reload 後再生效。

R2(config)#mpls label range 50 100
 % Label range changes will take effect at the next reload.
 R2(config)#exit
 R2#write memory
 R2#reload
 
 <After Reloaded>
 
 R2#show mpls forwarding-table
 Local  Outgoing    Prefix            Bytes tag  Outgoing   Next Hop
 tag    tag or VC   or Tunnel Id      switched   interface
 50     Pop tag     192.168.34.0/24   0          Et0/0      192.168.23.3
 51     16          192.168.45.0/24   0          Et0/0      192.168.23.3
 52     18          192.168.56.0/24   0          Et0/0      192.168.23.3
 53     Untagged    1.1.1.1/32        0          Et0/1      192.168.12.1
 54     20          6.6.6.6/32        0          Et0/0      192.168.23.3

要看 MPLS 把 Label 转换的过程,可以用 debug mpls packets 这道指令,我们用 R3 作示范:

R3#debug mpls packets
 MPLS packet debugging is on

首先用 R1 的 1.1.1.1 Ping R6 的 6.6.6.6:

R1#ping 6.6.6.6 source 1.1.1.1
 
 Type escape sequence to abort.
 Sending 5, 100-byte ICMP Echos to 6.6.6.6, timeout is 2 seconds:
 Packet sent with a source address of 1.1.1.1
 !!!!!
 Success rate is 100 percent (5/5), round-trip min/avg/max = 200/251/316 ms
 R1#

R3 便会收到从 R2 来的封包 (recvd),Label 是 20,根据 R3 的 Label Forwarding Table,Label 2 会 Swap 成 Label 17,然後送出去 (xmit)。

R3#show mpls forwarding-table
 Local  Outgoing    Prefix            Bytes tag  Outgoing   Next Hop
 tag    tag or VC   or Tunnel Id      switched   interface
 16     Pop tag     192.168.12.0/24   1140       Et0/1      192.168.23.2
 17     Pop tag     192.168.45.0/24   1140       Et0/0      192.168.34.4
 18     16          192.168.56.0/24   0          Et0/0      192.168.34.4
 19     19          1.1.1.1/32        1180       Et0/1      192.168.23.2
 20     17          6.6.6.6/32        1180       Et0/0      192.168.34.4
 R3#
 *Mar  1 00:14:39.455: MPLS: Et0/1: recvd: CoS=0, TTL=254, Label(s)=20
 *Mar  1 00:14:39.459: MPLS: Et0/0: xmit: CoS=0, TTL=253, Label(s)=17
 
 <Output Omitted>

如果 R1 Ping 的是 192.168.45.0/24,封包离开 R3 时就会被 Pop 掉 (no label)。

R1#ping 192.168.45.4 source 192.168.12.1
 
 Type escape sequence to abort.
 Sending 5, 100-byte ICMP Echos to 192.168.45.4, timeout is 2 seconds:
 Packet sent with a source address of 192.168.12.1
 !!!!!
 Success rate is 100 percent (5/5), round-trip min/avg/max = 136/162/204 ms
 R1#
R3#
 *Mar  1 00:19:05.859: MPLS: Et0/1: recvd: CoS=0, TTL=254, Label(s)=17
 *Mar  1 00:19:05.863: MPLS: Et0/0: xmit: (no label)

TDP 与 LDP

刚才有提及过 TDP 与 LDP,其实两种都是 MPLS 的 Protocol。TDP (Tag Distribution Protocol) 是 Cisco 独家的,是较早期的 MPLS Protocol。後来 IETF 制订了 LDP (Label Distribution Protocol),现在比较常用,Cisco Router 现在也 default 使用 LDP 了。除非有很特殊的情况 (例如 Cisco 考试……),否则一般都不会刻意使用 TDP 了。

如果想使用 TDP,可以用一个 global 指令 mpls label protocol tdp,这样所有 Interface 都会转用 TDP。这个指令在 Interface 下也可用,变成只有该 Interface 执行 TDP。要知道 Interface 究竟在使用那一种 MPLS Protocol,可以使用 show mpls interfaces

R3#show mpls interfaces
 Interface              IP            Tunnel   Operational
 Ethernet0/0            Yes (ldp)     No       Yes
 Ethernet0/1            Yes (tdp)     No       Yes
R3#

Implicit null 与 Explicit null

还记得刚才曾经提及过 Penultimate Hop Popping (PHP) 吗?用 PHP 需然可以让 Last Hop 的工作量减少,但会产生一个问题。32 bits Label 当中,除了一个 20 bits 的 Label 值之外,还包含了一些 QoS 资讯,Last Hop 可能需要利用这些资讯来对封包进行 QoS 的判断,如果太早 Pop 走 Label ,Last Hop 就无法得到 QoS 资讯。

所谓 PHP,其实是 LSR default 使用了 Implicit null,如果想保留 Label 至 Last Hop,可以使用mpls ldp explicit-null。请先看看 R4 的 Label Forwarding Table,Label 16 来到 R4 应该是被 Pop 然後经 Eth0/0 送到 R5 的,因为 R4 知道 192.168.56.0/24 是直接连着 R5 的。为了求证,在 R4 开 debug mpls packets 用 R1 Ping 192.168.56.6,在 R4 可以确认 Lable 被 Pop。

R4#show mpls forwarding-table
 Local  Outgoing    Prefix            Bytes tag  Outgoing   Next Hop
 tag    tag or VC   or Tunnel Id      switched   interface
 16     Pop tag     192.168.56.0/24   0          Et0/0      192.168.45.5
 17     16          6.6.6.6/32        0          Et0/0      192.168.45.5
 18     Pop tag     192.168.23.0/24   0          Et0/1      192.168.34.3
 19     16          192.168.12.0/24   0          Et0/1      192.168.34.3
 20     19          1.1.1.1/32        0          Et0/1      192.168.34.3
R4#debug mpls packets
 MPLS packet debugging is on
 R4#
 *Mar  1 00:09:13.827: MPLS: Et0/1: recvd: CoS=0, TTL=253, Label(s)=16
 *Mar  1 00:09:13.827: MPLS: Et0/0: xmit: (no label)

现在,我们希望 R4 保留 Label 传给 R5,所以在 R5 下指令 mpls ldp explicit-null ,作用是告诉 Penultimate Hop:「就算我是 Last Hop,请保留 Label 给我!」 然後在 R4 看看 Label Forwarding Table,这时候会发现 Outgoing tag 变成 0 了,而不是 Pop。

R5(config)#mpls ldp explicit-null
R4#show mpls forwarding-table
 Local  Outgoing    Prefix            Bytes tag  Outgoing   Next Hop
 tag    tag or VC   or Tunnel Id      switched   interface
 16     0           192.168.56.0/24   0          Et0/0      192.168.45.5
 17     16          6.6.6.6/32        0          Et0/0      192.168.45.5
 18     Pop tag     192.168.23.0/24   0          Et0/1      192.168.34.3
 19     16          192.168.12.0/24   590        Et0/1      192.168.34.3
 20     19          1.1.1.1/32        0          Et0/1      192.168.34.3

在 R1 Ping 192.168.56.6,证实 R4 传给 R5 的封包会 Swap 成 Label 0,Label 被成功保留了。

R4#
 *Mar  1 00:17:07.879: MPLS: Et0/1: recvd: CoS=0, TTL=253, Label(s)=16
 *Mar  1 00:17:07.883: MPLS: Et0/0: xmit: CoS=0, TTL=252, Label(s)=0

VRF-lite

接下来话题转一转,我们来介绍一下 VRF (Virtual Routing and Fowarding)。VRF 经常与 MPLS 一起使用来实行 MPLS-VPN,但在 MPLS-VPN 教学之前,我们可以先学会简化版,即是 VRF-lite。VRF 可以解释为 「Layer 3 的 VLAN」 技术,VLAN 把一个 LAN 分成很多个 Virtual 的 LAN,而 VRF 能够把一个 Routing Table 分成很多个 Virtual 的 Routing Table 。什麽时候会使用到 VRF-lite 呢?假如你是一间 ISP 的 administrator,这间 ISP 有两位客人都同时跟你的 Router 执行 OSPF Routing Protocol,不幸地,他们都使用相同 subnet 的 IP address,难道你要叫他们其中一位更改 IP subnet 吗?当然不行,这时候 VRF 便大派用场了。请看以下例子。

mpls

上图中 Company RED 与 Company BLUE 都希望使用 172.16.1.0/24 及 172.16.2.0/24 这两个 Network。所以 ISP 的 R1 要动点手脚,首先要用 ip vrf <name> 去设定 VRF。

R1(config)#ip vrf RED
R1(config-vrf)#exit
R1(config)#ip vrf BLUE
R1(config-vrf)#exit

够简单吧!然後把 Interface e0/0 设定成适当的 VRF,在 Interface 使用 ip vrf forwarding <name> 指令便可,不过每次 VRF 有改动之後系统会自动把 IP 移除,因此必需重新输入一次 IP。用同样的方法把 E0/1 和 E0/2 放进 VRF BLUE,E0/3 放进 VRF RED。

R1(config)#interface ethernet 0/0
R1(config-if)#ip vrf forwarding RED
% Interface Ethernet0/0 IP address 192.168.12.1 removed due to enabling VRF RED
R1(config-if)#ip address 192.168.12.1 255.255.255.0

完成後可使用 show ip vrf interface 查证:

R1#show ip vrf interfaces
Interface              IP-Address      VRF                              Protocol
Et0/0                  192.168.12.1    RED                               up
Et0/1                  192.168.13.1    BLUE                              up
Et0/2                  192.168.14.1    BLUE                              up
Et0/3                  192.168.15.1    RED                               up

最後,就执行 OSPF,但要在 Process ID 後补上 VRF 的名。

R1(config)#router ospf 1 vrf RED
 R1(config-router)#network 0.0.0.0 255.255.255.255 area 0
 R1(config)#router ospf 2 vrf BLUE
 R1(config-router)#network 0.0.0.0 255.255.255.255 area 0

而 R2 至 R5,即是客户的 Router 不用执行任何关於 VRF 的指令,只需要照常执行 OSPF 便可以了。

R2(config)#router ospf 1
 R2(config-router)#network 0.0.0.0 255.255.255.255 area 0

待 Route Update 好之後,在 R2 能够到达 R5,R3 能够到达 R4。

R2#show ip route | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.12.0/24 is directly connected, Ethernet0/0
      2.0.0.0/24 is subnetted, 1 subnets
 C       2.2.2.0 is directly connected, Loopback1
 O    192.168.15.0/24 [110/20] via 192.168.12.1, 00:03:21, Ethernet0/0
      5.0.0.0/32 is subnetted, 1 subnets
 O       5.5.5.5 [110/21] via 192.168.12.1, 00:03:21, Ethernet0/0
      172.16.0.0/16 is variably subnetted, 2 subnets, 2 masks
 C       172.16.1.0/24 is directly connected, Loopback0
 O       172.16.2.1/32 [110/21] via 192.168.12.1, 00:03:21, Ethernet0/0
 R2#ping 172.16.2.1 source 172.16.1.1
 
 Type escape sequence to abort.
 Sending 5, 100-byte ICMP Echos to 172.16.2.1, timeout is 2 seconds:
 !!!!!
 
 Success rate is 100 percent (5/5), round-trip min/avg/max = 52/76/96 ms
R3#show ip route | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.13.0/24 is directly connected, Ethernet0/0
 O    192.168.14.0/24 [110/20] via 192.168.13.1, 00:04:24, Ethernet0/0
      3.0.0.0/24 is subnetted, 1 subnets
 C       3.3.3.0 is directly connected, Loopback1
      4.0.0.0/32 is subnetted, 1 subnets
 O       4.4.4.4 [110/21] via 192.168.13.1, 00:04:24, Ethernet0/0
      172.16.0.0/16 is variably subnetted, 2 subnets, 2 masks
 C       172.16.1.0/24 is directly connected, Loopback0
 O       172.16.2.1/32 [110/21] via 192.168.13.1, 00:04:24, Ethernet0/0
 R3#ping 172.16.2.1 source 172.16.1.1
 
 Type escape sequence to abort.
 Sending 5, 100-byte ICMP Echos to 172.16.2.1, timeout is 2 seconds:
 !!!!!
 
 Success rate is 100 percent (5/5), round-trip min/avg/max = 76/99/128 ms

最精彩是 R1 现在有 3 个 Routing Table,分别是 1) 基本的 2) VRF RED 及 3) VRF BLUE,我们可以用 show ip route vrf <name> 来查看 VRF 的 Routing Table。

R1#show ip route | begin Gateway
 Gateway of last resort is not set
 
      1.0.0.0/32 is subnetted, 1 subnets
 C       1.1.1.1 is directly connected, Loopback0
 R1#
 R1#show ip route vrf RED | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.12.0/24 is directly connected, Ethernet0/0
      2.0.0.0/32 is subnetted, 1 subnets
 O       2.2.2.2 [110/11] via 192.168.12.2, 00:04:55, Ethernet0/0
 C    192.168.15.0/24 is directly connected, Ethernet0/3
      5.0.0.0/32 is subnetted, 1 subnets
 O       5.5.5.5 [110/11] via 192.168.15.5, 00:04:55, Ethernet0/3
      172.16.0.0/32 is subnetted, 2 subnets
 O       172.16.1.1 [110/11] via 192.168.12.2, 00:04:55, Ethernet0/0
 O       172.16.2.1 [110/11] via 192.168.15.5, 00:04:55, Ethernet0/3
 R1#
 R1#show ip route vrf BLUE | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.13.0/24 is directly connected, Ethernet0/1
 C    192.168.14.0/24 is directly connected, Ethernet0/2
      3.0.0.0/32 is subnetted, 1 subnets
 O       3.3.3.3 [110/11] via 192.168.13.3, 00:05:13, Ethernet0/1
      4.0.0.0/32 is subnetted, 1 subnets
 O       4.4.4.4 [110/11] via 192.168.14.4, 00:05:13, Ethernet0/2
      172.16.0.0/32 is subnetted, 2 subnets
 O       172.16.1.1 [110/11] via 192.168.13.3, 00:05:13, Ethernet0/1
 O       172.16.2.1 [110/11] via 192.168.14.4, 00:05:13, Ethernet0/2

不算太难吧!无错,OSPF 在 VRF 的设定上比较简单,不过 RIP 和 EIGRP 的设定上就稍有不同。现在请先把所有 Router 的 OSPF 设定删除,我们现在试试在 VRF RED 执行 RIP 让 R2 和 R5 连接。

要在 RIP 使用 VRF,需要在 RIP 设定中使用 address-family 指令,你可以把它想像成 VRF RED 是 RIP 设定下面的其中一个 Sub Group。

R1(config)#router rip
 R1(config-router)#address-family ipv4 vrf RED
 R1(config-router-af)#network 192.168.12.0
 R1(config-router-af)#network 192.168.15.0
 R1(config-router-af)#version 2
 R1(config-router-af)#no auto-summary
 R1(config-router-af)#exit-address-family
 R1(config-router)#

至於 R2 和 R5 只要按照 RIP 的基本设定便可以,下面只显示 R2 的设定:

R2(config)#router rip
 R2(config-router)#network 192.168.12.0
 R2(config-router)#network 172.16.0.0
 R2(config-router)#network 2.0.0.0
 R2(config-router)#version 2
 R2(config-router)#no auto-summary

跟着试试把 R3 和 R4 用 EIGRP。EIGRP 又要怎样玩呢?其实也是用 address-family。不过有一点要注意的是 autonomous (AS) number,由於 address-family 是 EIGRP 里的一个 Sub Group,你必需为它设定一个 AS number,而 R3 和 R4 要与 R1 的 VRF 成为 EIGRP Neighbor,用的 AS number 也是 address-family 里面那个,看一看以下例子就会明白。

R1(config)#router eigrp 1
 R1(config-router)#address-family ipv4 vrf BLUE
 R1(config-router-af)#autonomous-system 10
 R1(config-router-af)#network 192.168.13.0 0.0.0.255
 R1(config-router-af)#network 192.168.14.0 0.0.0.255
 R1(config-router-af)#no auto-summary
 R1(config-router-af)#exit-address-family

除了 Network IP 之外,R3 与 R4 设定基本相同。

R3(config)#router eigrp 10
 R3(config-router)#network 192.168.13.0 0.0.0.255
 R3(config-router)#network 172.16.1.0 0.0.0.255
 R3(config-router)#network 3.3.3.0 0.0.0.255
 R1(config-router)#no auto-summary

完成设定後看 R1 vrf Routing Table,可见 RED 在玩 RIP,而 BLUE 在玩 EIGRP。

R1#show ip route vrf RED | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.12.0/24 is directly connected, Ethernet0/0
      2.0.0.0/24 is subnetted, 1 subnets
 R       2.2.2.0 [120/1] via 192.168.12.2, 00:00:21, Ethernet0/0
 C    192.168.15.0/24 is directly connected, Ethernet0/3
      5.0.0.0/24 is subnetted, 1 subnets
 R       5.5.5.0 [120/1] via 192.168.15.5, 00:00:26, Ethernet0/3
      172.16.0.0/24 is subnetted, 2 subnets
 R       172.16.1.0 [120/1] via 192.168.12.2, 00:00:21, Ethernet0/0
 R       172.16.2.0 [120/1] via 192.168.15.5, 00:00:26, Ethernet0/3
 R1#
 R1#show ip route vrf BLUE | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.13.0/24 is directly connected, Ethernet0/1
 C    192.168.14.0/24 is directly connected, Ethernet0/2
      3.0.0.0/24 is subnetted, 1 subnets
 D       3.3.3.0 [90/409600] via 192.168.13.3, 00:00:32, Ethernet0/1
      4.0.0.0/24 is subnetted, 1 subnets
 D       4.4.4.0 [90/409600] via 192.168.14.4, 00:00:32, Ethernet0/2
      172.16.0.0/24 is subnetted, 2 subnets
 D       172.16.1.0 [90/409600] via 192.168.13.3, 00:00:32, Ethernet0/1
 D       172.16.2.0 [90/409600] via 192.168.14.4, 00:00:32, Ethernet0/2

MPLS VPN

虽然 VRF-lite 可以解决不同客户有相同 IP 的问题,不过,世事多变!如果今次你遇到下图中这个网络,客户 A 与 客户 B 分别连接着 ISP MPLS 网络中的不同 Router,我们又如何把客户的网络接通?这是就要出动真正的 MPLS VPN,加上 MP-BGP 技术,再加上之前所提到的 VRF 才能搞定。读者请先有心理准备,这会是一个颇难的课题。

mpls

MP-BGP

首先假设 MPLS 网络的部份已经设定好,跟着我们需要把 R2 和 R5 用 MP-BGP (Multiprotocol-BGP) 连接起来。平时所使用的 BGP,一般都是用在 IPv4 的路由交换。其实 BGP 除了作 IPv4 路由交换之外,还可以设定成 VPN ,用作交换 VRF 的路由。至於设定的方法在下面会有说明。

Route Distinguisher

另外,我们要知道什麽是 RD (Route Distinguisher) ,由於 RED 和 BLUE 公司都有相同的网络,假设 R2 帮我们把路由发到 R5,但 R5 又怎样分辨那条是 RED 的 172.16.1.0/24,那条是 BLUE 的 172.16.1.0/24 呢?所以,我们要在发出去的 Route 中加点东西作分辨,这就是 RD 的用途。RD 是一个 96 bits 的值,格式通常是 <AS number>:<任意number> 或 <IP>:<任意number>,例如:65000:100 或 172.16.1.0:200。RD 会加在 IP 之前,这样 Router 就可以靠这个 RD 来区分不同来源的 Route。

Route Target

还有还有,我们还要学 RT (Route Target)。套用刚才的例子,R2 把两间公司的 Route 好好的加了 RD,但其实它是可以选择是不是要把这些 Route 发出来,而另一方面 R5 也可以选择要接收那些 Route,这就是 RT 的作用。RT 是一个 64 bits 的值,是 BGP 的 Extended Community (当是一个特别的栏位好了) ,你可以为某个 VRF 的 Route 标好了 RT 然後发出去,也可选择只接收那个 RT 的 Route。现阶段可能还是比较难理解,但相信我,一路看下去就会更加明白。

R5#show ip bgp vpnv4 all 172.16.1.1
 BGP routing table entry for 65000:100:172.16.1.0/24, version 4
 Paths: (1 available, best #1, table RED)
   Not advertised to any peer
   Local
     2.2.2.2 (metric 31) from 2.2.2.2 (2.2.2.2)
       Origin incomplete, metric 1, localpref 100, valid, internal, best
       Extended Community: RT:100:100
       mpls labels in/out nolabel/21

好!现在我们尝试为上面的网络配置 MPLS VPN,过程相当烦复,不要紧,我们一步一步做下去。假设黄色部份 ISP 的网络已经配置好 OSPF,并且执行了 MPLS。

步骤 1:设置 VRF

跟 VRF-lite 一样是用 ip vrf <name> 指令,但不同的是我们要为 VRF 设 RD 和 RT。

rd <num>:<num> 设定 RD

route-target export <num>:<num> 设定 RT,export 表示这个 VRF 的 Route 会用 100:100 发送出去,import 即是这个 VRF 会接收 100:100 的 Route,如果 import 和 export 的 RT 相同,乾脆用 both 便可以了。

R2(config)#ip vrf RED
 R2(config-vrf)#rd 65000:100
 R2(config-vrf)#route-target export 100:100
 R2(config-vrf)#route-target import 100:100
 R2(config-vrf)#ip vrf BLUE
 R2(config-vrf)#rd 65000:200
 R2(config-vrf)#route-target both 200:200
R5(config)#ip vrf RED
 R5(config-vrf)#rd 65000:100
 R5(config-vrf)#route-target both 100:100
 R5(config-vrf)#ip vrf BLUE
 R5(config-vrf)#rd 65000:200
 R5(config-vrf)#route-target both 200:200

然後把 VRF 放置於适当的 Interface,设定方法之前说过,在此不详述,只显示结果:

R2#show ip vrf interfaces
 Interface              IP-Address      VRF                              Protocol
 Et0/1                  192.168.12.2    BLUE                             up
 Et0/2                  192.168.27.2    RED                              up
R5#show ip vrf interfaces
 Interface              IP-Address      VRF                              Protocol
 Et0/0                  192.168.56.5    BLUE                             up
 Et0/2                  192.168.58.5    RED

步骤 2:设置 MP-BGP

刚才说过我们要把 R2 和 R5 设定 MP-BGP,首先建好基本的 BGP Neighbor:

hostname R2
 !
 router bgp 65000
  no synchronization
  neighbor 5.5.5.5 remote-as 65000
  neighbor 5.5.5.5 update-source Loopback0
  no auto-summary
hostname R5
 !
 router bgp 65000
  no synchronization
  neighbor 2.2.2.2 remote-as 65000
  neighbor 2.2.2.2 update-source Loopback0
  no auto-summary

然後用 address-family vpnv4 进入 address family mode 进行设定:

neighbor <IP> activate 是告知使用那个 neighbor IP 来进行 vpnv4

neighbor <IP> send-community 是告诉这个 vpnv4 需要传送 community (即是刚刚说的 RT 就是其中一种 community 啦)

R2(config)#router bgp 65000
 R2(config-router)#address-family vpnv4
 R2(config-router-af)#neighbor 5.5.5.5 activate
 R2(config-router-af)#neighbor 5.5.5.5 send-community
 R2(config-router-af)#exit-address-family
R5(config)#router bgp 65000
 R5(config-router)#address-family vpnv4
 R5(config-router-af)#neighbor 2.2.2.2 activate
 R5(config-router-af)#neighbor 2.2.2.2 send-community
 R5(config-router-af)#exit-address-family

步骤 3:RED 的 RIP 设定

然後我们帮 RED 公司设定 RIP,此部份在 VRF-lite 已说明过,在此不加以详述。以下为 R2 和 R7 的 RIP 设定:

hostname R2
 !
 router rip
  !
  address-family ipv4 vrf RED
   network 192.168.27.0
   no auto-summary
   version 2
  exit-address-family
hostname R7
 !
 router rip
  version 2
  network 172.16.0.0
  network 192.168.27.0
  no auto-summary

以下是 R5 和 R8 的 RIP 设定:

hostname R5
 !
 router rip
  !
  address-family ipv4 vrf RED
   network 192.168.58.0
   no auto-summary
   version 2
  exit-address-family
hostname R8
 !
 router rip
  version 2
  network 172.16.0.0
  network 192.168.58.0
  no auto-summary

所以 R2 和 R5 都收到从 RIP 传来的 Route 了。

R2#show ip route vrf RED | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.27.0/24 is directly connected, Ethernet0/2
      172.16.0.0/24 is subnetted, 1 subnets
 R       172.16.1.0 [120/1] via 192.168.27.7, 00:00:20, Ethernet0/2
R5#show ip route vrf RED | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.58.0/24 is directly connected, Ethernet0/2
      172.16.0.0/24 is subnetted, 1 subnets
 R       172.16.2.0 [120/1] via 192.168.58.8, 00:00:03, Ethernet0/2

步骤 4:BLUE 的 EIGRP 设定

然後我们帮 BLUE 公司设定 EIGRP,此部份也在 VRF-lite 已说明过,在此不加以详述。以下为 R1 和 R2 的 EIGRP 设定:

R2(config)#router eigrp 1
 R2(config-router)#address-family ipv4 vrf BLUE
 R2(config-router-af)#autonomous-system 20
 R2(config-router-af)#network 192.168.12.0 0.0.0.255
 R2(config-router-af)#no auto-summary
R1(config)#router eigrp 20
 R1(config-router)#network 192.168.12.0 0.0.0.255
 R1(config-router)#network 172.16.1.0 0.0.0.255
 R1(config-router)#no auto-summary

以下为 R5 和 R6 的 EIGRP 设定:

R5(config)#router eigrp 1
 R5(config-router)#address-family ipv4 vrf BLUE
 R5(config-router-af)#autonomous-system 20
 R5(config-router-af)#network 192.168.56.0 0.0.0.255
 R5(config-router-af)#no auto-summary
R6(config)#router eigrp 20
 R6(config-router)#network 192.168.56.0 0.0.0.255
 R6(config-router)#network 172.16.2.0 0.0.0.255
 R6(config-router)#no auto-summary

如果设定无问题,在 R2 和 R5 会见到由 EIGRP 发过来的 Router。

R2#show ip route vrf BLUE | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.12.0/24 is directly connected, Ethernet0/1
      172.16.0.0/24 is subnetted, 1 subnets
 D       172.16.1.0 [90/409600] via 192.168.12.1, 00:06:35, Ethernet0/1
R5#show ip route vrf BLUE | begin Gateway
 Gateway of last resort is not set
 
      172.16.0.0/24 is subnetted, 1 subnets
 D       172.16.2.0 [90/409600] via 192.168.56.6, 00:02:15, Ethernet0/0
 C    192.168.56.0/24 is directly connected, Ethernet0/0

步骤 5:Redistribute

现在怎样把分隔两地的 Route 好好的带到对岸呢?答案就是依靠我们刚刚建立的 MP-BGP 这道桥梁。先用 RIP 作例子,我们要把 RIP 的 Route Redistribute 到 BGP 里,乘坐 BGP 去到对岸後,又再把 BGP redistribute 回到 RIP。不过要留意的是,RIP 是属於 VRF RED 的,所以要乘属於 VRF RED 的 BGP,要对号入座喔!

来试试看,在 R2 的 BGP 建立 VRF RED 然後 redistribute RIP,完成後用 show ip bgp vpnv4 vrf RED 已经可以看到 RIP 的 route 进入 BGP VRF RED 的 Database 了!

R2(config)#router bgp 65000
 R2(config-router)#address-family ipv4 vrf RED
 R2(config-router-af)#redistribute rip
 R2(config-router-af)#exit-address-family
 R2(config-router-af)#end
 R2#
 R2#show ip bgp vpnv4 vrf RED
 BGP table version is 5, local router ID is 2.2.2.2
 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
               r RIB-failure, S Stale
 Origin codes: i - IGP, e - EGP, ? - incomplete
 
    Network          Next Hop            Metric LocPrf Weight Path
 Route Distinguisher: 65000:100 (default for vrf RED)
 *> 172.16.1.0/24    192.168.27.7             1         32768 ?
 *> 192.168.27.0     0.0.0.0                  0         32768 ?

来到 R5 又看看有没有收到 RIP 这批乘客:

R5#show ip bgp vpnv4 vrf RED
 BGP table version is 23, local router ID is 5.5.5.5
 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
               r RIB-failure, S Stale
 Origin codes: i - IGP, e - EGP, ? - incomplete
 
    Network          Next Hop            Metric LocPrf Weight Path
 Route Distinguisher: 65000:100 (default for vrf RED)
 *>i172.16.1.0/24    2.2.2.2                  1    100      0 ?
 *>i192.168.27.0     2.2.2.2                  0    100      0 ?

有了!现在我们要做的是把 BGP 的乘客 redistribute 回到 RIP:

R5(config)#router rip
 R5(config-router)#address-family ipv4 vrf RED
 R5(config-router-af)#redistribute bgp 65000 metric 1
 R5(config-router-af)#exit-address-family

最後看看 R8 的 Routing Table 吧,收到从对岸远渡而来的 Route 了。

R8#show ip route | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.58.0/24 is directly connected, Ethernet0/0
 R    192.168.27.0/24 [120/1] via 192.168.58.5, 00:00:12, Ethernet0/0
      172.16.0.0/24 is subnetted, 2 subnets
 R       172.16.1.0 [120/1] via 192.168.58.5, 00:00:12, Ethernet0/0
 C       172.16.2.0 is directly connected, Loopback0

然後我们把同一个设定倒转再做一遍,把 R5 的 RIP route 经 BGP 送到 R2:

R5(config)#router bgp 65000
 R5(config-router)#address-family ipv4 vrf RED
 R5(config-router-af)#redistribute rip
 R5(config-router-af)#exit-address-family
 R5(config-router-af)#end
R2(config)#router rip
 R2(config-router)#address-family ipv4 vrf RED
 R2(config-router-af)#redistribute bgp 65000 metric 1
 R2(config-router-af)#exit-address-family

R7 收到从 R8 来的 RIP Route,而且……而且还 PING 通了!!

R7#show ip route | begin Gateway
 Gateway of last resort is not set
 
 R    192.168.58.0/24 [120/1] via 192.168.27.2, 00:00:16, Ethernet0/0
 C    192.168.27.0/24 is directly connected, Ethernet0/0
      172.16.0.0/24 is subnetted, 2 subnets
 C       172.16.1.0 is directly connected, Loopback0
 R       172.16.2.0 [120/1] via 192.168.27.2, 00:00:16, Ethernet0/0
 R7#
 R7#ping 172.16.2.1 source 172.16.1.1
 
 Type escape sequence to abort.
 Sending 5, 100-byte ICMP Echos to 172.16.2.1, timeout is 2 seconds:
 !!!!!
 Success rate is 100 percent (5/5), round-trip min/avg/max = 200/268/336 ms

完成了 RIP 的部份,我们再来试试 EIGRP 这一边,其实做法相同。以下显示了 R2 的设定,而 R5 的设定则和 R2 相同。

R2(config)#router bgp 65000
 R2(config-router)#address-family ipv4 vrf BLUE
 R2(config-router-af)#redistribute eigrp 20
 R2(config-router-af)#exit-address-family
 R2(config-router)#exit
 R2(config)#router eigrp 1
 R2(config-router)#address-family ipv4 vrf BLUE
 R2(config-router-af)#redistribute bgp 65000 metric 1 0 0 1 1
 R2(config-router-af)#exit-address-family

R1 有收到 EIGRP Route,也能够 Ping 到对面。

R1#show ip route | begin Gateway
 Gateway of last resort is not set
 
 C    192.168.12.0/24 is directly connected, Ethernet0/0
      172.16.0.0/24 is subnetted, 2 subnets
 C       172.16.1.0 is directly connected, Loopback0
 D       172.16.2.0 [90/435200] via 192.168.12.2, 00:07:21, Ethernet0/0
 D    192.168.56.0/24 [90/307200] via 192.168.12.2, 00:07:21, Ethernet0/0
 R1#
 R1#ping 172.16.2.1 source 172.16.1.1
 
 Type escape sequence to abort.
 Sending 5, 100-byte ICMP Echos to 172.16.2.1, timeout is 2 seconds:
 !!!!!
 Success rate is 100 percent (5/5), round-trip min/avg/max = 300/386/448 ms

小结

就是这样,终於让两家公司各自 PING 到自己的对家,可谓功德圆满。这确是个难度颇高的设定,笔者自己也花了很多时间去学习。不知大家有没有留意到,这个 MPLS VPN 最美妙的地方,其实是在於 R3 和 R4 这两只 Router 的 Route Table 中,一直都没有 RED 公司和 BLUE 公司的 Route,它们对 RED 公司和 BLUE 公司的 Route 其实是一无所知的,只是在忠实地执行 Label Switching,就算你有一百家公司在外面,他们都没有为那些 Route 操心过,这也是 MPLS 其中一个想达到的目的之一。

Coming Soon……

sham link

相關主題

发表回复

2021-07-22

Posted In: menu-tall-2-zh-hans, 虚拟私人网路 VPN

Leave a Comment