- 前言
- 1. 了解接收数据包的流程
- 将网卡收到的数据包转移到主机内存(NIC 与驱动交互)
- 通知系统内核处理(驱动与 Linux 内核交互)
- 2. ifconfig 解释
- 3. 网卡工作原理
- 网卡发包
- 网卡收包
- 网卡中断处理函数
- 缓冲区访问
- 4. 丢包排查思路
- 先查看硬件情况
- overruns 和 buffer size
- Red Hat 官方解决思路
- 参考文章
之前记录过处理因为 LVS 网卡流量负载过高导致软中断发生丢包的问题,RPS 和 RFS 网卡多队列性能调优实践[1],对一般人来说压力不大的情况下其实碰见的概率并不高。这次想分享的话题是比较常见服务器网卡丢包现象排查思路,如果你是想了解点对点的丢包解决思路涉及面可能就比较广,不妨先参考之前的文章如何使用 MTR 诊断网络问题[2],对于 Linux 常用的网卡丢包分析工具自然是 ethtool。
The probability of dealing with the loss of packages due to high load loads of LVS webcards has previously been documented.
ethtool 用于查看和修改网络设备(尤其是有线以太网设备)的驱动参数和硬件设置。你可以根据需要更改以太网卡的参数,包括自动协商、速度、双工和局域网唤醒等参数。通过对以太网卡的配置,你的计算机可以通过网络有效地进行通信。该工具提供了许多关于接驳到你的 Linux 系统的以太网设备的信息。
Ethool is used to view and modify drive parameters and hardware settings for network devices, especially cable-to-team devices. You can change the parameters for an Ethernet card as necessary, including automatic consultation, speed, double engineering, and local area network awakening. Your computer can communicate effectively through the network through the configuration of the Ethernet cards. The tool provides a lot of information about the Ethernet devices that have access to your Linux system.
接收数据包是一个复杂的过程,涉及很多底层的技术细节,但大致需要以下几个步骤:
The receipt of data packages is a complex process involving many bottom-level technical details, but the following steps are generally required:
- 网卡收到数据包。
- 将数据包从网卡硬件缓存转移到服务器内存中。
- 通知内核处理。
- 经过 TCP/IP 协议逐层处理。
- 应用程序通过 read() 从 socket buffer 读取数据。
将网卡收到的数据包转移到主机内存(NIC 与驱动交互)
NIC 在接收到数据包之后,首先需要将数据同步到内核中,这中间的桥梁是 rx ring buffer。它是由 NIC 和驱动程序共享的一片区域,事实上,rx ring buffer 存储的并不是实际的 packet 数据,而是一个描述符,这个描述符指向了它真正的存储地址,具体流程如下:
After receiving the package, NIC first needs to sync the data to the kernel, the middle bridge being rx ring buffer. It is an area shared by NIC and the driver. In fact, rx ring buffer does not store actual packet data, but rather is a descriptor that points to its true storage address, as follows:
- 驱动在内存中分配一片缓冲区用来接收数据包,叫做 sk_buffer;
- 将上述缓冲区的地址和大小(即接收描述符),加入到 rx ring buffer。描述符中的缓冲区地址是 DMA 使用的物理地址;
- 驱动通知网卡有一个新的描述符;
- 网卡从 rx ring buffer 中取出描述符,从而获知缓冲区的地址和大小;
- 网卡收到新的数据包;
- 网卡将新数据包通过 DMA 直接写到 sk_buffer 中。
当驱动处理速度跟不上网卡收包速度时,驱动来不及分配缓冲区,NIC 接收到的数据包无法及时写到 sk_buffer,就会产生堆积,当 NIC 内部缓冲区写满后,就会丢弃部分数据,引起丢包。这部分丢包为 rx_fifo_errors,在 /proc/net/dev 中体现为 fifo 字段增长,在 ifconfig 中体现为 overruns 指标增长。
When the speed of processing is driven and the speed of receiving off-line cards is not enough to distribute buffers, NICs receive data packages that cannot be written in time to sk_buffer, and when NIC internal buffers are filled, they discard some of the data and cause the drop of the package. This drop is rx_fio_errors, reflected in /proc/net/dev as a fifo field increase and reflected in ifconfig as an overruns indicator increase.
通知系统内核处理(驱动与 Linux 内核交互)
这个时候,数据包已经被转移到了 sk_buffer 中。前文提到,这是驱动程序在内存中分配的一片缓冲区,并且是通过 DMA 写入的,这种方式不依赖 CPU 直接将数据写到了内存中,意味着对内核来说,其实并不知道已经有新数据到了内存中。那么如何让内核知道有新数据进来了呢?答案就是中断,通过中断告诉内核有新数据进来了,并需要进行后续处理。
At this point, the data package has been transferred to ksk_buffer. As mentioned earlier, this is a buffer area distributed in the memory and written through DMA, which does not rely on CPU to write the data directly into the memory, meaning that for the kernel, it is not known that new data is already in the memory. So how do we let the kernel know that there are new data in it? The answer is to interrupt, to tell the kernel that there are new data in it, and that it needs to be followed up.
提到中断,就涉及到硬中断和软中断,首先需要简单了解一下它们的区别:
The reference to interruptions involves both hard and soft interruptions, which require a brief understanding of their differences:
- 硬中断:由硬件自己生成,具有随机性,硬中断被 CPU 接收后,触发执行中断处理程序。中断处理程序只会处理关键性的、短时间内可以处理完的工作,剩余耗时较长工作,会放到中断之后,由软中断来完成。硬中断也被称为上半部分。
- 软中断:由硬中断对应的中断处理程序生成,往往是预先在代码里实现好的,不具有随机性。(除此之外,也有应用程序触发的软中断,与本文讨论的网卡收包无关。)也被称为下半部分。
当 NIC 把数据包通过 DMA 复制到内核缓冲区 sk_buffer 后,NIC 立即发起一个硬件中断。CPU 接收后,首先进入上半部分,网卡中断对应的中断处理程序是网卡驱动程序的一部分,之后由它发起软中断,进入下半部分,开始消费 sk_buffer 中的数据,交给内核协议栈处理。
When NIC replicates the data package through DMA into the kernel buffer zone sk_buffer, NNIC immediately initiates a hardware interruption. After CPU takes over, it first enters the first half, and the netcard interrupts the corresponding interruption processing process as part of the netcard driver, then it initiates the soft interruption, enters the second half, starts consuming the data in ssk_buffer, and hands it over to the kernel deposit.
通过中断,能够快速及时地响应网卡数据请求,但如果数据量大,那么会产生大量中断请求,CPU 大部分时间都忙于处理中断,效率很低。为了解决这个问题,现在的内核及驱动都采用一种叫 NAPI(new API)的方式进行数据处理,其原理可以简单理解为 中断 + 轮询,在数据量大时,一次中断后通过轮询接收一定数量包再返回,避免产生多次中断。
Disruption allows rapid and timely response to network card data requests, but if the amount of data is large, there will be a large number of interruption requests, and the CPU has been busy processing interruptions and inefficiencies for most of the time. To solve this problem, the kernels and drivers are now processed in a manner called NAPI (new API), the rationale for which can be understood simply as interrupting the + round of queries, and when the amount of data is large, receiving a certain number of packages through a round of queries and returning them in order to avoid multiple interruptions.
- RX errors
表示总的收包的错误数量,这包括 too-long-frames 错误,Ring Buffer 溢出错误,crc 校验错误,帧同步错误,fifo overruns 以及 missed pkg 等等。
This includes too-long-frames error, Ring Buffer overflow error, crc verification error, frame sync error, fifo overruns and missing pkg etc.
- RX dropped
表示数据包已经进入了 Ring Buffer,但是由于内存不够等系统原因,导致在拷贝到内存的过程中被丢弃。
The data package was entered into Ring Buffer, but was discarded during the process of copying to memory due to systemic reasons such as inadequate memory.
- RX overruns
表示了 fifo 的 overruns,这是由于 Ring Buffer(aka Driver Queue) 传输的 IO 大于 kernel 能够处理的 IO 导致的,而 Ring Buffer 则是指在发起 IRQ 请求之前的那块 buffer。很明显,overruns 的增大意味着数据包没到 Ring Buffer 就被网卡物理层给丢弃了,而 CPU 无法即使的处理中断是造成 Ring Buffer 满的原因之一,上面那台有问题的机器就是因为 interruprs 分布的不均匀 (都压在 core0),没有做 affinity 而造成的丢包。
An overrun is expressed because the IO transmitted by Ring Buffer (aka Driver Queue) is larger than the IO that kennel can handle, while Ring Buffer refers to the buffer that predates the launch of the IRQ request. Clearly, the increase in overruns means that the data pack was discarded by Ring Buffer without reaching Ring Buffer, while the CPU could not even be interrupted as one of the causes of the overflow of Ring Buffer, which is the faulty machine caused by the uneven distribution of interrupups (all of which are core0) and the failure to do affinity.
- RX frame
表示 misaligned 的 frames。
Says misaligned frames.
网卡发包
网卡驱动程序将 IP 包添加 14 字节的 MAC 头,构成 frame(暂无 CRC)。Frame(暂无 CRC)中含有发送端和接收端的 MAC 地址,由于是驱动程序创建 MAC 头,所以可以随便输入地址,也可以进行主机伪装。
The netcard driver adds the IP package to the 14 byte header of the MAC, which constitutes the frame (for the time being, there is no CRC). Frame (for the time being, there is no CRC) which contains the sender and receiving end of the MAC address. Since the driver creates the MAC header, it is possible to enter the address, or to disguise the host.
驱动程序将 frame(暂无 CRC)拷贝到网卡芯片内部的缓冲区,由网卡处理。
The driver will copy (for the time being no CRC) into the buffer zone within the netcard chip, which will be handled by the net card.
网卡芯片将未完全完成的 frame(缺 CRC)再次封装为可以发送的 packet,也就是添加头部同步信息和 CRC 校验,然后丢到网线上,就完成一个 IP 报的发送了,所有接到网线上的网卡都可以看到该 packet。
The netcard chip is re-encapable as a package that can be sent, i. e. adds head sync information and CRC verification, and then drops it on the Internet, and completes an IP message, which can be seen on all web cards that are connected to the Internet.
网卡收包
网线上的 packet 首先被网卡获取,网卡会检查 packet 的 CRC 校验,保证完整性,然后将 packet 头去掉,得到 frame。网卡会检查 MAC 包内的目的 MAC 地址,如果和本网卡的 MAC 地址不一样则丢弃 (混杂模式除外)。
The packet on the net is first acquired by a net card, which checks the card's CRC check, ensures its integrity, and then removes the packet head and obtains the frame. The card checks the MAC address for the purpose in the MAC package, or discards it if it is different from the MAC address on this web card (except in mixed mode).
网卡将 frame 拷贝到网卡内部的 FIFO 缓冲区,触发硬件中断。(如有 ring buffer 的网卡,好像 frame 可以先存在 ring buffer 里再触发软件中断(下篇文章将详细解释 Linux 中 frame 的走向),ring buffer 是网卡和驱动程序共享,是设备里的内存,但是对操作系统是可见的,因为看到 linux 内核源码里网卡驱动程序是使用 kcalloc 来分配的空间,所以 ring buffer 一般都有上限,另外这个 ring buffer size,表示的应该是能存储的 frame 的个数,而不是字节大小。另外有些系统的 ethtool 命令 并不能改变 ring parameters 来设置 ring buffer 的大小,暂时不知道为什么,可能是驱动不支持。)
The netcards copy the frame to the FIFO buffer zone inside the netcard, triggering hardware disruptions. (If the linuffer netcards are used to allocate space, it seems that the linuffer can trigger software interruptions first (the next article will explain the direction of the ring buffer), the ring buffer is a network card and driver sharing, and it is a memory in the device, but it is visible for the operating system, because the linux inner nuclear source card driver is a kcaloc distribution space, so the ring buffer usually has a ceiling, and the ring buffer size is a number that can be stored, not a word size. Some other systems ethtool commands do not change the size of the ring grids to set the ring buffer, which may not be supported for a while.)
网卡驱动程序通过硬中断处理函数,构建 sk_buff,把 frame 从网卡 FIFO 拷贝到内存 skb 中,接下来交给内核处理。(支持 napi 的网卡应该是直接放在 ring buffer,不触发硬中断,直接使用软中断,拷贝 ring buffer 里的数据,直接输送给上层处理,每个网卡在一次软中断处理过程能处理 weight 个 frame)
The netcard driver builds sk_buff to copy the frame from the net card FIFO to the memory skb and then hands it over to the kernel. (The card that supports napi should be placed directly in the ring buffer, without triggering the hard break, direct use of the data in the copy ring buffer, direct transmission to the upper layer, and each network card can handle the weights during a soft break process.)
过程中,网卡芯片对 frame 进行了 MAC 过滤,以减小系统负荷。(除了混杂模式)
During the process, the webcard chip filters the frame MAC to reduce system loads. (Except mixing mode)
网卡中断处理函数
产生中断的每个设备都有一个相应的中断处理程序,是设备驱动程序的一部分。每个网卡都有一个中断处理程序,用于通知网卡该中断已经被接收了,以及把网卡缓冲区的数据包拷贝到内存中。
Each device that caused the interruption has a corresponding disconnection process, which is part of the device driver process. Each network card has an interruption process, which informs the network card that the interruption has been received, and a copy of the data package in the netcard buffer zone.
当网卡接收来自网络的数据包时,需要通知内核数据包到了。网卡立即发出中断。内核通过执行网卡已注册的中断处理函数来做出应答。中断处理程序开始执行,通知硬件,拷贝最新的网络数据包到内存,然后读取网卡更多的数据包。
When a network card receives data packages from the network, it is required to notify the kernel that the package has arrived. The kernel is immediately terminated. The kernel is answered by executing the network card's registered interruption processing function. The interruption processor starts, notifys hardware, copys the latest network data package to memory, and then reads the network card's additional data packs.
这些都是重要、紧迫而又与硬件相关的工作。内核通常需要快速的拷贝网络数据包到系统内存,因为网卡上接收网络数据包的缓存大小固定,而且相比系统内存也要小得多。所以上述拷贝动作一旦被延迟,必然造成网卡 FIFO 缓存溢出 - 进入的数据包占满了网卡的缓存,后续的包只能被丢弃,这也应该就是 ifconfig 里的 overrun 的来源。
These are important, urgent, and hardware-related tasks. The kernel usually requires a fast copy of the network data package to be stored in the system, because the cache size of the network data package on the web card is fixed and much smaller than in the system. So, if the above-mentioned copying action is delayed, it is bound to cause the net card FIFO cache to spill out -- the incoming data package is filled with the net card cache and the subsequent package can only be discarded, which should be the source of the overrun in ifconfig.
当网络数据包被拷贝到系统内存后,中断的任务算是完成了,这时它把控制权交还给被系统中断前运行的程序。
When the web-based data packs are copied into the system, the interrupted task is sort of completed, at which point it returns control to the procedure that operated before the system was interrupted.
缓冲区访问
网卡的内核缓冲区,是在 PC 内存中,由内核控制,而网卡会有 FIFO 缓冲区,或者 ring buffer,这应该将两者区分开。FIFO 比较小,里面有数据便会尽量将数据存在内核缓冲中。
The kernel buffer of the netcard is in the PC memory, controlled by the kernel, and the netcard will have a FIFO buffer zone, or ring buffer, which should separate the two. FIFO is smaller, and the data in it will try to contain the data in the kernel buffer.
网卡中的缓冲区既不属于内核空间,也不属于用户空间。它属于硬件缓冲,允许网卡与操作系统之间有个缓冲;
The buffer zone in the netcard is neither kernel nor user space. It is a hardware buffer that allows a buffer between the netcard and the operating system;
内核缓冲区在内核空间,在内存中,用于内核程序,做为读自或写往硬件的数据缓冲区;
(a) Inner nuclear buffer zones, where they are stored, are used for kernel procedures and are used as data buffers for reading or writing to hardware;
用户缓冲区在用户空间,在内存中,用于用户程序,做为读自或写往硬件的数据缓冲区;
User buffers are used in user space, in memory, for user programs and as data buffers for reading and writing to hardware;
另外,为了加快数据的交互,可以将内核缓冲区映射到用户空间,这样,内核程序和用户程序就可以同时访问这一区间了。
In addition, in order to accelerate the interaction of data, the kernel buffer zone can be mapped into the user space so that both kernel and user programs can access the area simultaneously.
对于有 ring buffer 的网卡,ring buffer 是由驱动与网卡共享的,所以内核可以直接访问 ring buffer,一般拷贝 frames 的副本到自己的内核空间进行处理(deliver 到上层协议,之后的一个个 skb 就是按 skb 的指针传递方式传递,直到用户获得数据,所以,对于 ring buffer 网卡,大量拷贝发生在 frame 从 ring buffer 传递到内核控制的计算机内存里)。
For webcards with ring buffer, the kernel is shared with the netcard by the driver, so that the kernel can directly access the rim buffer, a copy of which is usually copied into its own kernel space (deliver to the upper-level protocol, the next skb is passed by the skb's pointer until the user obtains the data, so for ring buffer webcards, a large number of copies are found in the computer memory of the kernel controlled by the kernel.
网卡工作在数据链路层,数据量链路层,会做一些校验,封装成帧。我们可以查看校验是否出错,确定传输是否存在问题。然后从软件层面,是否因为缓冲区太小丢包。
The net cards work on the data chain layer, the data volume chain layer, and they do some checks and wrap them up into frames. We can check whether there are errors in the check and determine if there are problems with the transmission. Then, at the software level, whether the buffer zone is too small to drop the package.
先查看硬件情况
一台机器经常收到丢包的报警,先看看最底层的有没有问题:
A machine often receives a drop-in call, first to see if there's any problem at the bottom:
- 查看工作模式是否正常
- 查看检验是否正常
Speed,Duplex,CRC 之类的都没问题,基本可以排除物理层面的干扰。
Speed, Duplex, CRC, etc., are fine, and interference at the physical level can be largely ruled out.
overruns 和 buffer size
Red Hat 官方解决思路
Issue
Why rx_crc_errors incrementing in the receive counter of ethtool -S output?
Resolution
- Change the cable.
- Check switch configuration.
- Change the network interface card.
Root Cause
- Most of the time incrementing the value of rx_crc_errors means the problem is in Layer-1 of the networking model.
- When a packet is received at the interface, it goes through a data integrity check which is called cyclic redundancy check. If the packet fails in that check, it is marked as rx_crc_errors.
- The switch was forcing the NIC to operate in half-duplex mode. Fixing the switch to tell the NIC to operate in full-duplex mode have resolved the issue.
Diagnostic Steps
Check ethtool -S output and find where are the drops and errors.
Check the numbers corresponding to rx_crc_errors.
显示了 p1p1 的接口类型,连接模式,速率等等信息,以及当前是否连接了网线(如果是网线 Supported ports 就是 TP,如果是光纤则显示 Fiber),这里例举下 3 个重要关键词:
Displays the type of interface for p1p1, connection mode, speed, etc., and whether the network line is currently connected (if it is a network supported port is a TP and if it is a fibre optic it is a Fiber), for example, three key words:
Supported ports: [FIBRE] Speed: 10000Mb/s Link detected: yes
ethtool[3]
Counters Troubleshooting for Linux Driver[4]
Why do I see rx_crc_errors in ethtool output?[5]
ping 请求错误分析[6]
ping Request Error Analysis [6]
ifconfig 命令详解[7]
Ifconfig command details [7]
ethtool 命令详解[8]
Ethtool command details [8]
ethtool 解决网卡丢包严重和网卡原理[9]
Ethtool solves the serious loss of net cards and the principles of net cards [9]
脚注:
Footnotes:
[1] RPS 和 RFS 网卡多队列性能调优实践: https://wsgzao.github.io/post/rps/
[1] RPS and RFS Netcardo Queue Practice: https://wsgzao.github.io/post/rps/
[2] 如何使用 MTR 诊断网络问题: https://wsgzao.github.io/post/mtr/
[2] How to use the MTR diagnostic network: https://wsgzao.github.io/post/mtr/
[3] ethtool: https://mirrors.edge.kernel.org/pub/software/network/ethtool/
[4] Counters Troubleshooting for Linux Driver: https://community.mellanox.com/s/article/counters-troubleshooting-for-linux-driver
[5] Why do I see rx_crc_errors in ethtool output?: https://access.redhat.com/solutions/154543
[6] ping 请求错误分析: https://blog.csdn.net/u011857683/article/details/83663316
[6] ping Request error analysis: https://blog.csdn.net/u01185783/article/details/83663316
[7] ifconfig 命令详解: https://blog.csdn.net/u011857683/article/details/83758503
[7] Ifconfig command details: https://blog.csdn.net/u0118857683/article/details/83758503
[8] ethtool 命令详解: https://blog.csdn.net/u011857683/article/details/83758689
[8] Ethtool command details: https://blog.csdn.net/u01185783/article/details/83758689
[9] ethtool 解决网卡丢包严重和网卡原理: https://blog.csdn.net/u011857683/article/details/83758869
[9] Ethtool solves the serious loss of net cards and the principles of net cards: https://blog.csdn.net/u0118857683/article/details/83758869
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论