Xen paravirtual platform and drivers in OCaml
README.md

nox-xen #

Warning. This is a work-in-progress experimental fork of the MirageOS Xen stack: mirage-net-xen, mirage-block-xen, shared-memory-ring and mirage-xen. Do not use it in production; use those libraries instead.

Xen paravirtual drivers for the mirage-eio Eio backend: the shared-memory rings, grant tables, event channels, XenStore and the network and block frontends.

A guest under Xen does not see emulated hardware. It talks to a backend domain through pages it shares with a grant, rings laid out in those pages, and event channels that act as interrupts, and the two sides find each other through XenStore, a key/value store run by the toolstack. A unikernel needs all of this before it can send a packet.

A paravirtual driver is one written for this shared-ring interface rather than for emulated hardware, split into a frontend in the guest and a backend in the domain that owns the real device. The toolstack is the management software in dom0 (xl, libxl) that creates domains and runs XenStore. A grant is an entry the guest writes in its grant table saying that domain D may map (or copy) its page P, read-only or read-write; the guest hands D the entry's index, the grant reference, through XenStore, and D asks Xen to map that page. An event channel is a pair of ports between two domains, managed by Xen: notifying one end sets a pending bit and raises a virtual interrupt in the other domain. Each side uses it to say that there is new work on the ring. Most of this package is imported from the MirageOS Xen libraries (ISC, copyright the MirageOS authors) and adapted to the Eio backend:

  • Xen.Ring is the split-ring request and response protocol, from mirage/shared-memory-ring. Xen.Ring.Rpc works over Cstruct without I/O, so the frontend and backend sides run unchanged in a host loopback and against a real Xen backend domain.
  • Xen.Net and Xen.Netfront are the network frontend, from mirage/mirage-net-xen.
  • Xen.Block and Xen.Blkfront are the block frontend, from mirage/mirage-block-xen.
  • Xen.Grant, Xen.Evtchn, Xen.Clock and Xen.Platform are the grant tables, event channels, wall clock and the injectable platform signature, from mirage/mirage-xen.

Xen.Xenstore and the Xen.Xenbus* modules are written from the Xen protocol headers instead of imported. Each one decodes the wire framing with the wire codec and runs the protocol as a pure state machine, and a thin adapter does the ring or socket I/O. The clients therefore run without a live ring in tests, and the same code serves a host loopback and a real Xen peer.

Xen.Xenstore is a client for the XenStore protocol that a frontend and a backend use to exchange the ring's grant reference, the event-channel port and the device state. It implements the xs_wire.h message framing and a pure request and reply state machine. The original OCaml XenStore is mirage/ocaml-xenstore by Thomas Gazagnaire and Vincent Hanquez (Citrix; see the OXenstored paper at ICFP 2009), which is LGPL-2.1.

The device connection handshake follows the XenbusState values of io/xenbus.h and spans three modules. Xen.Xenbus defines the nine states a device half publishes to its own XenStore node and watches on its peer's. Xen.Xenbus_frontend describes the frontend half as one typed Fsm table. Every phase change of Netfront and Blkfront comes from Fsm.step over that table, and the XenStore writes are its declared effects, performed by the four callbacks passed to Xenbus_frontend.connect. The same logic therefore serves a normal domU, which meets its backend through XenStore, and a static dom0less guest.

Xen.Xenbus_backend is the backend half, in both orders that real backends use: xen-netback maps its rings only once it has seen the frontend reach Connected, while xen-blkback maps them at Initialised. A frontend cannot check on its own that it works with both orders, so test/test_xenbus_frontend.ml composes the two halves with Fsm_check.Product, modelling the two XenStore nodes as one-slot registers, cells that hold only the latest value written. The product has four components (the frontend, one backend order, the frontend's node and the backend's node), and the test searches it for a jam, a reachable state in which no component can step; it passes when neither backend order has one, and it then asks for the state where the frontend is live and the backend serving, so that the ring comes up against each backend.

Installation #

$ opam install nox-xen

If opam cannot find the package, it has not reached the public opam-repository yet. Add the overlay repository first:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-xen

License #

ISC, like the upstream MirageOS libraries. See LICENSE.md. The Xen.Xenstore and Xen.Xenbus modules written for this package are ISC, copyright Thomas Gazagnaire.