Uncovering Hidden TCP Throttling in the Linux Kernel

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Disclaimer: No AI was used for writing this blog. Enjoy a humanly engineered piece.

免责声明:本文并非由人工智能撰写。请享受这篇由人类精心打造的文章。

One day, a customer was complaining about slow download speeds from one of our edge servers. Since the internet is full of unpredictability, complaints like that occasionally happen, but this time, there were no error codes. We couldn’t reproduce the issue. The only thing we could see was lower traffic on the network interface. The strangest part was that it happened randomly, recovered on its own, and it didn’t affect all clients connected to that server.

有一天,一位客户抱怨我们某台边缘服务器的下载速度缓慢。由于互联网充满了不可预测性,这类投诉偶尔会发生,但这次却没有出现任何错误代码。我们无法重现该问题。我们唯一能观察到的就是网络接口上的流量较低。最奇怪的是,这种情况是随机发生的,能够自行恢复,而且并非影响连接到该服务器的所有客户端。

CDNs are usually built in a multi-layer setup. When a client requests content, the closest edge node serves it from its cache. If the content isn’t there, the edge fetches it from an upper layer. The request can travel further up until it reaches a shield node, which finally fetches the content from the customer’s origin. At CDN77, all these layers run heavily modified nginx. As we would eventually discover, nginx was an important part of this story.

内容分发网络(CDN)通常采用多层架构构建。当客户端请求内容时,最近的边缘节点会从其缓存中提供服务。如果缓存中没有该内容,边缘节点会从上层获取。请求可以进一步向上传递,直到到达屏蔽节点(shield node),该节点最终从客户的源站获取内容。在 CDN77,所有这些层都运行着经过深度修改的 nginx。正如我们最终发现的那样,nginx 是这个故事的重要组成部分。

Here’s what we could see internally about the complaint: The download speed would collapse for certain connections, stay low for hours, and then recover without anyone touching anything. In one instance, it dropped just before 3 a.m., stayed flat for five hours, and returned to normal around 8 a.m. Nothing told us why. So we started with the usual suspects: Maybe it was an ISP, peering, or transit issue; some network problem outside our infrastructure. Maybe one of the kernel’s TCP settings was misconfigured - the well-known tcp_rmem, tcp_wmem, or tcp_mem. Or perhaps something in the nginx configuration, like proxy buffering. We also considered disk I/O stalling the cache, CPU or memory saturation, something hidden in the kernel logs, or a particular kernel version.

以下是我们从内部观察到的投诉情况:某些连接的下载速度会突然崩溃,保持低速数小时,然后在无人干预的情况下自行恢复。有一次,速度在凌晨 3 点前下降,保持平稳状态长达五个小时,并在早上 8 点左右恢复正常。没有任何迹象表明原因。因此,我们从常见的嫌疑对象开始排查:也许是 ISP、对等互联或传输问题;或者是我们基础设施之外的网络故障。也许是内核的某个 TCP 设置配置错误——比如众所周知的 tcp_rmem、tcp_wmem 或 tcp_mem。又或者可能是 nginx 配置中的某些问题,例如代理缓冲。我们还考虑了磁盘 I/O 导致缓存停滞、CPU 或内存饱和、内核日志中隐藏的问题,或者是特定的内核版本。

All reasonable possibilities. And every one of them led to a dead end. Over time, the issue appeared in multiple regions, ruling out a specific ISP or network path. The sysctl settings and nginx configuration were identical to those on healthy neighboring servers. So were the nginx and kernel versions. CPU utilization, memory usage, and kernel logs showed no discrepancies either. But we noticed something interesting: The issue only appeared on HTTP cache misses. So, when the bug was happening and actively throttling, we launched a curl command on that server to test the download speed. But it was working; only nginx downloaded it slowly. We also noticed that rebooting the server fixed the issue for quite some time. Until, of course, it happened again. At this point, we had a pretty strange combination of symptoms. The problem was: Dynamic, Self-healing, Affected only nginx processes, Appeared on cache-miss connections. Static config reviews weren’t getting us anywhere. We needed to see what was actually happening.

这些都是合理的可能性,但每一个都陷入了死胡同。随着时间的推移,该问题出现在多个地区,排除了特定 ISP 或网络路径的可能性。sysctl 设置和 nginx 配置与健康的邻近服务器完全相同。nginx 和内核版本也是如此。CPU 利用率、内存使用情况和内核日志也没有显示出任何差异。但我们注意到一个有趣的现象:该问题仅出现在 HTTP 缓存未命中(cache miss)时。因此,当错误发生并处于主动限流状态时,我们在该服务器上运行了 curl 命令来测试下载速度。结果却是正常的;只有 nginx 下载速度缓慢。我们还注意到,重启服务器可以在相当长的一段时间内解决该问题。当然,直到它再次发生。此时,我们面临着一组非常奇怪的症状组合。这个问题是:动态的、自愈的、仅影响 nginx 进程、出现在缓存未命名的连接上。静态配置审查毫无进展。我们需要观察实际发生了什么。

Going further down the rabbit hole: Since only nginx seemed to be affected, we were suspicious of userspace. Maybe nginx was delaying its read syscalls. Maybe it wasn’t reading enough bytes per call. We were even desperate enough to suspect that epoll itself was broken. We hooked into the entry and exit points of the read and epoll related syscalls, capturing function arguments, return codes, and syscall durations. If nginx was spending too much time waiting, or doing something strange while reading data, we should have been able to see it. But the experiments showed no anomalies whatsoever. Nginx wasn’t delaying its read syscalls. It was issuing them as fast as possible. Was it reading too few bytes? No. It was reading everything available until it hit EAGAIN, exactly as expected. So userspace probably wasn’t the culprit. Time to look into the kernel.

深入探究:由于似乎只有 nginx 受到影响,我们怀疑是用户空间的问题。也许 nginx 延迟了其读取系统调用。也许它每次调用读取的字节数不够。我们甚至绝望到怀疑 epoll 本身坏了。我们挂载了读取和 epoll 相关系统调用的入口和出口点,捕获了函数参数、返回代码和系统调用持续时间。如果 nginx 在等待上花费了太多时间,或者在读取数据时做了什么奇怪的事情,我们应该能够看到。但实验没有显示任何异常。Nginx 并没有延迟其读取系统调用。它正以尽可能快的速度发出这些调用。它读取的字节数太少了吗?没有。它读取了所有可用数据,直到遇到 EAGAIN,完全符合预期。所以用户空间可能不是罪魁祸首。是时候深入内核看看了。

What if… the bug was in the kernel? We hooked a kprobe into tcp_rcv_established using bpftrace and started printing fields from struct tcp_sock that could plausibly explain a throughput collapse. Fields like rcv_ssthresh, the advertised maximum segment size, the send congestion window, app_limited, the receive window, etc. (Note: The receive window was our main suspect.)

如果……错误在内核中呢?我们使用 bpftrace 将 kprobe 挂载到 tcp_rcv_established 中,并开始打印 struct tcp_sock 中可能解释吞吐量崩溃的字段。例如 rcv_ssthresh、通告的最大段大小、发送拥塞窗口、app_limited、接收窗口等。(注意:接收窗口是我们主要的怀疑对象。)

And finally, we started seeing a pattern. The value of rcv_ssthresh was tiny. Exactly four times the advertised maximum segment size. And that value is only ever written by tcp_adjust_rcv_ssthresh(). In our case, the only viable path leading to that function was through tcp_grow_window(), but only when tcp_under_memory_pressure() returned true.

最终,我们开始看到一种模式。rcv_ssthresh 的值非常小。正好是通告的最大段大小的四倍。而该值仅由 tcp_adjust_rcv_ssthresh() 写入。在我们的案例中,通向该函数的唯一可行路径是通过 tcp_grow_window(),但前提是 tcp_under_memory_pressure() 返回 true。

But that was suspicious. Earlier in our debugging, we’d checked the SNMP counters and they reported zero memory pressure, none whatsoever. And yet, tcp_under_memory_pressure() was returning true. Something didn’t add up.

但这很可疑。在调试初期,我们检查了 SNMP 计数器,它们报告内存压力为零,完全没有压力。然而,tcp_under_memory_pressure() 却返回了 true。有些地方不对劲。

Finding the hidden rabbit: It turns out that the TCP variant of the memory pressure check is implemented differently from others. Besides checking global TCP memory pressure, it also checks whether the socket’s cgroup is under memory pressure. This additional check happens when the cgroup memory controller is loaded, which it is by default on most distributions.

寻找隐藏的兔子:事实证明,TCP 版本的内存压力检查与其他检查的实现方式不同。除了检查全局 TCP 内存压力外,它还会检查套接字的 cgroup 是否处于内存压力之下。当加载 cgroup 内存控制器时(大多数发行版默认加载),会进行这种额外的检查。