virtio_p9: modernize to VirtioDevice trait#2840
Conversation
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.
There was a problem hiding this comment.
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+LegacyWrapperusage with a directVirtioDeviceimplementation invirtio_p9. - Add direct queue worker lifecycle management (
enable()/disable()) usingTaskControl,VmTaskDriver, andevent-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 butsize_ofisn’t imported or qualified, which will fail to compile. Importstd::mem::size_of(or usestd::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 { |
There was a problem hiding this comment.
may want to also implement drop
impl Drop for VirtioPlan9Device {
fn drop(&mut self) {
self.disable();
}
}
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
For some devices it will be dynamic.
Replace LegacyVirtioDevice impl with direct VirtioDevice impl:
The VirtioPlan9Worker and process_work() logic is unchanged. This is one step toward removing LegacyVirtioDevice entirely.