15M web servers on one VM – smoltcp, DPDK, AF_XDP

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

15M web servers on one VM – smoltcp, DPDK, AF_XDP

Put a web server online and everyone starts trying to hack it. Why? A basic rule of economics. Short supply, big demand - there aren’t enough web servers for everyone. Let’s run a web server for every human on the planet, all 8 billion of them. I couldn’t get funding, so we’re bootstrapping. The goal is to run 15 million web servers on one VM with 10GB of RAM and 1 CPU core. If we succeed, I’ll find some RAM from old microwaves around the house and we’ll scale it up so everyone can have their own web server. Demand will drop to zero and cybersecurity will be solved once and for all.

把一台 Web 服务器放到网上,每个人都会试图攻击它。为什么?这是一个基本的经济学规律。供不应求,需求巨大——Web 服务器根本不够每个人用。让我们为地球上的每个人,也就是全部 80 亿人,都运行一台 Web 服务器吧。我没能筹到资金,所以我们只能白手起家。目标是在一台拥有 10GB 内存和 1 个 CPU 核心的虚拟机上运行 1500 万台 Web 服务器。如果我们成功了,我会从家里的旧微波炉里拆些内存出来,把规模扩大,让每个人都能拥有自己的 Web 服务器。届时需求将降为零,网络安全问题也将一劳永逸地得到解决。

Instead of vibe coding it all, let’s use an ancient technique called learning. First of all, what is a web server? There are a few essential syscalls.

与其凭感觉写代码,不如使用一种古老的技术,叫做“学习”。首先,什么是 Web 服务器?它有几个必不可少的系统调用。

What is a syscall? A function that is executed by the OS kernel. Whatever fancy language or library you use for HTTP, it all comes down to those syscalls (well, yes, but actually no). Let’s use C to make it all explicit. We start listening, then read from the socket in a loop and always reply with OK.

什么是系统调用?它是操作系统内核执行的函数。无论你使用什么花哨的语言或库来处理 HTTP,最终都会归结为这些系统调用(嗯,理论上是这样,但实际上也不完全是)。让我们用 C 语言把这一切明确出来。我们开始监听,然后在循环中从套接字读取数据,并始终回复 OK。

It couldn’t be simpler: listen on a socket, accept a connection, read the data, send a reply, close the client connection, and repeat. It gives us around 65k RPS. Now that we’ve learned everything, let’s finish the article and call it a day:

这再简单不过了:监听套接字、接受连接、读取数据、发送回复、关闭客户端连接,然后重复。这能给我们带来大约 6.5 万的每秒请求数(RPS)。既然我们已经学到了一切,那就结束这篇文章,收工吧:

Hm, why did listen fail? Ah, we’ve run out of ports! We’re using port=0, so the kernel picks an available port from ip_local_port_range.

嗯,为什么监听失败了?啊,我们的端口用完了!我们使用的是 0 号端口,所以内核会从 ip_local_port_range 中选择一个可用端口。

Why are so many ports skipped? Historically, port 0 was created for outbound connections. When you call connect(‘example.com’, 80), Linux connects to the remote server (the sending part) and then allocates a random port on the host from ip_local_port_range to receive data (the receiving part). There are a few reasons the default range is small.

为什么跳过了这么多端口?从历史上看,0 号端口是为出站连接创建的。当你调用 connect(‘example.com’, 80) 时,Linux 会连接到远程服务器(发送部分),然后从 ip_local_port_range 中为宿主机分配一个随机端口来接收数据(接收部分)。默认范围较小的原因有几个。

Nothing prevents us from overriding the range and using 1-65535, but that’s still a bit short of 15 million. A UDP server and a TCP server can use the same port, so we can double that to 128k. We could run a proxy in front to forward requests to the right process (virtual hosting, yay), but these are all ancient techniques - in the AI era, whenever you have a software problem, you just throw money at the hardware. Let’s simply connect 15M/32k=450 new network cards to the server.

