Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a838dcc
openvmm_entry: add VM-mutation gate to the ttrpc VmService
Oct 14, 2026
940b7e8
Guide: document the VM operation gate in the gRPC reference
Oct 14, 2026
cf6062f
openvmm_entry: address gate review feedback
Oct 15, 2026
0ced5c4
openvmm_entry: make teardown and quit terminal over in-flight changes
Oct 16, 2026
83bcdc4
Merge branch 'main' into marma/vm-operation-gate
marma-dev Oct 28, 2026
53d2ff7
vmm_tests: check Consomme port changes against the running-state rule
Nov 6, 2026
98d06cc
Merge branch 'main' into marma/vm-operation-gate
Nov 6, 2026
01853af
openvmm_entry: test cancelling an active gated operation
Nov 6, 2026
5be0e3f
openvmm_entry: clarify what terminal cancellation interrupts
Nov 6, 2026
87fc678
Merge branch 'main' into marma/vm-operation-gate
marma-dev Nov 9, 2026
96b9854
Merge branch 'main' into marma/vm-operation-gate
marma-dev Nov 10, 2026
ea82f6d
openvmm_entry: simplify the VM operation gate
Nov 10, 2026
c0f2622
Merge branch 'main' into marma/vm-operation-gate
Nov 11, 2026
094ab51
openvmm_entry: gate SaveVM and RestoreVM
Nov 11, 2026
55bbe59
openvmm_entry: gate only SaveVM, after its preconditions
Nov 11, 2026
c6c2a67
openvmm_entry: release a cancelled save's gate when the worker stops
Nov 11, 2026
e7da738
openvmm_entry: check Consomme state before options; bound the shared …
Nov 12, 2026
042862f
openvmm_entry: seal mutations after SaveVM
Nov 20, 2026
b4c567c
Merge remote-tracking branch 'upstream/main' into pr-4659-review-fixes
Nov 20, 2026
d98dc17
vmm_tests: use Quit after SaveVM
Nov 22, 2026
078df41
vmm_tests: tolerate Quit reply loss in lifecycle test
Nov 22, 2026
0c8cbd2
Merge remote-tracking branch 'upstream/main' into pr-4659-review-fixes
Nov 22, 2026
10346bf
openvmm_entry: complete terminal SaveVM waiters
Nov 23, 2026
aec0ff4
openvmm_entry: complete late terminal WaitVM
Nov 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Guide/src/reference/openvmm/management/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,46 @@ Unlike `AddPcieDevice`, VPCI does not require a root complex or a predeclared
hotplug-capable PCIe port. `AddPcieDevice` remains available when standard PCIe
hotplug semantics or a non-VPCI host backend is required.

## Concurrent operations

VM-changing operations never overlap in a conflicting way. Device and resource
changes (`ModifyResource`, `AddPcieDevice`, `RemovePcieDevice`,
`AddVpciDevice`, `RemoveVpciDevice`) run in the background and may overlap one
another. `PauseVM` and `ResumeVM` are exclusive: if a device or resource change
is still in flight, they are rejected immediately with `ABORTED` and a message
naming both operations (for example, `VM busy: device or resource modification
in progress, cannot start pause`). A rejected request has no side effects, and
the server never retries it; the caller decides whether to retry.

`TeardownVM` and `Quit` are terminal and are never rejected by in-flight
device or resource changes. They cancel those changes first, and the cancelled
calls return `CANCELLED`. Cancellation stops the server waiting for the change;
a change already delivered to the VM is completed or discarded when the VM
stops.

| Operation | Class | In-flight device or resource changes |
|---|---|---|
| `PauseVM`, `ResumeVM` | Exclusive | Rejects the request with `ABORTED` |
| `TeardownVM`, `Quit` | Terminal | Cancelled; they return `CANCELLED` |
| `ModifyResource`, `AddPcieDevice`, `RemovePcieDevice`, `AddVpciDevice`, `RemoveVpciDevice` | Shared | Run alongside |

`PauseVM`, `ResumeVM`, and `TeardownVM` run to completion before the server
reads its next request. Requests that arrive while one of them is running
therefore wait and are processed after it finishes, rather than being
rejected.

`CreateVM` and the read-only RPCs (`WaitVM`, `CapabilitiesVM`, `PropertiesVM`)
are not gated.

Otherwise, the gate is held until the operation finishes, whether it succeeds
or fails. A client that cancels or disconnects does not release it early,
because the operation itself keeps running to completion.

Consomme port changes (`ModifyResource` updating or removing a Consomme NIC's
ports) require a running VM and fail with `FAILED_PRECONDITION` otherwise,
because the ports are serviced by the NIC's queue processing, which is stopped
while the VM is paused.

## VFIO and accelerated SMMU

On Linux, declare named host iommufd contexts in `VMConfig.iommufds`, then
Expand Down
133 changes: 123 additions & 10 deletions openvmm/openvmm_entry/src/ttrpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
// The fd-passing protocol relies on `SCM_RIGHTS` and so exists only on unix.
#[cfg(unix)]
mod fd_passing;
mod operation_gate;

