> For the complete documentation index, see [llms.txt](https://docs.chjina.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.chjina.com/it-zhi-shi-ku/lu-you-jiao-huan/hua-san/9.-huan-lu-jian-ce-pei-zhi-ju-li.md).

# 9.环路检测配置举例

## 1. 环路检测

### 1.1 环路检测简介

#### 1.1.1 环路检测产生背景

环路检测能够及时发现二层网络中的环路，通过周期性的检查，使网络中出现环路时能及时通知用户检查网络连接和配置情况。当网络中出现环路时，环路检测机制通过生成日志信息来通知用户，并可根据用户事先的配置来选择是否关闭出现环路的端口。

#### 1.1.2 环路检测报文

设备通过发送环路检测报文并检测其是否返回本设备（不要求收、发端口为同一端口）以确认是否存在环路。若某端口收到了由本设备发出的环路检测报文，就认定该端口所在链路存在环路。

#### 1.1.3 环路检测时间间隔

由于网络时刻处于变化中，因此环路检测是一个持续的过程，它以一定的时间间隔发送环路检测报文来确定各端口是否出现环路、以及存在环路的端口上是否已消除环路等，这个时间间隔就称为环路检测的时间间隔。

#### 1.1.4 环路检测处理模式

环路检测的处理模式是指当系统检测到端口出现环路时的处理方式，包括以下几种：

* Block模式：当系统检测到端口出现环路时，除了生成日志信息外，还会禁止端口学习MAC地址并将端口阻塞。
* No-learning模式：当系统检测到端口出现环路时，除了生成日志信息外，还会禁止端口学习MAC地址。
* Shutdown模式：当系统检测到端口出现环路时，除了生成日志信息外，还会自动关闭该端口，使其不能收发任何报文。

缺省情况下，系统不采用上述任何一种模式，当系统检测到端口出现环路时，除了生成日志信息外不对该端口进行任何处理。

#### 1.1.5 端口状态自动恢复

在Block模式和No-learning模式下，当设备检测到某端口出现环路后，若在三倍的环路检测时间间隔内仍未收到环路检测报文，就认为该端口上的环路已消除，自动将该端口恢复为正常转发状态，并通知给用户。这个过程就是端口状态的自动恢复过程。

在Shutdown模式下，出现环路的端口先被自动关闭，然后在**shutdown-interval**命令（请参考“基础配置命令参考”中的“设备管理”）所配置的时间之后自动恢复。如果此时环路尚未消除，该端口将被再次关闭，然后恢复……如此往复直至环路消除。

```
当网络中存在环路时，为防止大量报文的冲击，设备会丢弃部分报文。而如果环路检测报文也被丢弃，设备在端口状态自动恢复功能的作用下会误判定环路已消除。在这种情况下，建议将环路检测的处理模式配置为Shutdown模式，或当设备提示出现环路时通过手工排查来消除环路。
```

## 2. 环路检测典型配置举例

### 2.1 环路检测基本功能配置举例

#### **1. 组网需求**

· 三台设备Device A、Device B和Device C组成一个物理上的环形网络。

· 通过在Device A上配置环路检测功能，使系统能够自动关闭Device A上出现环路的端口，并通过打印日志信息来通知用户检查网络。

#### **2. 组网图**

图1-3 环路检测基本功能配置组网图

![img](https://pic.chjina.com/2023/07/25/20230531_9120074_x_Img_x_png_4_1858327_30005_0.png)

‌

#### **3. 配置步骤**

(1) 配置Device A

\# 创建VLAN 100，并全局开启该VLAN内的环路检测功能。

```
<DeviceA> system-view
[DeviceA] vlan 100
[DeviceA–vlan100] quit
[DeviceA] loopback-detection global enable vlan 100
```

\# 配置端口GigabitEthernet1/0/1和GigabitEthernet1/0/2为Trunk类型，并允许VLAN 100通过。

```
[DeviceA] interface gigabitethernet 1/0/1
[DeviceA-GigabitEthernet1/0/1] port link-type trunk
[DeviceA-GigabitEthernet1/0/1] port trunk permit vlan 100
[DeviceA-GigabitEthernet1/0/1] quit
[DeviceA] interface gigabitethernet 1/0/2
[DeviceA-GigabitEthernet1/0/2] port link-type trunk
[DeviceA-GigabitEthernet1/0/2] port trunk permit vlan 100
[DeviceA-GigabitEthernet1/0/2] quit
```

\# 全局配置环路检测的处理模式为Shutdown模式。

```
[DeviceA] loopback-detection global action shutdown
```

\# 配置环路检测的时间间隔为35秒。

```
[DeviceA] loopback-detection interval-time 35
```

(2) 配置Device B

\# 创建VLAN 100。

```
<DeviceB> system-view
[DeviceB] vlan 100
[DeviceB–vlan100] quit
```

\# 配置端口GigabitEthernet1/0/1和GigabitEthernet1/0/2为Trunk类型，并允许VLAN 100通过。

```
[DeviceB] interface gigabitethernet 1/0/1
[DeviceB-GigabitEthernet1/0/1] port link-type trunk
[DeviceB-GigabitEthernet1/0/1] port trunk permit vlan 100
[DeviceB-GigabitEthernet1/0/1] quit
[DeviceB] interface gigabitethernet 1/0/2
[DeviceB-GigabitEthernet1/0/2] port link-type trunk
[DeviceB-GigabitEthernet1/0/2] port trunk permit vlan 100
[DeviceB-GigabitEthernet1/0/2] quit
```

(3) 配置Device C

\# 创建VLAN 100。

```
<DeviceC> system-view
[DeviceC] vlan 100
[DeviceC–vlan100] quit
```

\# 配置端口GigabitEthernet1/0/1和GigabitEthernet1/0/2为Trunk类型，并允许VLAN 100通过。

```
[DeviceC] interface gigabitethernet 1/0/1
[DeviceC-GigabitEthernet1/0/1] port link-type trunk
[DeviceC-GigabitEthernet1/0/1] port trunk permit vlan 100
[DeviceC-GigabitEthernet1/0/1] quit
[DeviceC] interface gigabitethernet 1/0/2
[DeviceC-GigabitEthernet1/0/2] port link-type trunk
[DeviceC-GigabitEthernet1/0/2] port trunk permit vlan 100
[DeviceC-GigabitEthernet1/0/2] quit
```

#### **4. 验证配置**

当配置完成后，系统在一个环路检测时间间隔内在Device A的端口GigabitEthernet1/0/1和GigabitEthernet1/0/2上都检测到了环路，于是将这两个端口自动关闭，并打印了如下日志信息：

```
[DeviceA]
%Feb 24 15:04:29:663 2013 DeviceA LPDT/4/LPDT_LOOPED:  A loop was detected on GigabitEthernet1/0/1.
%Feb 24 15:04:29:664 2013 DeviceA LPDT/4/LPDT_VLAN_LOOPED: A loop was detected on GigabitEthernet1/0/1 in VLAN 100.
%Feb 24 15:04:29:667 2013 DeviceA LPDT/4/LPDT_LOOPED: A loop was detected on GigabitEthernet1/0/2.
%Feb 24 15:04:29:668 2013 DeviceA LPDT/4/LPDT_VLAN_LOOPED: A loop was detected on GigabitEthernet1/0/2 in VLAN 100.
%Feb 24 15:04:44:243 2013 DeviceA LPDT/5/LPDT_VLAN_RECOVERED: A loop was removed on GigabitEthernet1/0/1 in VLAN 100.
%Feb 24 15:04:44:243 2013 DeviceA LPDT/5/LPDT_RECOVERED: All loops were removed on GigabitEthernet1/0/1.
%Feb 24 15:04:44:248 2013 DeviceA LPDT/5/LPDT_VLAN_RECOVERED: A loop was removed on GigabitEthernet1/0/2 in VLAN 100.
%Feb 24 15:04:44:248 2013 DeviceA LPDT/5/LPDT_RECOVERED: All loops were removed on GigabitEthernet1/0/2.
```

使用**display loopback-detection**命令可以查看Device A上环路检测的配置和运行情况：

\# 显示Device A上环路检测的配置和运行情况。

```
[DeviceA] display loopback-detection
Loop detection is enabled.
Loop detection interval is 35 second(s).
Loop is detected on following interfaces:
 Interface           Action mode   VLANs
 GigabitEthernet1/0/1      Shutdown    100
 GigabitEthernet1/0/2      Shutdown    100
```

由此可见，Device A上显示在端口GigabitEthernet1/0/1和GigabitEthernet1/0/2上检测到环路，由于环路检测功能运行在Shutdown模式下，端口GigabitEthernet1/0/1和GigabitEthernet1/0/2上出现环路后均已被自动关闭，因此设备打印的日志信息显示这两个端口上的环路已消除。此时，使用**display interface**命令分别查看Device A上端口GigabitEthernet1/0/1和GigabitEthernet1/0/2的状态信息：

\# 显示Device A上端口GigabitEthernet1/0/1的状态信息。

```
[DeviceA] display interface gigabitethernet 1/0/1
GigabitEthernet1/0/1 current state: DOWN (Loopback detection down)
...
```

\# 显示Device A上端口GigabitEthernet1/0/2的状态信息。

```
[DeviceA] display interface gigabitethernet 1/0/2
GigabitEthernet1/0/2 current state: DOWN (Loopback detection down)
...
```

由此可见，端口GigabitEthernet1/0/1和GigabitEthernet1/0/2均已被环路检测模块自动关闭。

引用：

{% embed url="<https://www.h3c.com/cn/d_202305/1858327_30005_0.htm#_Ref470192304>" %}

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