Skip to content

Commit

Permalink
Add Practical OCaml to the Planet (ocaml#1806)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmattio authored and sabine committed Dec 12, 2023
1 parent 1c403a6 commit bdfb6c6
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 0 deletions.
3 changes: 3 additions & 0 deletions data/planet-sources.yml
@@ -1,3 +1,6 @@
- id: practicalocaml
name: Practical OCaml
url: https://practicalocaml.com/rss/
- id: ocamlpro
name: OCamlPro
url: https://ocamlpro.com/blog/feed
Expand Down
49 changes: 49 additions & 0 deletions data/planet/hannes/re-developing-tcp-from-the-grounds-up.md
@@ -0,0 +1,49 @@
---
title: Redeveloping TCP From the Ground Up
description:
url: https://hannes.robur.coop/Posts/TCP-ns
date: 2023-11-28T21:17:01-00:00
preview_image:
featured:
authors:
- hannes
source:
---

<p>The <a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol">Transmission Control Protocol (TCP)</a> is one of the main Internet protocols. Usually spoken on top of the Internet Protocol (legacy version 4 or version 6), it provides a reliable, ordered, and error-checked stream of octets. When an application uses TCP, they get these properties for free (in contrast to UDP).</p>
<p>As common for Internet protocols, TCP is also specified in a series of so-called requests for comments (RFC). The latest revised version from August 2022 is <a href="https://datatracker.ietf.org/doc/html/rfc9293">RFC 9293</a>; the initial one was <a href="https://datatracker.ietf.org/doc/html/rfc793">RFC 793</a> from September 1981.</p>
<h1>My Brief Personal TCP Story</h1>
<p>My interest in TCP started back in 2006 when we worked on a <a href="https://github.com/dylan-hackers/network-night-vision">network stack in Dylan</a> (these days abandoned). Ever since, I wanted to understand the implementation tradeoffs in more detail, including attacks and how to prevent a TCP stack from being vulnerable.</p>
<p>In 2012, I attended ICFP in Copenhagen while a PhD student at ITU Copenhagen. There, <a href="https://www.cl.cam.ac.uk/~pes20/">Peter Sewell</a> gave an invited talk &quot;Tales From the Jungle&quot; about rigorous methods for real-world infrastructure (C semantics, hardware (concurrency) behaviour of CPUs, TCP/IP, and likely more). Working on formal specifications myself in (<a href="https://en.itu.dk/-/media/EN/Research/PhD-Programme/PhD-defences/2013/130731-Hannes-Mehnert-PhD-dissertation-finalpdf.pdf">my dissertation</a>) and having a strong interest in real systems, I was immediately hooked by his perspective.</p>
<p>To dive a bit more into <a href="https://www.cl.cam.ac.uk/~pes20/Netsem/">network semantics</a>, the work done on TCP by Peter Sewell, et. al., is a formal specification (or a model) of TCP/IP and the Unix sockets API developed in HOL4. It is a label transition system with nondeterministic choices, and the model itself is executable. It has been validated with the real world by collecting thousands of traces on Linux, Windows, and FreeBSD, which have been checked by the model for validity. This copes with the different implementations of the English prose of the RFCs. The network semantics research found several issues in existing TCP stacks and reported them upstream to have them fixed (though, there still is some special treatment, e.g., for the &quot;BSD listen bug&quot;).</p>
<p>In 2014, I joined Peter's research group in Cambridge to continue their work on the model: updating to more recent versions of HOL4 and PolyML, revising the test system to use DTrace, updating to a more recent FreeBSD network stack (from FreeBSD 4.6 to FreeBSD 10), and finally getting the <a href="https://dl.acm.org/doi/10.1145/3243650">journal paper</a> (<a href="http://www.cl.cam.ac.uk/~pes20/Netsem/paper3.pdf">author's copy</a>) published. At the same time, the <a href="https://mirage.io">MirageOS</a> melting pot was happening at University of Cambridge, where I contributed OCaml-TLS, etc., with David.</p>
<p>My intention was to understand TCP better and use the specification as a basis for a TCP stack for MirageOS. The <a href="https://github.com/mirage/mirage-tcpip">existing one</a> (which is still used) has technical debt: a high issue to number of lines ratio. The Lwt monad is ubiquitous, which makes testing and debugging pretty hard, so utilising multiple cores with OCaml Multicore won't be easy. Plus it has various resource leaks, and there is no active maintainer. But honestly, it works fine on a local network, and with well-behaved traffic. It doesn't work that well on the wild Internet with a variety of broken implementations. Apart from resource leakage, which made me implement things such as restart-on-failure in albatross, there are certain connection states which will never be exited.</p>
<h1>The Rise of <a href="https://github.com/robur-coop/utcp">&micro;TCP</a></h1>
<p>Back in Cambridge, I didn't manage to write a TCP stack based on the model, but in 2019, I restarted that work and got &micro;TCP (the formal model manually translated to OCaml) to compile and do TCP-session setup and teardown. Since it was a model that uses nondeterminism, this couldn't be translated one-to-one into an executable program, but there are places where decisions have to be made. Due to other projects, I worked only briefly in 2021 and 2022 on &micro;TCP, but finally in the Summer of 2023, I motivated myself to push &micro;TCP into a usable state. So far I've spent 25 days in 2023 on &micro;TCP. Thanks to <a href="https://tarides.com">Tarides</a> for supporting my work.</p>
<p>Since late August, we have been running some unikernels using &micro;TCP, e.g., the <a href="https://retreat.mirage.io">retreat</a> website. This allows us to observe &micro;TCP and find and solve issues that occur in the real world. It turned out that the model is not always correct (i.e., there is no retransmit timer in the close wait state, which avoids proper session teardowns). We report statistics about how many TCP connections are in which state to an influx time series database and view graphs rendered by Grafana. If there are connections that are stuck for multiple hours, this indicates a resource leak that should be addressed. Grafana was tremendously helpful to find out where to look for resource leaks. Still, there's work to understand the behaviour, look at what the model does, what &micro;TCP does, what the RFC says, and eventually what existing deployed TCP stacks do.</p>
<h1>The Secondary Nameserver Issue</h1>
<p>One of our secondary nameservers attempts to receive zones (via AXFR using TCP) from another nameserver that is currently not running. Thus it replies to each SYN packet a corresponding RST. Below I graphed the network utilisation (send data/packets is positive y-axis, receive part on the negative) over time (on the x-axis) on the left and memory usage (bytes on y-axis) over time (x-axis) on the right of our nameserver. You can observe that both increases over time, and roughly every 3 hours, the unikernel hits its configured memory limit (64 MB), crashes with *out of memory*, and is restarted. The graph below is using the `mirage-tcpip` stack.</p>
<p><a href="https://hannes.robur.coop/static/img/a.ns.mtcp.png"><img src="https://hannes.robur.coop/static/img/a.ns.mtcp.png" width="750"/></a></p>
<p>Now, after switching over to &micro;TCP, graphed below, there's much less network utilisation and the memory limit is only reached after 36 hours, which is a great result. Though, still it is not very satisfying that the unikernel leaks memory. On their left side, both graphs contain a few hours of `mirage-tcpip`, and shortly after 20:00 on Nov 23rd, &micro;TCP got deployed.</p>
<p><a href="https://hannes.robur.coop/static/img/a.ns.mtcp-utcp.png"><img src="https://hannes.robur.coop/static/img/a.ns.mtcp-utcp.png" width="750"/></a></p>
<p>Investigating the involved parts showed that a TCP connection that was never established has been registered at the MirageOS layer, but the pure core does not expose an event from the received RST that the connection has been cancelled. This means the MirageOS layer piles up all the connection attempts, and it doesn't inform the application that the connection couldn't be established. Once this was well understood, developing the <a href="https://github.com/robur-coop/utcp/commit/67fc49468e6b75b96a481ebe44dd11ce4bb76e6c">required code changes</a> was straightforward. The graph shows that the fix was deployed at 15:25. The memory usage is constant afterwards, but the network utilisation increased enormously.</p>
<p><a href="https://hannes.robur.coop/static/img/a.ns.utcp-ev.png"><img src="https://hannes.robur.coop/static/img/a.ns.utcp-ev.png" width="750"/></a></p>
<p>Now, the network utilisation is unwanted. This was hidden by the application waiting forever that the TCP connection getting established. Our bug fix uncovered another issue--a tight loop:</p>
<ul>
<li>The nameserver attempts to connect to the other nameserver (<code>request</code>);
</li>
<li>This results in a <code>TCP.create_connection</code> which errors after one roundtrip;
</li>
<li>This leads to a <code>close</code>, which attempts a <code>request</code> again.
</li>
</ul>
<p>This is unnecessary since the DNS server code has a timer to attempt to connect to the remote nameserver periodically (but takes a break between attempts). After understanding this behaviour, we worked on <a href="https://github.com/mirage/ocaml-dns/pull/347">the fix</a> and redeployed the nameserver again. On the left edge, the graph has the tight loop (so you have a comparison), and at 16:05, we deployed the fix, Since then it looks pretty smooth, both in memory usage and in network utilisation.</p>
<p><a href="https://hannes.robur.coop/static/img/a.ns.utcp-fixed.png"><img src="https://hannes.robur.coop/static/img/a.ns.utcp-fixed.png" width="750"/></a></p>
<p>To give you the entire picture, below is the graph where you can spot the `mirage-tcpip` stack (lots of network, restarting every 3 hours), &micro;TCP-without-informing-application (run for 3 * ~36 hours), DNS-server-high-network-utilisation (which only lasted for a brief period, thus it is more a point in the graph), and finally the unikernel with both fixes applied.</p>
<p><a href="https://hannes.robur.coop/static/img/a.ns.all.png"><img src="https://hannes.robur.coop/static/img/a.ns.all.png" width="750"/></a></p>
<h1>Conclusion</h1>
<p>What can we learn from that? Choosing convenient tooling is crucial for effective debugging. Also, fixing one issue may uncover other issues. And of course, the `mirage-tcpip` was running with the DNS server that had a tight reconnect loop. But, below the line: should such an application lead to memory leaks? I don't think so. My approach is that all core network libraries should work in a non-resource-leaky way with any kind of application on top of it. When one TCP connection returns an error (and thus is destroyed), the TCP stack should have no more resources used for that connection.</p>
<p>We'll take more time to investigate issues of &micro;TCP in production, plan to write further documentation and blog posts, and hopefully soon will be ready for an initial public release. In the meantime, you can follow our development repository.</p>
<p>We at <a href="https://robur.coop">Robur</a> are working as a collective since 2018 on public funding, commercial contracts, and donations. Our mission is to get sustainable, robust, and secure MirageOS unikernels developed and deployed. Running your own digital communication infrastructure should be easy, including trustworthy binaries and smooth upgrades. You can help us continue our work by <a href="https://aenderwerk.de/donate/">donating</a> (select Robur from the drop-down or put &quot;donation Robur&quot; in the purpose of the bank transfer).</p>
<p>If you have any questions, reach us best via eMail to team AT Robur DOT coop.</p>

16 changes: 16 additions & 0 deletions data/planet/practicalocaml/-hello--world-.md
@@ -0,0 +1,16 @@
---
title: '{ hello = `world; }'
description: And here we are! After writing my share of ReScript at Practical ReScript,
I figured I'd start a blog for OCaml that would help consolidate my experiences
with it into some Practical advice, that you can use to start new projects, to contribute
to existing ones, hell even get a
url: https://practicalocaml.com/hello-world/
date: 2023-08-23T00:30:16-00:00
preview_image:
featured:
authors:
- Practical OCaml
source:
---

<p>And here we are! After writing my share of ReScript at Practical ReScript, I figured I'd start a blog for OCaml that would help consolidate my experiences with it into some Practical advice, that you can use to start new projects, to contribute to existing ones, hell even get a job as with OCaml!</p><p>Since I don't write OCaml <em>every day</em>, sometimes I'll tag in other people to help out.</p><p>But today is just about saying hello &#128075; </p><p>Some of the subjects you can expect to read about here are:</p><ul><li>How to set up projects with shared libraries between melange and dune</li><li>How to structure your GraphQL server/client</li><li>How to build web apps with HTMX, React</li><li>Maybe even how to run OCaml on some runtimes like AWS Lambda</li></ul><p>And definitely on topics like:</p><ul><li>Modeling, and how to cheaply explore ideas </li><li>How to structure your projects so refactoring is easy</li><li>How to build command line applications</li><li>and more!</li></ul><p>So if you've got any ideas, feel free to post at me in X: <a href="https://practicalocaml.com/rss/x.com/leostera">@leostera</a></p>

0 comments on commit bdfb6c6

Please sign in to comment.