Metadata-Version: 2.4
Name: bsuml
Version: 0.1.0
Summary: Run User Mode Linux guests on simulation time with Virtio devices
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/XiaoMi/bsworld/tree/main/components/bsuml
Project-URL: Issues, https://github.com/XiaoMi/bsworld/issues
Classifier: Operating System :: POSIX :: Linux
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: test
Requires-Dist: pytest>=8.4; extra == "test"
Requires-Dist: pytest-asyncio>=1.4; extra == "test"
Requires-Dist: ruff==0.14.10; extra == "test"
Requires-Dist: build>=1; extra == "test"
Dynamic: license-file

# BSUML

BSUML runs User Mode Linux (UML) guests on simulation time from a Python asyncio program. UML's `time-travel=ext` mode hands the guest kernel's clock to an external calendar, so the guest runs only when the simulation lets it. BSUML implements that calendar and a vhost-user backend for the guest's Virtio devices. Your code supplies the devices, which connect the guest to the rest of the simulation, and decides when each guest runs and how far it may run on its own. BSUML does not start the kernel, build a root filesystem, or schedule guests.

BSUML has two modules, and neither imports the other. Use the calendar alone for a guest without devices, or the backend with any devices you write.

| Module | Contents |
| --- | --- |
| `bsuml.timetravel` | `TimeTravelCalendar`, the `GuestDevices` protocol and `UML_TIMEOUT` |
| `bsuml.vhost` | `VhostUserBackend`, which speaks the vhost-user dialect of UML's `virtio_uml` driver: two queues per device, one shared memory region, and in-band notifications, so the calendar controls when each interrupt arrives |

## Install the Python package

Use Linux and Python 3.11 or newer. The guest kernel needs `CONFIG_UML_TIME_TRAVEL_SUPPORT`, and `CONFIG_VIRTIO_UML` for vhost-user devices.

```sh
python3 -m venv .venv
. .venv/bin/activate
python -m pip install bsuml
```

## Run the sample

[samples/README.md](https://github.com/XiaoMi/bsworld/blob/main/components/bsuml/samples/README.md) sets up a UML kernel and runs a guest with a Virtio console. The sample types `date`, `sleep 5` and `echo` into the guest's shell, and the 5 seconds of simulation time pass at once.

## Run a guest

A guest needs a calendar and, optionally, devices. The calendar listens on a Unix socket, and the kernel connects to it when it boots. The sample follows these steps with one Virtio console.

1. Create a `bsuml.timetravel.TimeTravelCalendar` on a socket path and call `listen()`.
2. Start a `bsuml.vhost.VhostUserBackend` for each Virtio device and call `listen()` on it.
3. Start the kernel with `time-travel=ext:<calendar socket>` and `virtio_uml.device=<backend socket>:<Virtio device ID>` for each device.
4. Call `calendar.connect(loop, now_ns=..., alive=..., devices=..., on_ready=...)`. `now_ns()` returns the current simulation time. It blocks until the guest first waits for simulation time.
5. When the guest is done, stop the kernel, then close the calendar. UML panics when the calendar socket closes while it runs.

## Write the devices

`devices` implements the `GuestDevices` protocol. It wraps your backends and whatever they connect to, such as a socket or a log. While the guest runs, the event loop is blocked, so the calendar services the devices itself: a guest can block on a device until its host side answers.

| Member | Use |
| --- | --- |
| `transport_read_fds` | Descriptors to service while the guest runs: only those the guest may block on, such as the backends' sockets. Input from outside, such as a peer's socket, belongs in `read_fds` only, so it reaches the guest when it next waits. |
| `read_fds`, `write_fds` | Descriptors the calendar watches in the event loop while the guest waits. |
| `service(readable, writable)` | Handle the ready descriptors without advancing simulation time. |
| `has_notifications` | A device completed a request and needs to interrupt the guest. |
| `flush_notifications()` | Send those interrupts. The calendar calls it when the guest can take them. |
| `notifications_pending` | The guest has not taken the interrupts yet. |

A `VhostUserBackend` provides the parts for one device: `read_fds`, `write_fds`, `service(readable, writable)`, which returns the queues the guest kicked, `pop(queue)` and `complete(queue, buffer, length)` for the guest's requests, and `has_notifications`, `notifications_pending` and `flush_notifications()`.

## Schedule the guest

The calendar never runs the guest on its own: your code decides when it runs, whether guests run one at a time, and how far each one may run without reporting its events.

| Member | Use |
| --- | --- |
| `request_ns` | The time the guest asked to run, or `None`. |
| `next_ns` | The earliest time the guest needs a turn, including device interrupts, or `None`. |
| `resume(free_until_ns, deadline)` | Run the guest at `request_ns` until it waits again. |
| `service_idle(deadline)` | Service the devices of a waiting guest, and deliver interrupts that are due. |
| `devices_changed()` | Report device input queued by your code, such as console input. It calls `on_ready`. |
| `close()` | Disconnect the guest. Afterwards it is never due. |

`on_ready` runs when the devices of a waiting guest have input. Give the guest a turn then, or at `next_ns`, by calling `service_idle()`. When simulation time reaches `request_ns`, call `resume()`. `free_until_ns` becomes the guest's FREE_UNTIL: it handles its own events before that time without reporting them, so pick the earliest time another participant may need to act. The calendar injects later interrupts no earlier than that time, so guest time never moves back. `deadline` is a `time.monotonic()` limit for the guest to yield. The event loop is blocked meanwhile, so simulation time stands still.

## API

| Name | Use |
| --- | --- |
| `bsuml.timetravel.TimeTravelCalendar(path, *, on_failure=None)` | The calendar side of the time-travel protocol for one guest. `on_failure` receives the error when the guest fails after boot. |
| `bsuml.timetravel.GuestDevices` | The protocol described above. |
| `bsuml.timetravel.UML_TIMEOUT` | Wall-clock seconds a guest may take to connect or yield, 60 by default. |
| `bsuml.vhost.VhostUserBackend(path, *, config=None)` | A vhost-user device backend with split virtqueues. `config` is the device's configuration space. |

## Develop from source

Install the package and its test tools from `components/bsuml` in the repository checkout.

```sh
python -m pip install -e '.[test]'
```

Run the tests and build the distributions. The tests play the guest's side of the sockets and need no kernel.

```sh
python -m pytest
python -m build
```

## License

Licensed under the [Apache License 2.0](https://github.com/XiaoMi/bsworld/blob/main/components/bsuml/LICENSE).
