# 6.策略路由改变下一跳

## 1 简介

本文档介绍了策略路由的配置举例。

普通报文是根据目的IP地址来查找路由表转发的，策略路由是一种依据用户制定的策略进行路由选择的机制。策略路由可以基于到达报文的源地址、目的地址、IP优先级、协议类型等字段灵活地进行路由选择。

## 2 配置前提

本文档中的配置均是在实验室环境下进行的配置和验证，配置前设备的所有参数均采用出厂时的缺省配置。如果您已经对设备进行了配置，为了保证配置效果，请确认现有配置和以下举例中的配置不冲突。

本文假设您已了解策略路由特性。

## 3 使用限制

· 本设备只支持转发策略路由。转发策略路由只对**接口接收的报文**起作用，指导其转发，对本地产生的报文不起作用；

· 配置重定向到下一跳时，不能将IPv4规则重定向到IPv6地址，反之亦然。

## 4 IPv4策略路由配置举例

### 4.1 组网需求

如[图1](https://www.h3c.com/cn/d_202005/1292943_30005_0.htm#_Ref355687799)所示，缺省情况下，Device的Vlan-interface2上收到的所有访问Server的报文根据路由表转发的下一跳均为10.4.1.2。

现要求在Device上配置IPv4策略路由，对于访问Server的报文实现如下要求：

(1) 首先匹配Vlan-interface2上收到的源IP地址为10.2.1.1的报文，将该报文的下一跳重定向到10.5.1.2；

(2) 其次匹配Vlan-interface2上收到的HTTP报文，将该报文的下一跳重定向到10.3.1.2。

图1 IPv4策略路由特性典型配置组网图

![img](https://pic.chjina.com/2023/03/10/20200514_4918368_image001_1292943_30005_0.png)

### 4.2 配置思路

· 为了确保能同时满足对于两种不同类型的报文重定向到不同的下一跳，需要配置两个访问控制列表，一个用于匹配Vlan-interface2上收到的源IP地址为10.2.1.1的报文，另一个用于匹配Vlan-interface2上收到的HTTP报文，并在策略路由中创建两个节点，分别对匹配上的报文进行重定向；

· **同一条策略路由中，创建的节点编号越小，优先级越高**。为了确保Vlan-interface2上收到源IP地址为10.2.1.1的HTTP报文下一跳能优先被重定向到10.5.1.2，需要在策略路由中配置该策略使用较小的节点编号（本例中使用0号节点，另一策略使用1号节点）。

### 4.3 配置步骤

\#配置Vlan-interface2的IP地址。

```
<Device> system-view
[Device] interface vlan-interface 2
[Device-Vlan-interface2] ip address 10.1.2.1 255.255.255.0
[Device-Vlan-interface2] quit
```

\#请参考以上方法配置图1中其它接口的IP地址，配置步骤这里省略。

\# 配置静态路由，保证三条路径都可达，并且缺省下一跳为10.4.1.2。**路由优先级越小越优先，默认为60**.

```
[Device] ip route-static 192.168.1.0 24 10.3.1.2
[Device] ip route-static 192.168.1.0 24 10.4.1.2 preference 40
[Device] ip route-static 192.168.1.0 24 10.5.1.2
```

\# 定义访问控制列表ACL 3005，用于匹配源IP地址为10.2.1.1的报文。

```
[Device] acl advanced 3005
[Device-acl-ipv4-adv-3005] rule 0 permit ip source 10.2.1.1 0
[Device-acl-ipv4-adv-3005] quit
```

\# 定义访问控制列表ACL 3006，用于匹配HTTP报文。

```
[Device] acl advanced 3006
[Device-acl-ipv4-adv-3006] rule 0 permit tcp destination-port eq www
[Device-acl-ipv4-adv-3006] quit
```

\# 创建策略路由pbr1的0号节点，将匹配ACL 3005的报文下一跳重定向到10.5.1.2。

```
[Device] policy-based-route pbr1 permit node 0
[Device-pbr-pbr1-0] if-match acl 3005
[Device-pbr-pbr1-0] apply next-hop 10.5.1.2
[Device-pbr-pbr1-0] quit
```

\# 创建策略路由pbr1的1号节点，将匹配ACL 3006的报文下一跳重定向到10.3.1.2。

```
[Device] policy-based-route pbr1 permit node 1
[Device-pbr-pbr1-1] if-match acl 3006
[Device-pbr-pbr1-1] apply next-hop 10.3.1.2
[Device-pbr-pbr1-1] quit
```

\# 在Device的接口Vlan-interface2上应用策略。

```
[Device] interface vlan-interface 2
[Device-Vlan-interface2] ip policy-based-route pbr1
[Device-Vlan-interface2] quit
```

### 4.4 验证配置

通过display ip policy-based-route命令可以查看到当前策略路由配置已经配置成功：

```
[Device] display ip policy-based-route policy pbr1
Policy name: pbr1
 node 0 permit:
  if-match acl 3005
  apply next-hop 10.5.1.2
 node 1 permit:
  if-match acl 3006
  apply next-hop 10.3.1.2
```

\# 通过tracert命令查看以下报文的转发路径（使用Tracert功能需要在中间设备上开启ICMP超时报文发送功能，在目的端开启ICMP目的不可达报文发送功能）：

源IP为10.1.1.1的非HTTP报文，重定向到10.4.1.2进行转发。

```
<Switch> tracert -a 10.1.1.1 192.168.1.1
traceroute to 192.168.1.1 (192.168.1.1) from 10.1.1.1, 30 hops at most, 40 bytes
 each packet, press CTRL_C to break
 1 10.1.2.1 (10.1.2.1) 2.178 ms 1.364 ms 1.058 ms
 2 10.4.1.2 (10.4.1.2) 1.548 ms 1.248 ms 1.112 ms
 3 192.168.1.1 (192.168.1.1) 1.594 ms 1.321 ms 1.093 ms
```

源IP为10.2.1.1的报文，重定向到10.5.1.2进行转发。

```
<Switch> tracert -a 10.2.1.1 192.168.1.1
traceroute to 192.168.1.1 (192.168.1.1) from 10.2.1.1, 30 hops at most, 40 bytes
 each packet, press CTRL_C to break
 1 10.1.2.1 (10.1.2.1) 1.721 ms 1.226 ms 1.050 ms
 2 10.5.1.2 (10.5.1.2) 4.494 ms 1.385 ms 1.170 ms
 3 192.168.1.1 (192.168.1.1) 1.448 ms 1.304 ms 1.093 ms
```

## 5 查看配置

5.1显示已经配置的策略

```
[Device]display ip policy-based-route
Policy name: pbr1
  node 0 permit:
    if-match acl 3005
    apply next-hop 10.5.1.2
  node 1 permit:
    if-match acl 3006
    apply next-hop 10.3.1.2
```

5.2显示已经应用的策略路由信息

```
[Device]dis ip policy-based-route setup
Policy name              Type     Interface
pbr1                     Forward  Vlan-interface2
```

引用：

{% embed url="<https://www.h3c.com/cn/d_202005/1292943_30005_0.htm>" %}

{% embed url="<https://www.h3c.com/cn/d_202208/1659284_30005_0.htm#_Ref293667388>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.chjina.com/it-zhi-shi-ku/lu-you-jiao-huan/hua-san/6.-ce-le-lu-you-gai-bian-xia-yi-tiao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
