DEV Community

Sandscale
Sandscale

Posted on Originally published at sandscale.dev

Firecracker vsock: how the host and a microVM talk without a network

If you run code in a microVM, sooner or later you need a channel between the host and the guest that is not the guest's network. You want to send a command in and get output back, even when the guest has no network at all. Firecracker's answer is virtio-vsock, and its implementation has a few details that are worth knowing before you build on it.

This post explains vsock as Firecracker documents it in docs/vsock.md and the design document, read in November 2026. It is about Firecracker itself, not about how any particular platform, Sandscale included, wires up its guests.

Why not just use the network?

You could give the guest a network interface and run an agent that listens on TCP. Three reasons people do not:

  • A sandbox with no network should really have no network. If your control channel is TCP, the guest has a network interface, and "no network" becomes a firewall rule you have to get right rather than an absent device.
  • Addressing. A TCP channel needs IP configuration in the guest, and every guest needs its own. vsock uses a context ID (CID) per VM instead.
  • The host side is a Unix socket. On the host, Firecracker exposes vsock as AF_UNIX sockets, which you can protect with file permissions and put inside the jailer's chroot like any other file.

The model: guest ports become host socket paths

Firecracker implements the virtio-vsock device model itself and, in its words, bypasses the vhost kernel code on the host. It mediates between AF_VSOCK sockets in the guest and AF_UNIX sockets on the host.

You configure the device before boot with a guest CID and a Unix socket path:

curl --unix-socket /tmp/firecracker.socket -i \
  -X PUT 'http://localhost/vsock' \
  -H 'Content-Type: application/json' \
  -d '{ "guest_cid": 3, "uds_path": "./v.sock" }'
Enter fullscreen mode Exit fullscreen mode

From there, the direction of the connection decides how it works.

Host to guest

  1. In the guest, a program listens on an AF_VSOCK port, say 52.
  2. On the host, you connect to ./v.sock, the uds_path.
  3. You send a line of text: CONNECT 52 followed by a newline.
  4. Firecracker forwards the connection to the guest listener and replies on your socket with OK <port>, where the port is the host-side vsock port assigned to your end.
  5. From then on, the Unix socket connection is a byte stream to the guest program.

If nothing in the guest is listening on that port, Firecracker closes your connection. Your host code should treat a closed socket after CONNECT as "the guest agent is not ready yet", not as a hard failure.

Guest to host

  1. On the host, a program listens on a Unix socket at ./v.sock_52: the uds_path, an underscore, and the port number.
  2. In the guest, a program connects to CID 2 (the host) on port 52.
  3. Firecracker forwards the connection to ./v.sock_52.

If that socket does not exist, nothing is listening, or its backlog is full, the guest gets a reset (VIRTIO_VSOCK_OP_RST).

Details that bite

A reserved port range. Ports from 2^30 to 2^31 - 1 (1073741824 to 2147483647) are reserved for the host side of host-initiated connections. A guest connection to a port in that range is always reset. Do not put host listeners at v.sock_<port> in that range.

A connection limit. Firecracker caps the combined number of vsock connections and host sockets still waiting to send their CONNECT line at 1023. At the limit, a new connection closes the oldest host socket that is still waiting for CONNECT; if every slot is an established connection, the new one is rejected. In practice: send CONNECT right after connecting, close connections you are done with, and do not hold hundreds of idle streams per VM.

Kernel configuration. The host kernel needs CONFIG_VHOST_VSOCK and the guest kernel needs CONFIG_VIRTIO_VSOCKETS. In the guest, /dev/vsock existing is the quick check.

Snapshots. The docs say vsock snapshot support is limited and point to a vsock device reset note in the snapshot documentation: treat connections as gone after a restore, and have both sides reconnect. Without the jailer, two VMs restored from the same snapshot would both try to use the same host socket path; the snapshot load API's vsock_override lets you give the restored VM a different uds_path.

What runs on top

vsock gives you a byte stream. You still need a protocol on it. The usual shape for a code-execution guest is a small agent in the guest that:

  • listens on one well-known vsock port,
  • accepts a request (a command, environment, working directory, time limit),
  • runs it, streaming stdout and stderr back as framed messages,
  • sends an exit status and closes.

A few design choices pay off:

  • Frame everything. Prefix each message with a length and a type (stdout, stderr, exit). Raw interleaved output is impossible to split back into streams.
  • Bound everything. The guest is untrusted by definition. The host side should cap message sizes, total output and time, and treat a malformed frame as the end of the conversation.
  • One request per connection. It makes cancellation simple: close the socket.

The Firecracker design document states the stance this follows from: all vCPU threads are considered to be running malicious code as soon as they start. Anything that reads from the guest, including the host end of a vsock stream, is parsing hostile input.

Where Sandscale fits

You do not handle any of this when you use Sandscale. Each sandbox is its own Firecracker microVM, and you talk to it through the API, SDK, CLI or MCP server: sandboxes.run returns the exit code, standard output and standard error, with live output if you pass onStdout or onStderr, and writeFile and readFile move files. A sandbox's network is none by default and otherwise follows your plan and your organization's policy, and running commands and moving files work the same with the network off. The quickstart shows the whole flow.

If you are building your own Firecracker platform, vsock is the piece to get right early, and the Firecracker doc is short enough to read in full.

Originally published on the Sandscale blog.

Top comments (0)