二层环路问题是一种常见且严重的故障,可能导致网络拥塞、数据包丢失,甚至整个网络瘫痪。为了确保网络的稳定性和性能,及时有效地检测和解决二层环路问题至关重要。本文将详细介绍四种关键方法,帮助网络管理员定位和解决二层环路问题。这四种方法包括查看接口带宽流量、查看MAC地址漂移、环路检测和查看CPU占用率。
目录:
网络环路(Loop)问题是常见的网络故障之一,会导致网络拥塞、性能下降,甚至网络瘫痪。为了诊断和解决网络环路问题,查看接口带宽流量是一个基本且关键的步骤。
接口带宽流量(Interface Bandwidth Utilization)指的是通过网络接口传输的数据量。它是衡量网络接口性能的关键指标之一,通常以每秒传输的比特数(bps)来表示。
接口带宽流量直接影响网络的整体性能。当接口带宽利用率接近或达到其最大容量时,数据包可能会丢失,导致网络延迟增加和吞吐量下降。通过监测接口带宽流量,管理员可以识别和解决这些问题,从而优化网络性能。
SNMP 是一种标准的网络管理协议,用于收集和组织网络设备的信息,并进行远程监控和管理。
- Router(config)# snmp-server community public RO
-
安装 MRTG
- sudo apt-get install mrtg
-
配置 MRTG
- Target[router]: 1.3.6.1.2.1.2.2.1.10.1&1.3.6.1.2.1.2.2.1.16.1:public@router
- MaxBytes[router]: 1250000
- Title[router]: Router Traffic Analysis
- PageTop[router]: <H1>Router Traffic Analysis</H1>
-
运行 MRTG
- mrtg /etc/mrtg.cfg
-
NetFlow 是由思科开发的一种网络协议,用于收集 IP 流量信息。它能够详细记录每个流的开始和结束时间、源和目的地 IP 地址、传输的字节数等。
- Router(config)# ip flow-export destination 192.168.1.100 9996
- Router(config)# ip flow-export version 9
- Router(config)# interface GigabitEthernet0/1
- Router(config-if)# ip flow ingress
-
安装 ntopng
- sudo apt-get install ntopng
-
配置 ntopng
- --interface=eth0
- --netflow=3000
- --local-networks="192.168.1.0/24"
-
运行 ntopng
- sudo systemctl start ntopng
-
sFlow 是一种基于采样的流量监控协议,通过随机采样的数据包来收集网络流量信息。
- ProCurve(config)# sflow 1 destination 192.168.1.100 6343
- ProCurve(config)# sflow 1 polling 30
- ProCurve(config)# sflow 1 sampling 512
-
下载和安装 sFlowTrend
- wget http://www.inmon.com/products/sFlowTrend-download.php
-
配置 sFlowTrend 连接到 sFlow 数据源
运行 sFlowTrend 并查看实时流量
- Router(config)# class-map match-any critical
- Router(config-cmap)# match ip dscp af31
- Router(config)# policy-map QoS-Policy
- Router(config-pmap)# class critical
- Router(config-pmap-c)# bandwidth percent 50
-
- Switch(config)# policy-map Traffic-Control
- Switch(config-pmap)# class critical
- Switch(config-pmap-c)# police 5000000 conform-action transmit exceed-action drop
-
MAC地址漂移是指同一个MAC地址在不同的网络接口之间频繁切换,这通常是由于网络拓扑中存在环路或网络设备故障导致的。MAC地址漂移会导致交换机的MAC地址表频繁更新,增加CPU负担,影响网络性能。
当MAC地址频繁在不同端口间漂移时,交换机需要不断更新其MAC地址表。这种频繁的更新不仅增加了交换机的CPU负担,还会导致数据包转发的不稳定,影响网络的整体性能。
- Switch# show log | include MACFLAP
-
- Switch# show mac address-table flapping
-
Wireshark是一款开源的网络协议分析工具,可以捕获并分析网络流量,帮助检测MAC地址漂移。
打开Wireshark,选择网络接口,点击"Start"按钮开始捕获流量。
使用过滤器"eth.addr == <mac_address>"查看特定MAC地址的流量。
SolarWinds NPM是一款商业网络监测工具,具有检测MAC地址漂移的功能。
通过NPM的用户界面,查看MAC地址表并检测漂移情况。
通过编写Python脚本,自动化检测MAC地址漂移。
参考脚本:
- from pysnmp.hlapi import *
-
- def get_mac_address_table(ip, community):
- iterator = nextCmd(SnmpEngine(),
- CommunityData(community),
- UdpTransportTarget((ip, 161)),
- ContextData(),
- ObjectType(ObjectIdentity('1.3.6.1.2.1.17.4.3.1.1')),
- ObjectType(ObjectIdentity('1.3.6.1.2.1.17.4.3.1.2')),
- lookupMib=False)
-
- mac_table = {}
- for errorIndication, errorStatus, errorIndex, varBinds in iterator:
- if errorIndication:
- print(errorIndication)
- break
- elif errorStatus:
- print('%s at %s' % (errorStatus.prettyPrint(),
- errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
- break
- else:
- mac = varBinds[0][1].prettyPrint()
- port = varBinds[1][1].prettyPrint()
- if mac in mac_table:
- mac_table[mac].append(port)
- else:
- mac_table[mac] = [port]
-
- return mac_table
-
- def detect_mac_flapping(mac_table):
- flapping_macs = {}
- for mac, ports in mac_table.items():
- if len(set(ports)) > 1:
- flapping_macs[mac] = ports
- return flapping_macs
-
- ip = '192.168.1.1'
- community = 'public'
- mac_table = get_mac_address_table(ip, community)
- flapping_macs = detect_mac_flapping(mac_table)
-
- if flapping_macs:
- print("Detected MAC address flapping:")
- for mac, ports in flapping_macs.items():
- print(f"MAC: {mac} on Ports: {', '.join(ports)}")
- else:
- print("No MAC address flapping detected.")
-
确认网络拓扑中不存在不必要的环路。比如检查交换机和路由器的物理连接,确保没有形成环路。
STP 是一种防止环路的网络协议,能够自动检测并阻止网络中的环路。
例子:在思科交换机上启用STP
- Switch(config)# spanning-tree mode pvst
- Switch(config)# spanning-tree vlan 1 priority 4096
-
确认交换机和路由器的固件是最新版本,旧版本固件可能存在已知的BUG。
例子:在思科交换机上查看固件版本
- Switch# show version
-
根据设备制造商的指导,下载并升级设备固件。
在思科交换机上升级固件
- Switch# copy tftp://192.168.1.100/image.bin flash:
- Switch# reload
-
使用端口安全特性限制每个端口上允许的MAC地址数量,从而防止MAC地址漂移。
在思科交换机上配置端口安全
- Switch(config)# interface GigabitEthernet0/1
- Switch(config-if)# switchport mode access
- Switch(config-if)# switchport port-security
- Switch(config-if)# switchport port-security maximum 1
- Switch(config-if)# switchport port-security violation restrict
-
通过持续监控网络流量,及时调整网络配置,防止MAC地址漂移的发生。
比如使用NetFlow或sFlow监控流量,发现异常流量及时处理。
网络环路是指网络中存在一个或多个路径,使得数据包在网络中无限循环。这种情况会导致网络拥塞、数据包丢失,甚至整个网络瘫痪。为了保障网络的稳定性和性能,及时检测和解决环路问题是非常必要的。
环路是网络中设备连接错误或配置不当导致的现象。常见成因包括:
例如,两个交换机之间的多个连接未正确配置生成树协议(STP),形成物理环路。
不当的VLAN配置、冗余链路未配置STP等。
网络设备如交换机或路由器故障,导致数据包无限转发。
环路会导致:
STP 是一种网络协议,用于防止网络环路,通过阻止冗余路径来确保网络的树形结构。
在交换机上启用并配置STP。
例子:在思科交换机上配置STP
- Switch(config)# spanning-tree mode rapid-pvst
- Switch(config)# spanning-tree vlan 1 root primary
- Switch(config)# spanning-tree vlan 1 priority 4096
-
NPM 能够自动检测网络中的环路,并提供详细的报告。通过NPM的用户界面,查看环路检测报告并处理环路问题。
使用Wireshark捕获并分析网络流量,检测环路。
打开Wireshark,选择网络接口,点击"Start"按钮开始捕获流量。使用过滤器"eth.addr == <mac_address>"查看特定MAC地址的流量,检测重复的数据包。
编写Python脚本自动检测环路。
参考脚本:
- from scapy.all import *
-
- def detect_loop(packet):
- if packet.haslayer(Ether):
- src_mac = packet[Ether].src
- dst_mac = packet[Ether].dst
- if src_mac == dst_mac:
- print(f"Loop detected: {src_mac} -> {dst_mac}")
-
- sniff(prn=detect_loop)
-
识别并物理断开形成环路的连接。比如通过检查交换机和路由器的连接,找到并断开多余的连接。
确保所有交换机和路由器正确配置生成树协议。
在思科交换机上重新配置STP
- Switch(config)# spanning-tree mode rapid-pvst
- Switch(config)# spanning-tree vlan 1 root primary
-
定期检查和更新网络拓扑图,确保没有不必要的环路。例如使用网络监测工具如SolarWinds NPM定期生成网络拓扑图。
使用交换机的环路保护机制,如BPDU Guard、Loop Guard。
在思科交换机上启用BPDU Guard
- Switch(config)# interface range GigabitEthernet0/1 - 24
- Switch(config-if-range)# spanning-tree bpduguard enable
-
CPU占用率(CPU Utilization)是指在一定时间内,CPU处理任务所占用的百分比。它反映了CPU的负载情况,是衡量设备性能的重要指标之一。
高CPU占用率会导致设备响应时间增加、数据包处理延迟,甚至数据包丢失,从而影响网络性能。监控CPU占用率可以帮助网络管理员识别潜在的性能瓶颈和故障。
在思科设备上,可以通过CLI命令查看CPU占用率。
查看思科路由器的CPU占用率
- Router# show processes cpu
-
- CPU utilization for five seconds: 10%/3%; one minute: 5%; five minutes: 3%
-
在华为设备上,也可以通过CLI命令查看CPU占用率。
查看华为交换机的CPU占用率
- <Switch> display cpu-usage
-
- CPU utilization for five seconds: 15%; one minute: 12%; five minutes: 10%
-
NPM是一款强大的网络监控工具,可以实时监控设备的CPU占用率。通过NPM的用户界面,查看设备的CPU占用率图表和报告。
PRTG是一款综合的网络监控工具,支持监控CPU占用率。添加需要监控的网络设备,并配置CPU占用率传感器。通过PRTG的用户界面,查看设备的CPU占用率图表和报告。
过编写Python脚本,自动化查看和监控设备的CPU占用率。
参考脚本:
- from pysnmp.hlapi import *
-
- def get_cpu_usage(ip, community):
- iterator = getCmd(SnmpEngine(),
- CommunityData(community),
- UdpTransportTarget((ip, 161)),
- ContextData(),
- ObjectType(ObjectIdentity('1.3.6.1.4.1.9.2.1.57.0')))
-
- errorIndication, errorStatus, errorIndex, varBinds = next(iterator)
- if errorIndication:
- print(errorIndication)
- elif errorStatus:
- print('%s at %s' % (errorStatus.prettyPrint(),
- errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
- else:
- for varBind in varBinds:
- print(' = '.join([x.prettyPrint() for x in varBind]))
-
- ip = '192.168.1.1'
- community = 'public'
- get_cpu_usage(ip, community)
-
分析设备上的进程和任务,找出占用CPU资源最多的进程。
比如在思科路由器上查看CPU占用率最高的进程
- Router# show processes cpu sorted
-
分析网络流量,找出导致高CPU占用率的流量模式或攻击行为。
比如使用Wireshark分析流量:打开Wireshark,捕获流量,使用过滤器分析异常流量。
通过优化设备配置,减少CPU负载。
比如调整路由器上的ACL(访问控制列表)配置,减少CPU处理负担。
- Router(config)# access-list 100 permit ip any any
-
升级设备的硬件或固件,以提高设备性能。
比如升级思科交换机的固件
- Switch# copy tftp://192.168.1.100/image.bin flash:
- Switch# reload
-
通过分流和负载均衡,将负载分散到多个设备上,降低单台设备的CPU占用率。比如使用F5负载均衡器配置负载均衡,将流量分散到多台服务器。
最后给大家做个总结,精简一下文章内容:
本文完!