基於策略的路由(PBR)

關於PBR的set ip next-hop和set ip default next-hop

基於策略的路由(PBR)

拓撲很簡單,在R13和R16有lo0:1.1.1.1, R14要訪問1.1.1.1,咱們經過set ip next-hop和set ip default next-hop來控制其選路,看看他們區別在什麼地方.本實驗拓撲簡單,僅爲測試相關feature。網絡

1 . 基本環境介紹
R14模擬一臺終端設備地址爲192.168.42。14,其網關地址爲192.168.42.12(R12 -> e0/0),R12使用P2P方式與R13和R16相連
R14的默認配置以下:
interface Ethernet0/0
ip address 192.168.42.14 255.255.255.0
no ip route-cache
ip default-gateway 192.168.42.12app

R12默認配置:
interface Serial1/0
ip address 192.168.23.12 255.255.255.0
serial restart-delay 0
!
interface Serial1/1
ip address 192.168.26.12 255.255.255.0
serial restart-delay 0
!
此時R12路由表沒有到1.1.1.1的路由,後面就須要經過PBR測試其聯通性和進行選路
R12#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 routeide

    • replicated route, % - next hop override

Gateway of last resort is not setoop

192.168.23.0/24 is variably subnetted, 2 subnets, 2 masks

C 192.168.23.0/24 is directly connected, Serial1/0
L 192.168.23.12/32 is directly connected, Serial1/0
192.168.26.0/24 is variably subnetted, 2 subnets, 2 masks
C 192.168.26.0/24 is directly connected, Serial1/1
L 192.168.26.12/32 is directly connected, Serial1/1
192.168.42.0/24 is variably subnetted, 2 subnets, 2 masks
C 192.168.42.0/24 is directly connected, Ethernet0/0
L 192.168.42.12/32 is directly connected, Ethernet0/0測試

R13配置:
interface Loopback0
ip address 1.1.1.1 255.255.255.255
interface Serial1/0
ip address 192.168.23.13 255.255.255.0
serial restart-delay 0
ip route 0.0.0.0 0.0.0.0 Serial1/0 (默認路由出局)rest

R16配置:
interface Serial1/0
ip address 192.168.26.16 255.255.255.0
serial restart-delay 0
interface Loopback0
ip address 1.1.1.1 255.255.255.255
ip route 0.0.0.0 0.0.0.0 Serial1/0 (默認路由出局)code

2 . 根據上述配置,R14是沒法ping通1.1.1.1,由於在R12並無到達1.1.1.1的路由
R14#ping 1.1.1.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
U.U.U
Success rate is 0 percent (0/5)blog

2.1 需求1 ,咱們要求R14可以ping通1.1.1.1,而且走的路徑是R14->R12->R13,那麼咱們經過PBR將R12達到1.1.1.1的下一跳設置爲R13的s1/0(192.168.23.13)
配置以下:
第一步:匹配流量,簡單起見,直接匹配全部流量,固然也能夠根據需求,匹配特定流量
access-list 1 permit any
第二步:配置route-map,匹配access-list,並設置下一跳爲192.168.23.13
route-map map01 permit 1
match ip address 1
set ip next-hop 192.168.23.13
route-map map01 permit 2(除了匹配的流量,咱們須要放行其餘流量,而後不作任何設置便可,所以須要在添加一條permit, 這一點必須注意)
第三步:將route-map應用於R12的e0/0口,
interface Ethernet0/0
ip address 192.168.42.12 255.255.255.0
ip policy route-map map01
完成上述配置後,咱們測試一下連通性和路徑走向,能ping通,而且走的是R13
R14#ping 1.1.1.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 9/10/12 ms
R14#tra
R14#traceroute 1.1.1.1
Type escape sequence to abort.
Tracing the route to 1.1.1.1
VRF info: (vrf in name/id, vrf out name/id)
1 192.168.42.12 1 msec 0 msec 0 msec
2 192.168.23.13 10 msec * 10 msecip

2.2 需求2, 如今R12在沒有到達1.1.1.1的路由表的狀況下,咱們經過PBR完成的連通性,那,咱們若是配置一條靜態路由到1.1.1.1,下一跳是R16 s1/0(192.168.26.16),那R14是否能夠ping通1.1.1.1,而且路徑會不會變呢?
l R12配置:
咱們在需求1的基礎上添加一條靜態路由
Ip route 1.1.1.1 255.255.255.255 192.168.26.16ci

測試:
R14#traceroute 1.1.1.1
Type escape sequence to abort.
Tracing the route to 1.1.1.1
VRF info: (vrf in name/id, vrf out name/id)
1 192.168.42.12 0 msec 2 msec 1 msec
2 192.168.23.13 11 msec 10 msec
根據咱們測試,仍是會選在PBR設置的next-hop
結論:若是咱們使用PBR,而且設置使用set ip next-hop命令,那麼next-hop的優先級要高於任何路由表項
2.3 需求3,若是咱們想優選路徑爲R14->R12->R16,應該如何進行修改呢?那就須要使用set ip default next-hop 192.168.26.16,
修改R12配置:
route-map map01 permit 1
match ip address 1
set ip default next-hop 192.168.23.13
測試:
R14#traceroute 1.1.1.1
Type escape sequence to abort.
Tracing the route to 1.1.1.1
VRF info: (vrf in name/id, vrf out name/id)
1 192.168.42.12 1 msec 1 msec 0 msec
2 192.168.26.16 11 msec
10 msec
測試如咱們所預料,路徑發生了改變
結論:若是咱們使用PBR,而且設置使用set ip default next-hop命令,那麼會先查看路由表有無該路由,若是有就優選路由表,不然在選擇next-hop

2.4 需求4,若是咱們把靜態路由改爲缺省路由,PBR仍是採用set ip default next-hop,那麼路徑是否還會走R14->R12->R16呢?
修改R12配置:
將靜態路由刪除,改成缺省路由
ip route 0.0.0.0 0.0.0.0 192.168.26.16

測試:
R14#traceroute 1.1.1.1
Type escape sequence to abort.
Tracing the route to 1.1.1.1
VRF info: (vrf in name/id, vrf out name/id)
1 192.168.42.12 1 msec 2 msec 2 msec
2 192.168.23.13 10 msec * 11 msec
咱們發現,路徑又回到了R14->R12->R13,那是爲何呢?
結論:set ip default next-hop僅會對明細路由生效,因此對於缺省路由來說,當數據包到達R12後,發現並無到達1.1.1.1的明細路由,所以仍是走next-hop。所以,咱們能夠總結如下:

基於策略的路由(PBR)

總結:不管是在點到點的網絡環境(本實驗環境)仍是多路訪問的環境,都會遵循上表的原則。所以咱們能夠使用PBR做爲路由選路的一種方式,能夠比較靈活的進行路由的控制,這裏咱們使用的是靜態路由,使用動態路由,也能夠經過PBR完成路徑的自由切換,你們不妨能夠測試一下。

相關文章
相關標籤/搜索