没有什么能阻止我们覆盖这个范围并使用 1-65535,但这距离 1500 万还差得远。UDP 服务器和 TCP 服务器可以使用同一个端口,所以我们可以将其翻倍到 12.8 万。我们可以在前端运行一个代理来将请求转发到正确的进程(虚拟主机,耶),但这些都是古老的技术——在人工智能时代,每当你遇到软件问题,你只需要往硬件上砸钱就行了。让我们简单地给服务器连接 1500 万/3.2 万 = 450 张新网卡吧。

The 65k limit comes from the TCP standard. A port is a u16, so even if we hack the kernel with find ‘*.c’ -exec sed -i ‘s/u16/u32/’ ; to support bigger port numbers, normal clients still wouldn’t be able to connect. Each listening server is identified by a (protocol, IP, port) tuple. That explains how we can use the same port for UDP and TCP. It also means we can use a bunch of different IPs for our web servers. If we connect 450 network cards to the server, we’ll get 450 unique IP addresses and be able to listen on 15 million unique addresses!

6.5 万的限制来自 TCP 标准。端口是一个 16 位无符号整数(u16),所以即使我们通过 find ‘*.c’ -exec sed -i ‘s/u16/u32/’ ; 这种方式破解内核来支持更大的端口号,普通客户端仍然无法连接。每个监听服务器都由一个(协议、IP、端口)元组标识。这解释了为什么我们可以对 UDP 和 TCP 使用相同的端口。这也意味着我们可以为我们的 Web 服务器使用一堆不同的 IP。如果我们给服务器连接 450 张网卡,我们将获得 450 个唯一的 IP 地址,并能够监听 1500 万个唯一的地址!

UPD. I’ve heard from readers that the network card is the ONLY hardware component that didn’t get 100 times more expensive in the last year. Can’t we avoid ruining that too and solve this in software?

更新:我从读者那里听说,网卡是过去一年中唯一没有涨价 100 倍的硬件组件。我们能不能别把这个也毁了,而是通过软件来解决问题?

Let’s use a beautiful technology called AnyIP, which lets us listen on a whole subnet. Many VPS and dedicated server providers routinely give you a whole TCPv6 subnet. We can simply run sudo ip -6 route add local 2001:db8:1234:5678::/64 dev lo to get 18 quintillion addresses (plus or minus one) to listen on. You can do the same for IPv4 - acquiring a subnet with 15M addresses would cost around $250 million, so adjust the command for your environment. We’ll change our server to pick the nth IP from our range:

让我们使用一种名为 AnyIP 的优美技术,它允许我们在整个子网上进行监听。许多 VPS 和专用服务器提供商通常会给你一个完整的 TCPv6 子网。我们只需运行 sudo ip -6 route add local 2001:db8:1234:5678::/64 dev lo,就能获得 1800 京(18 quintillion)个地址(正负一个)来监听。你也可以对 IPv4 做同样的事情——获取一个拥有 1500 万个地址的子网大约需要 2.5 亿美元,所以请根据你的环境调整命令。我们将修改服务器,使其从我们的范围内选择第 n 个 IP:

And now we can finally finish the article.

现在我们终于可以结束这篇文章了。

Hm, one server consumes 1.5MB of RAM, so running that many would take… 20TB of RAM, which is a few bytes more than our 10GB budget.

嗯,一台服务器消耗 1.5MB 内存,所以运行那么多服务器将需要……20TB 内存,这比我们 10GB 的预算多了那么几个字节。

By pure coincidence (what are the odds?), I explained in Running 10 million Codex CLIs on one server - LFI processes why running millions of processes creates too much overhead. The answer is to run them all in one process inside a custom runtime, much as V8 can run millions of guests in a single process. In Userland Linux scheduler in Rust, I showed how to build a specialised userland scheduler and switch between tasks with the absolute minimum amount of RAM per task.