#[cfg(unix)]
use fd_passing::FdRegistry;
use operation_gate::VmOperation;
use operation_gate::VmOperationGate;

/// On non-unix platforms the fd-passing protocol does not exist. The registry
/// is still threaded through the shared NIC configuration code, so provide an
Expand Down Expand Up @@ -367,6 +370,7 @@ impl Worker for TtrpcWorker {

fn run(self, recv: mesh::Receiver<WorkerRpc<Self::State>>) -> anyhow::Result<()> {
DefaultPool::run_with(async |driver| {
let (topology_cancel_ctx, topology_cancel) = mesh::CancelContext::new().with_cancel();
let mut service = VmService {
driver,
vm: None,
Expand All @@ -376,6 +380,10 @@ impl Worker for TtrpcWorker {
wait_vm_response: None,
lifecycle: VmLifecycle::Uninitialized,
rpc_tasks: Vec::new(),
operation_gate: VmOperationGate::default(),
gated_tasks: Vec::new(),
gated_cancel_ctx: topology_cancel_ctx,
gated_cancel: topology_cancel,
transport: self.transport,
registry: FdRegistry::default(),
};
Expand Down Expand Up @@ -504,6 +512,9 @@ impl VmService {
}
};

// Cancel in-flight device and resource changes; see `cancel_gated_tasks`.
self.cancel_gated_tasks().await;

// If the controller is still alive (non-Quit exit), shut it down.
if !quit {
if let Some(controller) = self.vm_controller.take() {
Expand Down Expand Up @@ -546,6 +557,68 @@ impl VmService {
Err(err) => response.send(Err(grpc_error(err))),
}
}

/// Like [`Self::start_rpc`], but admits the operation through the
/// VM-mutation gate first. `build` runs only after admission, so a rejected
/// operation has no side effects, and the gate is held until the operation
/// completes or is cancelled by a terminal operation (see
/// [`Self::cancel_gated_tasks`]).
fn start_gated_rpc<F, R>(
&mut self,
operation: VmOperation,
response: mesh::OneshotSender<Result<R, Status>>,
build: impl FnOnce(&Self) -> anyhow::Result<F>,
) where
F: 'static + Future<Output = anyhow::Result<R>> + Send,
R: 'static + MeshPayload + Send,
{
let fut = match self
.operation_gate
.try_acquire(operation)
.and_then(|guard| build(self).map(|fut| (guard, fut)))
{
Ok(v) => v,
Err(err) => {
response.send(Err(grpc_error(err)));
return;
}
};
let (guard, fut) = fut;
let mut cancel = self.gated_cancel_ctx.clone();
let task = self.driver.spawn("ttrpc-gated-rpc", async move {
let r = match cancel.until_cancelled(fut).await {
Ok(r) => r,
Err(_) => Err(anyhow::Error::new(Code::Cancelled))
.context("cancelled because the VM is being torn down"),
};
drop(guard);
response.send(map_grpc(r));
});
self.gated_tasks.retain(|task| !task.is_finished());
self.gated_tasks.push(task);
}

/// Cancels every in-flight gated background operation and waits for it to
/// finish, so it releases the gate and any device RPC channel it holds.
///
/// Used by the terminal operations (teardown and quit), which must always
/// make progress: an in-flight device or resource change has no meaning
/// once the VM is destroyed, and one can otherwise wait forever (for
/// example, a Consomme port change whose NIC is not processing its queue).
/// The cancelled callers receive `CANCELLED`. Only the service-side wait
/// is cancelled; a request already delivered to the VM is completed or
/// discarded when the VM worker stops.
///
/// Cancellation drops the operation's future, so gated futures must be
/// safe to drop at any await point. Today they await only RPC replies and
/// blocking file I/O. They must not issue io_uring I/O on the service
/// driver, whose in-flight futures abort the process if dropped.
async fn cancel_gated_tasks(&mut self) {
let (ctx, cancel) = mesh::CancelContext::new().with_cancel();
self.gated_cancel_ctx = ctx;
std::mem::replace(&mut self.gated_cancel, cancel).cancel();
futures::future::join_all(self.gated_tasks.drain(..)).await;
Comment thread
marma-dev marked this conversation as resolved.
Resolved
}
}

struct Vm {
Expand Down Expand Up @@ -679,6 +752,15 @@ struct VmService {
wait_vm_response: Option<(mesh::CancelContext, mesh::OneshotSender<Result<(), Status>>)>,
lifecycle: VmLifecycle,
rpc_tasks: Vec<Task<()>>,
/// Rejects an operation that conflicts with one already in flight.
operation_gate: VmOperationGate,
/// In-flight gated operations that run as background tasks (device and
/// resource changes).
gated_tasks: Vec<Task<()>>,
/// Observed by every task in `gated_tasks`; cancelled by the terminal
/// operations (teardown and quit) through `gated_cancel`.
gated_cancel_ctx: mesh::CancelContext,
gated_cancel: mesh::Cancel,
transport: ResolvedTransport,
/// Registry of file descriptors passed in over the fd-passing protocol,
/// resolvable by name (e.g. for tap NIC backends).
Expand Down Expand Up @@ -724,6 +806,12 @@ impl VmService {
response.send(map_grpc(self.teardown_vm().await))
}
vmservice::Vm::Quit((), response) => {
// Quit is terminal. Cancel in-flight device and resource
// changes first: each can hold a clone of a device RPC channel
// (for example the SCSI controller's), and a live channel keeps
// the worker from stopping, so the controller wait below could
// otherwise hang.
self.cancel_gated_tasks().await;
// Shut down the controller (which stops and joins the worker).
// Drop the VM's device RPC channels first; see `teardown_vm`.
self.vm.take();
Expand Down Expand Up @@ -764,24 +852,29 @@ impl VmService {
}
}
vmservice::Vm::ModifyResource(request, response) => {
let r = self.modify_resource(request);
self.start_rpc(response, r);
self.start_gated_rpc(VmOperation::Topology, response, |this| {
this.modify_resource(request)
});
Comment thread
Copilot marked this conversation as resolved.
Resolved
}
vmservice::Vm::AddPcieDevice(request, response) => {
let r = self.add_pcie_device(request);
self.start_rpc(response, r);
self.start_gated_rpc(VmOperation::Topology, response, |this| {
this.add_pcie_device(request)
});
}
vmservice::Vm::RemovePcieDevice(request, response) => {
let r = self.remove_pcie_device(request);
self.start_rpc(response, r);
self.start_gated_rpc(VmOperation::Topology, response, |this| {
this.remove_pcie_device(request)
});
}
vmservice::Vm::AddVpciDevice(request, response) => {
let r = self.add_vpci_device(request);
self.start_rpc(response, r);
self.start_gated_rpc(VmOperation::Topology, response, |this| {
this.add_vpci_device(request)
});
}
vmservice::Vm::RemoveVpciDevice(request, response) => {
let r = self.remove_vpci_device(request);
self.start_rpc(response, r);
self.start_gated_rpc(VmOperation::Topology, response, |this| {
this.remove_vpci_device(request)
});
}
}
HandleAction::None
Expand Down Expand Up @@ -1366,7 +1459,23 @@ impl VmService {
Ok(())
}

/// Consomme port requests are serviced from the NIC's queue processing,
/// which is stopped while the VM is not running, so a request made then
/// would never complete. Reject it up front.
fn require_running_for_consomme_ports(&self) -> anyhow::Result<()> {
if !matches!(self.lifecycle, VmLifecycle::Running) {
return Err(anyhow::Error::new(Code::FailedPrecondition))
.context("consomme port changes require a running VM");
Comment thread
marma-dev marked this conversation as resolved.
Resolved
}
Ok(())
}

async fn teardown_vm(&mut self) -> anyhow::Result<()> {
// Teardown is terminal: it cancels in-flight device and resource
// changes rather than being rejected by them. Pause and resume run
// inline, so none is in flight here and the gate is now free.
self.cancel_gated_tasks().await;
let _gate = self.operation_gate.try_acquire(VmOperation::Teardown)?;
Comment thread
Copilot marked this conversation as resolved.
Resolved
let controller = self.vm_controller.take().context("vm not created")?;
// Drop the VM's device RPC channels before waiting on the controller.
// A live `ScsiControllerRequest` sender keeps the detached storvsp task
Expand Down Expand Up @@ -1431,6 +1540,7 @@ impl VmService {
}

async fn pause_vm(&mut self) -> anyhow::Result<()> {
let _gate = self.operation_gate.try_acquire(VmOperation::Pause)?;
let vm = self.vm.clone().context("VM not created yet")?;
vm.worker_rpc
.call(VmRpc::Pause, ())
Expand All @@ -1444,6 +1554,7 @@ impl VmService {
}

async fn resume_vm(&mut self) -> anyhow::Result<()> {
let _gate = self.operation_gate.try_acquire(VmOperation::Resume)?;
let vm = self.vm.clone().context("VM not created yet")?;
vm.worker_rpc
.call(VmRpc::Resume, ())
Expand Down Expand Up @@ -1631,6 +1742,7 @@ impl VmService {
vmservice::nic_config::Backend::Consomme(c) => c,
_ => anyhow::bail!("port update only supported for consomme backend"),
};
self.require_running_for_consomme_ports()?;
let consomme_rpc = vm
.consomme_rpc
.as_ref()
Expand All @@ -1652,6 +1764,7 @@ impl VmService {
vmservice::nic_config::Backend::Consomme(c) => c,
_ => anyhow::bail!("port remove only supported for consomme backend"),
};
self.require_running_for_consomme_ports()?;
let consomme_rpc = vm
.consomme_rpc
.as_ref()
Expand Down
Loading
Loading