Cloudflare hits the limits of the Linux network stack
Summary
On Hackaday, Maya Posch summarises a report by Chris Branch on how Cloudflare pushed the Linux networking stack to its limits for soft-unicast. Because conntrack and the socket subsystem know nothing of each other, Netfilter silently rewrote connections to ports outside the assigned range. After detours via TCP_REPAIR, TCP Fast Open and disabled early demux, Cloudflare stayed with the simpler local proxy.
Two truths about the same connection
With soft-unicast, machines in many data centres share IP addresses; each machine owns only combinations of address and source-port range. A dedicated service called fish (SLATFATF) forwarded IP packets over this address space, while other services opened normal TCP sockets on the same addresses.
If a packet rewritten by SNAT already occupies a 5-tuple and a process then connects a socket with the same addresses, connect() still reports success. Conntrack silently rewrites the source port to the next free one, if necessary outside its own range, and the connection fails with apparent timeouts.
The detours and why they were dropped
Creating conntrack entries in advance via Netlink was too slow, and a stray RST packet could erase the entry. A socket with TCP_FASTOPEN_CONNECT and TCP_FASTOPEN_NO_COOKIE, on the other hand, counts as connected immediately but sends nothing, and so reserves the 5-tuple.
For incoming packets to still reach the TUN device, the ip rule priorities of the local table had to be rearranged. In production the packets still disappeared: since 2012, early demux finds the local socket during the routing decision and skips the custom route. Only net.ipv4.tcp_early_demux=0 helped, at a slightly higher softirq load.
Ideas
- Conntrack and sockets keep separate books on the same 5-tuples.
- A successful connect() does not guarantee the intended source address on the wire.
- TCP Fast Open can reserve a 5-tuple without sending a packet.
- The local routing table takes precedence over all custom rules.
- Early demux saves route lookups and bypasses custom routing rules in the process.
- A local TCP proxy costs more but is much easier to operate.
Insights
- Combining two subsystems requires a single source of truth.
- Kernel optimisations rest on assumptions that special cases silently violate.
- The measurable costs of a simple solution are often smaller than the operational risks of a clever one.
Quotes
- It is not a place for fragile solutions. (Chris Branch)
Facts
- For SNAT, Netfilter picks the first free entry and reports EPERM when none is left.
- The default ip rule 0 points to the local table.
- Early demux entered the kernel in 2012 and can be disabled via net.ipv4.tcp_early_demux.
- At Cloudflare, fish now only handles ICMP packets.
References
- Hackaday: Escaping the Linux Networking Stack at Cloudflare
- Chris Branch: So long, and thanks for all the fish (Cloudflare blog)
Remarks
- According to Cloudflare, the behaviour also occurs without SNAT or MASQUERADE rules as soon as conntrack is active.
Recommendations
- In NAT setups, check the actual source ports with conntrack -L instead of only the socket.
- Use nftrace in nftables to follow a packet's path through the tables.
- Measure CPU load before and after kernel tuning such as tcp_early_demux on identical hardware.
Found a mistake or have something to add? Get in touch.
Links to the original source and the Web Archive open in a new tab.