纯属巧合(概率有多大?),我在《在一台服务器上运行 1000 万个 Codex CLI - LFI 进程》一文中解释了为什么运行数百万个进程会产生过多的开销。答案是将它们全部运行在一个自定义运行时的一个进程中,就像 V8 可以在单个进程中运行数百万个客户程序一样。在《用 Rust 编写用户态 Linux 调度器》中,我展示了如何构建一个专门的用户态调度器,并以每个任务最少的内存开销在任务之间进行切换。

TLDR: We’re using LFI for sandboxing. It gives us memory isolation and replaces all syscalls with calls to our host handler, which lets us implement I/O however we like. To schedule multiple guests, we load them all into our main process’s memory and make the CPU constantly switch between guests - we become our own micro OS.

简而言之:我们使用 LFI 进行沙箱隔离。它为我们提供了内存隔离,并将所有系统调用替换为对我们宿主处理程序的调用,这使我们可以随心所欲地实现 I/O。为了调度多个客户程序,我们将它们全部加载到主进程的内存中,并让 CPU 在客户程序之间不断切换——我们成为了自己的微型操作系统。

Since we need to save every byte and CPU cycle we can, we’re doomed to build another specialised runtime. Let’s get started. How much is 10GB divided by 15M, exactly?

由于我们需要节省每一个字节和 CPU 周期,我们注定要构建另一个专门的运行时。让我们开始吧。10GB 除以 1500 万,精确是多少?

Ten Great Britains divided by 15 million gives around half of kilo britain. Not much. If we run all our guests in our runtime, our custom scheduler needs to switch between them. What’s the absolute minimum state we need to preserve for each guest? We can ignore server initialisation and assume switches only happen within the main loop. Let’s take another look at the loop.

10 个“大不列颠”(GB)除以 1500 万大约等于半个“千不列颠”(KB)。不多。如果我们所有的客户程序都在我们的运行时中运行,我们的自定义调度器就需要在这两者之间进行切换。我们需要为每个客户程序保留的绝对最小状态是什么?我们可以忽略服务器初始化,并假设切换只发生在主循环内。让我们再看看那个循环。

This code has only two state variables, server and client. Both are simple file descriptors. If our scheduler had to suspend a guest at any syscall point in this loop, we’d need to save the following state:

这段代码只有两个状态变量:server 和 client。它们都是简单的文件描述符。如果我们的调度器必须在这个循环的任何系统调用点挂起一个客户程序,我们需要保存以下状态:

We only need 16 bytes per guest, so to run 15M guests in our runtime and be able to suspend them and switch between them, we would need 220MB of RAM. We’re playing it safe here - we don’t need the full u64 or i32, and with bit packing, we could probably get down to 8 bytes per guest. What else do we need?

每个客户程序我们只需要 16 字节,所以要在我们的运行时中运行 1500 万个客户程序并能够挂起和切换它们,我们需要 220MB 内存。我们在这里采取了保守的做法——我们不需要完整的 u64 或 i32,通过位打包,我们可能可以将每个客户程序的内存需求降至 8 字节。我们还需要什么?

Here’s what we get if we assume that only 1M of connections are active at any given moment.

如果我们假设在任何给定时刻只有 100 万个连接处于活动状态,我们得到的结果如下。

Only half of our RAM budget, yay! I don’t often exceed my own expectations. Let’s validate it:

只用了我们内存预算的一半,耶!我不常超出自己的预期。让我们验证一下:

Looks like the kernel still has some leftover comments from Linus. I’m not stupid, but I have to admit I was below average here. While an LLM is a significant boost for learning, it’s too easy to accept its answers without thinking critical

看起来内核里还有一些 Linus 留下的注释。我不笨,但我必须承认我在这里的表现低于平均水平。虽然大语言模型(LLM)对学习有很大的帮助,但如果不进行批判性思考就接受它的答案太容易了。