Skip to content

virtio_p9: modernize to VirtioDevice trait#2840

Merged
jstarks merged 1 commit intomicrosoft:mainfrom
jstarks:p9_no_legacy
Feb 26, 2026
Merged

virtio_p9: modernize to VirtioDevice trait#2840
jstarks merged 1 commit intomicrosoft:mainfrom
jstarks:p9_no_legacy

Conversation

@jstarks
Copy link
Member

@jstarks jstarks commented Feb 25, 2026

Replace LegacyVirtioDevice impl with direct VirtioDevice impl:

  • Add driver, worker, exit_event fields for direct queue management
  • Implement enable()/disable() following virtiofs pattern
  • Constructor now takes VmTaskDriverSource parameter
  • Remove LegacyWrapper from resolver, construct device directly
  • Use Option instead of Vec since there is only one queue
  • Add deps: vmcore, task_control, event-listener, pal_async

The VirtioPlan9Worker and process_work() logic is unchanged. This is one step toward removing LegacyVirtioDevice entirely.

Replace LegacyVirtioDevice impl with direct VirtioDevice impl:
- Add driver, worker, exit_event fields for direct queue management
- Implement enable()/disable() following virtiofs pattern
- Constructor now takes VmTaskDriverSource parameter
- Remove LegacyWrapper from resolver, construct device directly
- Use Option<TaskControl> instead of Vec since there is only one queue
- Add deps: vmcore, task_control, event-listener, pal_async

The VirtioPlan9Worker and process_work() logic is unchanged.
This is one step toward removing LegacyVirtioDevice entirely.
Copilot AI review requested due to automatic review settings February 25, 2026 18:28
@jstarks jstarks requested review from a team as code owners February 25, 2026 18:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Modernizes the virtio_p9 (virtio-9p) device to implement the VirtioDevice trait directly (matching the newer direct-queue pattern used by devices like virtiofs), removing the legacy wrapper usage in the resolver.

Changes:

  • Replace LegacyVirtioDevice + LegacyWrapper usage with a direct VirtioDevice implementation in virtio_p9.
  • Add direct queue worker lifecycle management (enable()/disable()) using TaskControl, VmTaskDriver, and event-listener.
  • Update the resolver and crate dependencies to construct the device directly and support the new worker model.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
vm/devices/virtio/virtio_p9/src/resolver.rs Stop wrapping with LegacyWrapper; construct VirtioPlan9Device directly.
vm/devices/virtio/virtio_p9/src/lib.rs Implement VirtioDevice with explicit queue worker management and updated constructor signature.
vm/devices/virtio/virtio_p9/Cargo.toml Add new dependencies required for direct queue/task management.
Cargo.lock Lockfile updates reflecting new dependencies for virtio_p9.
Comments suppressed due to low confidence (1)

vm/devices/virtio/virtio_p9/src/lib.rs:52

  • size_of::<u16>() is used in the constructor but size_of isn’t imported or qualified, which will fail to compile. Import std::mem::size_of (or use std::mem::size_of::<u16>()).
        // The tag uses the same format as 9p protocol strings (2 byte length followed by string).
        let length = tag.len() + size_of::<u16>();

        // Round the length up to a multiple of 4 to make the read function simpler.
        let length = (length + 3) & !3;
        let mut tag_buffer = vec![0u8; length];

}

impl LegacyVirtioDevice for VirtioPlan9Device {
impl VirtioDevice for VirtioPlan9Device {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may want to also implement drop

impl Drop for VirtioPlan9Device {
    fn drop(&mut self) {
        self.disable();
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the drop thing isn't really correct, because disable really needs to be async to work for many devices (such as virtio-blk)--we need to wait for the device to finish accessing memory or whatever. I want to add another state transition to devices to disable them before they're torn down... for now I am just eliminating the drops.

assert!(index == 0);
Box::new(VirtioPlan9Worker {
fn enable(&mut self, resources: Resources) {
let queue_resources = resources
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use an iter when queues is a Vec? Just call .get(0). And also should probably validate that length is exactly 1, what if we get many queues?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really need to be that defensive--we specify the queue count in traits and can rely on the infra to pass the right number back to us.

.get(0) doesn't work because we need ownership. I guess .remove(0) would work. This seems fine though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we know the queue count ahead of time, could it be made a const-sized generic array? Not in this PR ofc, just food for thought.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some devices it will be dynamic.

@jstarks jstarks merged commit cb931e2 into microsoft:main Feb 26, 2026
60 checks passed
@jstarks jstarks deleted the p9_no_legacy branch February 26, 2026 21:13
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.

5 participants