16 ms·
Why do we use the Linux kernel's TCP stack? (2016)
- the_only_law 6y agoCould it perhaps be even faster just to build the entire stack in hardware perhaps with an FPGA accelerator or even a custom card? I know TOE[1] is a thing, but from what I read Linux doesn't really support it. [1]https://en.m.wikipedia.org/wiki/TCP_offload_engine https://en.m.wikipedia.org/wiki/TCP_offload_engine
- dijit 6y agoThat is the case, but you need some DMA in the OS, so it’s up to the operating system driver to expose some portion of memory to data coming in with these means. The difference between non-DMA and DMA is staggering.
- notacoward 6y agoThis has been done many, many times. It works extremely well for some applications, but also often becomes an operational nightmare. Let's say that you do manage to create a hardware implementation that's fully equivalent to the kernel one at some point in time, even in terms of things like configurability and monitoring. Something that has never happened before AFAIK. Then technology changes, your needs change, or there's a security issue. Now you have to go through a special update process - assuming an update is even available - to keep up. This might not be a problem for a small deployment, but in a small deployment it probably wasn't worth going the specialized route anyway. In a large deployment, anything that doesn't fit into the common update/remediation workflow is going to require special accommodation in code. Is the engineering cost plus the hardware cost worth it to the customer? Sometimes still yes, but more often no. There are many examples of companies who found out the hard way that the market for this kind of thing isn't big enough to recoup their own development and other costs. P.S. This is very similar to the arguments for/against hardware RAID controllers. For whatever reasons, rightly or wrongly, those are also steadily losing popularity. Software really is eating the world. P.P.S. In some cases, e.g. Amazon, the "smart NIC" approach is the common workflow, so the color of this argument changes. OTOH, it's also worth noting that the kinds of network filtering/virtualization/whatever that Amazon does is very specific to them and has nothing to do with any standard. They dedicate staff to support it. Bespoke ASIC/FPGA approaches aren't the same as a market in which you can sell them.
- regularfry 6y agoIf there was something that looked like a standardised FPGA coprocessor on the NIC that the kernel could write microcode to on boot, or on hot-plug, updating the FPGA would be part of a kernel upgrade.
- maven29 6y agohttps://www.theregister.com/2020/09/25/smartnic_dpu/ https://www.theregister.com/2020/09/25/smartnic_dpu/ I was under the impression that these "SmartNICs" were already commonplace in prominent public clouds. Is this a completely unrelated use-case?
- wmf 6y agoMost SmartNICs do not offload TCP; they're used for other purposes.
- luizfelberti 6y agoI think this means that the Linux kernel only doesn't support the proprietary vendor-specific ASIC offloading mechanisms. Linux already supports offloading even arbitrary eBPF/XDP programs to compatible NICs, which is already extensively used for DDoS mitigation (Cloudflare) or even Load Balancing (Facebook). I'd be surprised if it didn't leverage these mechanisms to offload the kernel's own network stack as well, but haven't really checked to confirm...
- tsimionescu 6y agoI think implementing a general-purpose TCP/IP stack entirely in an FPGA is not likely to be very good return on investment, especially if you'll want stuff like filtering, (selective) packet capture etc. However, specialized stacks for certain purposes are a relatively common thing. For example, most L23 network testing solutions use FPGAs to generate and receive massive amounts of (almost) stateless traffic (think line rate for 400GE using just 2 machines), while performing certain kinds of analysis on it (latency, loss). But these are usually just Ethernet frames or maybe IP datagrams with random payloads.
- dmurray 6y agoPrevious discussion (linked by the author) https://news.ycombinator.com/item?id=12021195 https://news.ycombinator.com/item?id=12021195
- ignoramous 6y agoRelated discussion: https://news.ycombinator.com/item?id=19418997 https://news.ycombinator.com/item?id=19418997
- jvns 6y agoMarek Majkowski wrote a response to this: "Why we use the Linux kernel's TCP stack" https://blog.cloudflare.com/why-we-use-the-linux-kernels-tcp-stack/ https://blog.cloudflare.com/why-we-use-the-linux-kernels-tcp...
- tekknolagi 6y agoThis is mentioned in the article.
- jvns 6y agoyeah, I wrote this comment and then realized that I should add it to the original post too :)
- NelsonMinar 6y agoI think they meant "this is mentioned in your article" ;-)
- tekknolagi 6y agoOh! Sorry about that. I didn't notice.
- rrss 6y agoI wonder if cloudflare is using mellanox libvma these days. The article notes they were using ef_vi on solarflare NICs at the time, but it looks like they switched to mellanox NICs in their 9th and 10th generation servers. https://github.com/Mellanox/libvma https://github.com/Mellanox/libvma https://blog.cloudflare.com/a-tour-inside-cloudflares-g9-servers/ https://blog.cloudflare.com/a-tour-inside-cloudflares-g9-ser...
- DenseComet 6y agoThey built a utility called Floodgate that used that solarflare feature, but it seems that they switched away from that and any vendor specific tech to using XDP a while back. https://blog.cloudflare.com/l4drop-xdp-ebpf-based-ddos-mitigations/ https://blog.cloudflare.com/l4drop-xdp-ebpf-based-ddos-mitig...
- notacoward 6y agoThere's no simple slam-dunk on either side of this issue, but I was a bit surprised that some of the more common arguments that I've heard in favor of kernel networking didn't get mentioned. The #1 reason I've heard is features. When you use the kernel stack you have a dizzying array of protocols, congestion-control algorithms, and other features available to you. You have all sorts filtering and rerouting and rate-control features, from old iptables to netfilter and eBPF. There's all sorts of familiar monitoring. User-space stacks never duplicate all of this, nor should they. They should support their own use cases, no more, but that means there will still be a lot of people who are better off with the kernel stack. The #2 reason I've heard is that the kernel stack is known to work in a broad variety of environments. User-space stacks are often used in constrained environments where the device only has to communicate in certain ways with certain peers across a certain network topology. The kernel stack is known to work in a vast array of combinations simultaneously with less need for local debugging. There are also security aspects here. For example, a good TCP/IP stack will have features to mitigate DoS attacks both inbound and outbound, whereas some user-space stacks do things like "forget to implement" proper congestion control so they can DoS others. Whatever vulnerabilities do exist in the kernel stack tend to become known and fixed rather quickly. In your user-space stack? Good luck. I'm not actually arguing that more people should use the kernel stack. There are plenty of good reasons to go the other route when appropriate. I'm just trying to present arguments I've seen and which should be considered when making that choice. The OP seems to look at things from a bit of a "single machine, single purpose" POV, which naturally skews the outcome toward user-space stacks, but in an era of virtualization and containers we need to consider multi-purpose machines as well.
- regularfry 6y agoWouldn't both of these be neutralised somewhat if you took a rump-kernel approach of some sort? Use a real kernel network stack, just run it in user-space with whatever parts you don't need ripped out?
- ohazi 6y agoThese are the same reasons you should opt for an SoC that can run Linux over a "wifi microcontroller" if your embedded project needs networking and has the cost and power budget to support it. The Linux community has spent the last 25 years finding and fixing bugs that you absolutely will encounter if you use the Microchip or Espressif or SiLabs networking stack.
- secondcoming 6y agoI looked at DPDK for our GCP instances. It seems a bit fussy and, from my understanding of it, requires a second VPC for the second network interface. Also, one of the user-space stacks seems to be available via binary blob only. I'm hoping io_uring will be good enough for higher QPS. One concern with custom HTTP handling is that HTTP/2 and HTTP/3 seem to be non-trivial to hand-roll. HTTP/1 is a pretty simple request-reply design.
- tsimionescu 6y agoHTTP is normally handled entirely in user space, both on the client and the server side, so I don't think it has much to add to this discussion.
- secondcoming 6y agoHow do you think the packets containing HTTP information get to user space?
- tsimionescu 6y agoI mean, the TCP/IP stack doesn't normally know much about what the TCP payload is. For example, there is no logic in the TCP/IP stack that implements the TLS side, no logic that knows about what an HTTP header is, no logic that knows how HTTP/2 multiplexes requests over TCP connections etc. Sure, the kernel is implementing the TCP/IP stack, but once the right socket has been identified based on IP address and TCP port, and once the packet has been re-assembled, all checksums verified, and the ACK sent to the sender, the bytes of the TCP payload are delivered to the user-space read() or equivalent call, and it's up to user space code to interpret that as an encrypted HTTP request or response, decide if the entire request has been received etc. This is why OpenSSL or GnuTLS are libraries that you can find on your system. And the actual HTTP implementation is part of Apache or NGINX or other web servers, or by browsers and other clients.
- secondcoming 6y ago
- IgorPartola 6y agoA bunch of years ago I ended up writing a userpsace C program to essentially split incoming UDP packets to multiple destinations. I could have probably done that with iptables or some such but partially this was a fun exercise in writing a small C program that did big things. The company I worked for relied on these UDP packets as they were a core part of our business and as we migrated from our platform version 1 to the newly developed version 2 we needed both systems to receive the same stream of data. My little program I called wedge did that really well, down to spoofing the sender’s IP address. This allowed us to make a seamless migration of a huge number of customers without any downtime. I say all this because I highly recommend playing with Linux’s raw packet interface and learning the structure of IP packets to do so. You can really gleam a ton of interesting info about it that way. Writing your own TCP stack was outside of what I needed but even the comparatively much simpler task of routing UDP packets was really fun and rewarding.
- vlovich123 6y agoWhen you saw raw packets due you mean the libc network APIs or raw sockets? It sounded like the latter but for anyone unaware such sockets require root/special privileges. It’s cool but of more limited general value and can be a security exploit as a user space exploit can launch really nasty network attacks that are otherwise impossible (hence why it’s not made available to user space).
- antoinealb 6y agoThese days can't you do it with CAP_NET_RAW linux capabilities ? It is what is used for ping traceroute et al. if I recall correctly
- vlovich123 6y agoSure but you still need to grant your binary that (eg scripting languages need that for the interpreter itself which isn’t great since all programs get it) and it’s just the administrator of the machine going “I know this is dangerous but I’m deciding to do it anyway”. It doesn’t address the underlying issues (eg if I compromise your binary with this capability I have some pretty powerful capabilities including sniffing all traffic on the machine through a promiscuous socket).
- jeffbee 6y agoThe kernel TCP stack made a lot of sense when processes were almost as large as machines. When you had 1 CPU and 32MB of memory to run a web server then why wouldn't you use the kernel stack? But these days the host is as large as what we used to consider entire networks. You can easily have 256 CPU cores in a box now, and that box will be running hundreds of different processes at a time. Not all of those processes will be served equally well by whatever constants are defined in the kernel, and by whatever system-wide tuning parameters are available. And yet, there's still the problem that the box has one or a few hardware network interfaces, and something has to manage access to those resources. The architecture described at [1] is an interesting solution to this problem which avoids kernel networking, mediates NIC access, and provides custom stacks and parameters to each application. 1: Snap: a microkernel approach to host networking https://blog.acolyer.org/2019/11/11/snap-networking/ https://blog.acolyer.org/2019/11/11/snap-networking/
- the8472 6y ago> The TCP standard is evolving, and if you have to always use your kernel’s TCP stack, that means you can NEVER EVOLVE. ... if you don't update your kernel.
- throwaway894345 6y agoThat quote was in context of Android vendors who don’t update their kernel. So you’re making the same point as the article.
- the8472 6y agoI know, but most people are not in charge of an ecosystem where they don't manage the hardware but are in the position of deploying a userspace network stack with kernel bypass. So it is a fairly narrow reason.
- darksaints 6y agoWhat tasks of a TCP stack actually need to coordinate between processes, or in other words, actually need a kernel? From what I understand, complete kernel bypass networking is only really feasible if you only have a single process that is using the network. And with a monolithic kernel, the TCP stack needs the kernel for port allocation and load balancing the demands on the network interface. Okay, that makes sense, but why can't most of the processing be offloaded to userspace, with the kernel just doing those specific tasks? Port allocation should happen very rarely, and throttling should only ever happen when the network interface is saturated. Wouldn't it be better to limit the kernel to just those tasks, and offload the rest to userspace?
- MikeTheGreat 6y agoI don't have great references, but I know that this has been researched in the past. For example: "U-Net: A User-Level Network Interface for Parallel and Distributed Computing" [0] ------ [0] https://www.cs.rice.edu/~alc/old/comp520/papers/U-Net%20A%20User.pdf https://www.cs.rice.edu/~alc/old/comp520/papers/U-Net%20A%20...
- tsimionescu 6y agoThere are many ways to look at this. One of the simpler ones is looking at the Rx side: a frame has arrived on the network interface. Something needs to decode that and decide what to do with it, perhaps dropping it immediately. The TCP/IP stack will interpret the Ethernet headers, drop the frame if it is not addressed to any of the machines, check checksums, handle ARP requests etc. Also, the stack would buffer the frames until it can reassemble an entire IPv4 datagram out of them. Then, it will perform similar checks on the datagram destination address as above. Finally, with the IP datagram in hand, we can decide whether to hand the payload over to the TCP handler, the UDP handler, the ICMP handler, or perhaps others. Of course, we need some TCP knowledge as well, to allow multiple TCP handlers to co-exist, choosing the right one to forward the packet to by port. At all of these steps there are other concerns that need to be handled. Network security tools should get to look at the IP addresses and MACs to decide if the packet is accepted at all. Raw sockets created by user space can receive the Ethernet frames or IP datagrams directly, before other processing. There can also be contention on those as well (e.g. Maybe 2 processes both open a raw socket on the same NIC - who gets the bytes?). Multiple NICs on the same machine also need to be shared and load-balanced. The Linux kernel for example handles routing between these, even applying networking decisions such as advertising IPs from one NIC on other NICs when it thinks it can improve delivery rates. Now, all of these can and have been done in user-space for various reasons. But it's important to understand that there is a large amount of processing. Also, given the importance of networking to IPC and security, there are also security considerations at almost every step of the way. For example, if two privileged processes on the same machine are communicating over a virtual NIC with static IP allocation, can they expect their communication to be isolated from a non-privileged process running on the same machine, or do they need to use an encrypted&authenticated scheme? A centralized TCP/IP stack can make that guarantee, a per-process stack can't.
- mathieubordere 6y agoixy is a nice project for developing an understanding of user-space network drivers. https://github.com/emmericp/ixy https://github.com/emmericp/ixy
- dnautics 6y agoI remember when the Beos moved networking from userland to the kernel because userland was too slow. Are we coming full circle?
- jeffbee 6y agoThat's pretty misleading. BeOS moved from a trashy single-threaded userspace network stack that was bodged on to a much better implementation that happened to be in the kernel.
- dnautics 6y agoright, that was around the time that everyone using the BSD network stack became a thing. Did you mean to say thrashy (not that it's wasn't also trashy)? Because IIRC that was the main problem. There were issues with, for example, the memory mapped area leaving the caches of the resident core that the process lived on; and also generally BeOS was terrible at pinning processes to CPU cores. And I recall that the sentiment was also that the network stack needed low latency in general and that it was rather difficult to get a good implementation of a userland network for these reasons. I imagine that at least some of these concerns may play out in more contemporary userland network stacks. Possibly, worse so if you don't know what is going on with noisy neighbors in containerized or even vm-ized deployments (though I would guess that Amazon/GCP but maybe not Azure does a good job of quotaing/throttling vms)
- ncmncm 6y agoI do kernel bypass in production. In my case, I am getting UDP (multicast) packets at a sometimes ferocious rate, that need various kinds of processing done, and all captured, with nanosecond timestamps, to disk. The key is to get the kernel not involved at all. You use NICs from Solarflare (now a Xilinx company, which they used to be a customer of); or Exablaze (now owned by Cisco); Netronome; used to be, Mellanox (now owned by NVidia); or even Napatech (expensive). The NIC driver sets up a ring buffer in DMA memory and just starts dumping packets into it in real time. Each packet gets a little bit of metadata: a nanosecond timestamp, byte count, checksum. The NIC might be filtering by IP address or port, to distribute incoming packets to different ring buffers. The ring buffer is typically a few megabytes, enough for packets to be there for a few ms before they get overwritten. Your program has to watch this ring buffer for updates, and do whatever it needs to do before the packet gets overwritten. I memcpy them to a big-ass ring buffer, say 8GB of hugepages, and then other unprivileged processes can pick over them for interesting bits, with more leeway for stalls, and can be started and stopped independently. The process watching the NIC has to be protected against interruptions from the kernel, which involves a mess of kernel boot options -- nohz_full, isolcpus, rcu_nocbs -- because kernels are very jealous of their privilege to stall any process and steal its core for their own purposes. The program needs to do no system calls after startup, and not write to any mmapped memory backed by actual disks (/dev/shm and /dev/hugepages are ok), or the kernel will stall it anyway, boot flags notwithstanding. Typically each NIC maker has its own kernel-bypass driver and library, often open-source, that understands its ring buffer. Usually they provide a .so your program can LD_PRELOAD to divert regular socket calls into their library, that you will ignore unless you want to, e.g., send out TCP traffic. NICs have unique features. Intel and others implement a more-or-less portable DPDK interface to their library. Solarflare provides an Onload implementation, and pretty smart hardware filters. ExaNIC has less-smart filters, but delivers packets 120 bytes at a time, so you can start work on a packet before it has all arrived. A Netronome NIC can run eBPF code on a core in the NIC, against packets not even copied to host memory yet. Napatech lets you mess with the filters from the command line, while it's running, and can send packets on a nanosecond-resolution schedule. Most let you queue up packets and trigger sending certain ones on a dime. You could have a dozen packets with different possible choices, and send only the one you later determine is right. Lately there is a kernel service, AF_XDP, that is supposed to be a portable, kernel-maintainer approved way to do some of what the proprietary libraries do. I haven't tried it. Getting reliable nanosecond-resolution timestamps is tricky. Nowadays everything is referenced to atomic clocks on GPS satellites. So, you need a GPS receiver, and a way for the NIC to know what it says. ExaNICs have a receiver on board. Often there is a connector for "PPS" input, expecting a clock rising or falling edge at a known offset from the second boundary. A protocol, PTP, provides ~microsecond resolution, but burns one of your 10Gbps ports. Some switches will process PTP, PPS, or GPS, and tag packets with various non-standard annotations. If you need to do trickier things, several NICs have FPGAs you can program yourself.
- kureikain 6y agoIf anyone wants to see how to write a simple userspace tcp stack, here is a good one https://www.saminiir.com/lets-code-tcp-ip-stack-1-ethernet-arp/ https://www.saminiir.com/lets-code-tcp-ip-stack-1-ethernet-a...
- spicyramen 6y agoI normally use Tornado or Flask, as many many developers we live at a higher layer in the OSI layer, what would be a good starting point to plugin my app to a new TCP stack, Specifically for example a Jupyter Notebook.
- airocker 6y agoYou mean write your app as a Jupiter notebook app?
- GoOnThenDoTell 6y agoYou don’t need to worry about TCP then
- nicolaslem 6y agoYou can probably find a user space TCP implementation that you can use with LD_PRELOAD. In this case you wouldn't need to do anything to make your app use it. If instead you want a networking stack written in Python, for WSGI based frameworks like Flask it is enough to write a WSGI server that instead of using the `socket` module uses your custom stack. It will be compatible with the vast majority of Python web apps thanks to WSGI. That being said it's very unlikely that you will notice any performance improvement. In the case of a networking stack written in Python it would even be way worse than the kernel implementation.
- chmod775 6y agoSince the HN crowd will be mostly running HTTP(S) servers, the answer is that you probably should just be using the kernel's TCP stack, because however slow it is, it'll only use a tiny fraction of the CPU time your HTTP(S) server will be using, especially if you're doing SSL termination on the same machine. Rolling a custom TCP stack is only interesting when the amount of work per TCP connection is minimal and you're in the territory of handling half a million or more concurrent connections. Think of caching servers etc. However if you had that many concurrent connections to some website you'd already be comfortably sitting in the Alexa top 1k of internet services.
- secondcoming 6y agoWouldn't it be more common to have SSL termination done on a load balancer that forwards the traffic on to a group of instances?
- Matthias247 6y agoIt depends on your overall system setup, and how much you trust the path between your LB and the actual webservers. You might even have TLS on both paths. Maybe using different certificates
- chmod775 6y agoDepends on where your main workload is. On my servers haproxy (doing SSL termination) accounts for about 70% of CPU time spent. Doing SSL termination on my load balancers would mean I would need about 2 "load balancers" for every 1 server - which could be a legitimate setup in other circumstances though. If I needed to do load balancing via an intermediate server, it would make more sense for me to load-balance on the raw (i.e. TLS traffic) TCP layer.
- kuharich 6y agoPast comments: https://news.ycombinator.com/item?id=12021195 https://news.ycombinator.com/item?id=12021195
- sally1620 6y agoIn 2020, why not just go all the way to QUIC?
- wmf 6y agoIt depends whether you control both ends. If you need to talk to TCP clients then you have to use TCP.
- Matthias247 6y ago1. Your peer might use TCP and not support QUIC 2. It hasn't even finished standarization 3. It can be up to 5x less efficient than using TCP+TLS (personal measurement). If you don't require TLS it can be even worse. 4. It doesn't even answer the question that the article asks: You still have lots of options on how to get packets for your Quic endpoint to the application. You can use regular socket APIs, you can do some trickery using parts of the kernel stack (using eBPF). Or you can even do even more kernel bypass and using AF_XDP to get data into the app.
- cesaref 6y agoAs there was mention of SolarFlare cards, i'd just like to say that I had much success in a previous life with their products for reducing latency in trading systems - the products are really robust, and OpenOnload works really well. At the most basic level you can run an existing binary under Onload (which basically uses an LD_LIBRARY_PATH to direct the socket system calls to their own stack) and get a significant performance improvement, especially if you are careful about how the card is configured, and to ensure your app is running on cores on the same socket that the network card is connected to. Taking it further, you can use a zero copy API to get inbound packets whilst they are still in the network card buffer, and do very cool things with both hardware timestamping and pre-caching of packets on the network card memory (sort of like a message template) which can reduce the latency to sending responses if you have a set of stock responses you want to supply. Anyhow, do check them out if you are into that sort of thing.
- hawk_ 6y agoi am into that kind of thing :-) if i wanted to try this kind of stuff at home, i.e. buying any necessary hardware myself, how much do you reckon the whole setup would cost? any suggestions on used NICs/switches to try this with?
- ncmncm 6y agoUsed Solarflare cards can be had for ~$300. Warning, the fancier features are disabled on cheap cards; you pay Solarflare to upgrade the firmware to get access. The kernel-bypass software and kernel driver (for setup) are usually free to download. A new ExaNIC might be $2000? last I checked. Netronome, maybe $1200? You can get experience with ordinary Intel hardware and AF_XDP or DPDK.
- scott00 6y agoI'll add that you should look for the PLUS skus, ie SFN-8522-PLUS. They come with all the fun stuff. Might also want to look at Packet. They offer bare metal cloud servers by the hour, some of which have Mellanox NICs in them[0]. I'm less familiar with Mellanox than SolarFlare, but I think they have a similar feature set. [0] https://www.packet.com/cloud/servers/c3-small/ https://www.packet.com/cloud/servers/c3-small/
- oscargrouch 6y agoIn my opinion OS's should only give you mapped buffers of memory, where it defines the source.. a network driver or a file in the disk, a gpu memory, or simply the heap and the userspace applications would deal with. OS's are doing too much in my opinion and the problem is that the status-quo of applications expect them to do it, so theres no clear way out of this. If im not mistaken, theres a paper with the 'exokernel' design from the late nineties, built from a NetBSD, that describes a little bit about this approach, if you are curious. Our OS's should be like that, and i bet they would be so much better giving they would be able to concentrate in being good in a much smaller spectrum, while applications would probably negotiate less with the kernel, giving us a better performance and stability overall. TPC and sockets would be a library, and once we need to prototype new shiny stuff, it would be much better to get the needed adoption by just linking to the appropriate library. Projects like QUIC and gRPC would be much more viral, and i bet they could even appear earlier if some part of the stack were not frozen right on the kernel. I guess, this will become more evident with time, as a lot of those technologies on the kernel will become deprecated, while in the heat of modernism it look a great idea to stuck that thing that "everybody uses" on kernel space.
- victor106 6y agoThis is very timely. Anyone know of a tool/command that can measure how much traffic (mb/s) is being sent to an application?
- l3s2d 6y agoI like to use nethogs for quickly looking at per-application traffic. Requires root. Related: trickle (per application bandwidth shaping) and wondershaper (per interface bandwidth shaping) are great for testing.
- btschaegg 6y agoAs a simple solution, you could poll `/proc/<pid>/net/dev` and calculate it yourself (from the difference of the total received bytes between two polls). `nethogs` is also useful. If you'd like to do something more specialized, you could build something with a couple of lines in `bpftrace`, but you'll have to put in more work there if you're not familiar with it. The book "BPF Performance Tools" by Brendan Gregg is a good introduction there. Of course, for a simple overview, that would be way overkill, though :)
- btschaegg 6y agoAddendum: Since I'm somewhat bored, anyway :) I whipped up a simple shell script for the polling thing. Usage: ./traffic.sh <pid> Copy-pastable for your terminal (dollar signs signify your prompt, obviously): $ cat <<'EOF' > traffic.sh #!/usr/bin/env bash pid=$1 set -euo pipefail while true; do date +'Time: %H:%M:%S' sed '/:/!d' /proc/$pid/net/dev echo sleep 1 done \ | awk ' BEGIN { MB = 1024*1024.0; } /^Time/ || /^$/ { print $0; next; } seen[$1] { rx_mbps = ($2 - rx[$1]) / MB; tx_mbps = ($11 - tx[$1]) / MB; printf("%10s Rx: %8.1f MiB/s, Tx: %8.1f MiB/s\n", $1, rx_mbps, tx_mbps); } { seen[$1] = 1; rx[$1] = $2; tx[$1] = $11; } ' EOF $ chmod +x traffic.sh
- victor106 6y agoThank you. Very helpful
- JoeAltmaier 6y agoOther reasons: on a cloud server with many VMs vying for use of the network, it can help if they coordinate their traffic. Each OS running in a VM can use a special TCP stack that responds to the VM monitor's network backpressure etc.
- aeontech 6y agoPreviously discussed on HN here with many more interesting comments https://news.ycombinator.com/item?id=12021195 https://news.ycombinator.com/item?id=12021195
- nabla9 6y agoReally good answers there. Linux Kernel TCP stack is not just a faithful implementation of the spec. It's also all that hard learned experience about Undocumented Real Internet that is constantly evolving and what must be dealt with. A bug in TCP stack can cause lots of problems for users. Taking responsibility of that is also huge task.
- brendangregg 6y agoThe Linux network stack has been rapidly evolving; nowadays we have XDP for a fast path through the kernel. But there's also a lot of moving parts that ensure high throughput on noisy networks, for different application workload patterns, to avoid bufferbloat, and to do this fairly across multiple processes/containers. I'd imagine one can develop a user-space stack that appeared better in a simple iperf microbenchmark in a lab, but would lose for real-world applications and networks (unless your real-world use case was very narrow). I'd recommend understanding the stack before choosing to replace it (the old programmer's advice about understanding a gate before removing it). I summarized the TCP send path in my next book (sysperf2, with help from Linux network engineers); it involves: - Socket send buffers - Pacing - TSO - Congestion controls - Nagle - TSQ - qdisc (optional) - GSQ - BQL - TSO (NIC) That's just the TCP send path. I'm happy to expand all these acronyms, but if any are new to you, that's kind-of my point. These parts have a purpose, and starting from scratch may lead you to eventually re-invent them all as you encounter the problems they solve.
- dilyevsky 6y agoBig fan of your books and blog! Would you say usefulness of dpdk and xdp has diminished purely from performance standpoint? I hear rumors you can do line rate on modern linux kernels these days but haven’t had a chance to try it personally yet. I think at the time maglev was originally developed google was still on 2.6 kernels...
- brendangregg 6y agoThanks. I'd expect more future uses of xdp over dpdk. As for max performance (I saw a Linux server max out multiple 10 GbE ports years ago) I'm not sure anyone right now knows what it is. We've recently added XDP, io_uring, BPF congestion control algorithms, and more. There are performance numbers for these in isolation, but I don't think anyone has performance tested (and tuned) them all working together yet. And that poses a problem: to reach maximum performance requires many engineering hours to learn and configure all of these. You'd need a large gain to justify doing this: E.g., for a system doing over 1M packets/s. This complexity may change. Look at BPF for observability: we built BCC and bpftrace so that using it was trivial. What trivial front-end/technology could we build upon XDP and io_uring to make deployment easy?
- nimish 6y agoVery few things need to be run at line rate 1.5 million packets per second on 1gbit so the effort is just not worth it I'm not competent or eager enough to want to deal with, say, Path MTU discovery on my own
- techman9 6y ago> So – this made me wonder. When we do high performance networking – why do we bother using the Linux kernel’s TCP stack at all, if it’s so expensive? What makes the Linux kernel TCP stack expensive?
- commandlinefan 6y agoLinux’s TCP stack (and any other TCP stack you’re going to run across) is general-purpose: it’s designed to handle nearly anything you might want to throw at it. All that genericity comes at a price. If you want something to be hyper-efficient, it has to be specialized to the case you’re putting it to, which usually means you have to write most of it yourself. It’s really sad how many developers actively reject writing software just because somebody else wrote something else sort of similar once.
- WarChortle 6y agoSome software you shouldn't write yourself unless you are doing it to learn only. I would put TCP stack on that list. I would never want to deploy my own TCP stack into the wild.
- Matthias247 6y ago> I would never want to deploy my own TCP stack into the wild. Why not? Because you don't trust your skillset to write one? Because you don't find time to write a good enough one? Because the existing ones are good for you? Those can all be valid reasons. But I don't buy into the general "software you shouldn't write yourself" thing. If everyone would follow that advice, we would have a lot less progression and improvements in the field. I have written custom HTTP implementations for all protocol versions and would be find deploying them in production - that's what they had been written for. I would also be fine writing a custom TCP stack or TLS stack if something requires that. But before doing that I would need to make sure I've done all my due diligence: Have I fully understood the problem - and is my solution solving it? Do I understand all the edge-cases, which might lead to serious availability or security issues later on? Is my code well tested? Obviously there is always something that one only learns later, so those checks will never be exhaustive. One good minimum check is reviewing your own implementation and tests against the one of existing implemetations, and see where it improves something and where it might miss out on something. You will also need a goal, on where you want to improve over the existing solution. If you don't have that goal, and have no way to measure/quantify it, then don't do it. Also think about how much that goal really matters for your end product. If it doesn't matter at all - then either don't build the thing or build it as a side project or learning project.
- nottorp 6y ago> Google can’t force Android vendors to rebase kernels but requires new TCP functionality such as TCP fast open. Maybe that sounded open minded and "oh shiny new feature" in 2016, these days it sounds like embrace and extend...
- shekharshan 6y agoTo implement file system in userspace or TCP stack in userspace how does the process invoke the device driver? I am sure the Kernel steps in at some point. What system calls would you invoke from userspace TCP stack to access the NIC?
- iofiiiiiiiii 6y agoMicrosoft is also working on a kernel-bypass userspace network stack: https://github.com/demikernel/demikernel https://github.com/demikernel/demikernel
- yencabulator 6y agoHTTP/3 will completely change that landscape. It's essentially a TCP-stack-equivalent inside each process. Lots of userspace TCP-stack-equivalents will be running soon, though they'll probably consists of just various versions of a couple of codebases -- your average webserver won't implement it's own low-level stack.