Skip to content

feat(krun): make virtio-vsock opt-in when TSI does not need it#47

Merged
appcypher merged 1 commit into
krunfrom
appcypher/vsock-optin
Apr 18, 2026
Merged

feat(krun): make virtio-vsock opt-in when TSI does not need it#47
appcypher merged 1 commit into
krunfrom
appcypher/vsock-optin

Conversation

@appcypher
Copy link
Copy Markdown
Member

@appcypher appcypher commented Apr 18, 2026

Summary

  • Stop unconditionally attaching a virtio-vsock device in Vm::build(). configure_vsock now only attaches it when the TSI transport needs it (no virtio-net in guest → HIJACK_INET, or single root virtio-fs on Linux → HIJACK_UNIX) or when the caller explicitly opts in.
  • Add MachineBuilder::vsock(bool) for callers that want a guest-visible vsock device even when TSI wouldn't otherwise require one, backed by a new VmResources::request_vsock flag.
  • Reclaim one IRQ + one MMIO slot per VM for the common case where vsock had no work to do (virtio-net present and multiple virtio-fs tags, so neither TSI hijack applies). For microsandbox's default configuration this drops the x86_64 baseline from 8 IRQs to 7, giving one extra slot for user mounts before the in-kernel-IOAPIC ceiling is hit.
  • Guest remains safe with the device absent: the krun-init shipped in libkrunfw is built without __TIMESYNC__ and therefore has no AF_VSOCK callers; Linux's built-in CONFIG_VIRTIO_VSOCKETS driver sits dormant when no matching device is enumerated. The VMM-side tsi_hijack* kernel-cmdline injection in builder.rs already lives inside if let Some(vsock) = vm_resources.vsock.get(), so the guest is never told to expect a transport that isn't there.

Test Plan

  • cargo build -p msb_krun — clean
  • cargo fmt --check
  • End-to-end: microsandbox patched against this branch, just build && just install, msb run alpine boots and exits cleanly
  • Verify a TSI-using configuration (no virtio-net) still attaches vsock implicitly — tsi_hijack should appear in the guest kernel cmdline
  • Verify explicit .vsock(true) forces the device on even when both TSI flags would be empty (virtio-net configured plus multiple fs tags)

Previously Vm::build() unconditionally attached a virtio-vsock device
via configure_vsock(). That consumed a permanent IRQ + MMIO slot even
for VMs that never use vsock — typically ones with virtio-net and more
than one virtio-fs tag, where neither HIJACK_INET nor HIJACK_UNIX
applies and the vsock has no actual work to do.

Skip the attach in that case and let callers opt in when they need
guest-visible vsock sockets for their own purposes.

- src/krun/src/api/vm.rs: configure_vsock() now early-returns when the
  caller hasn't opted in (VmResources::request_vsock) and no TSI flag
  is needed. builder.rs (VMM side) already only injects tsi_hijack /
  tsi_hijack_unix into the kernel cmdline inside
  `if let Some(vsock) = vm_resources.vsock.get()`, so the guest never
  gets told to expect a transport that isn't there.
- src/krun/src/api/builders.rs + builder.rs: add
  MachineBuilder::vsock(bool), threaded through to vmr.request_vsock.
- src/vmm/src/resources.rs: add request_vsock: bool (default false)
  alongside the existing split_irqchip flag.

Guest is safe with the device absent: the krun-init shipped in
libkrunfw is built without __TIMESYNC__, so it has no AF_VSOCK
callers; and Linux's CONFIG_VIRTIO_VSOCKETS driver sits dormant when
no virtio-vsock device is enumerated. Even with TIMESYNC enabled,
socket(AF_VSOCK, ...) returning ENODEV is handled gracefully in
clock_worker().

Net effect for microsandbox's typical VM: baseline virtio-mmio IRQ
use drops by one (from 8 to 7 on x86_64), freeing an extra slot for
user-defined mounts.
@appcypher appcypher merged commit aa60ba5 into krun Apr 18, 2026
8 checks passed
@appcypher appcypher deleted the appcypher/vsock-optin branch April 18, 2026 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant