Skip to content

feat(virtq): add packed virtio ring primitives#1382

Open
andreiltd wants to merge 9 commits intohyperlight-dev:mainfrom
andreiltd:tandr/virtq-1-ring
Open

feat(virtq): add packed virtio ring primitives#1382
andreiltd wants to merge 9 commits intohyperlight-dev:mainfrom
andreiltd:tandr/virtq-1-ring

Conversation

@andreiltd
Copy link
Copy Markdown
Member

@andreiltd andreiltd commented Apr 16, 2026

Add low-level packed virtqueue ring implementation in hyperlight_common::virtq, based on the virtio packed ring format.

This is split from: #1368 and does not include any actual plumbing for guest/host communication.

Useful materials:

@andreiltd andreiltd force-pushed the tandr/virtq-1-ring branch from 3db3820 to bd2fd96 Compare April 16, 2026 10:20
@andreiltd andreiltd added the kind/enhancement For PRs adding features, improving functionality, docs, tests, etc. label Apr 16, 2026
@andreiltd andreiltd force-pushed the tandr/virtq-1-ring branch 2 times, most recently from b4c8142 to 16de50c Compare April 16, 2026 10:30
Copy link
Copy Markdown
Contributor

@dblnz dblnz left a comment

Choose a reason for hiding this comment

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

Great stuff, @andreiltd !
I left a few comments, most are small remarks, others are things I might have missed.
This is part 1 of my review, I still have some things left to look at that I plan on doing tomorrow.

Comment thread src/hyperlight_common/src/virtq/access.rs Outdated
Comment thread src/hyperlight_common/src/virtq/access.rs Outdated
Comment thread src/hyperlight_common/src/virtq/desc.rs Outdated
Comment thread src/hyperlight_common/src/virtq/desc.rs Outdated
Comment thread src/hyperlight_common/src/virtq/desc.rs Outdated
Comment thread src/hyperlight_common/src/virtq/event.rs Outdated
Comment thread src/hyperlight_common/src/virtq/ring.rs
Comment thread src/hyperlight_common/src/virtq/ring.rs
Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
@andreiltd andreiltd force-pushed the tandr/virtq-1-ring branch 4 times, most recently from 857f4bf to 2c6adcb Compare April 22, 2026 13:09
Comment thread src/hyperlight_common/src/virtq/access.rs
Comment thread src/hyperlight_common/src/virtq/access.rs
Comment thread src/hyperlight_common/src/virtq/ring.rs
Comment thread src/hyperlight_common/src/virtq/ring.rs
@andreiltd andreiltd force-pushed the tandr/virtq-1-ring branch 3 times, most recently from 79ee809 to 141fc6b Compare April 27, 2026 12:32
Comment thread src/hyperlight_common/src/virtq/ring.rs
@andreiltd andreiltd force-pushed the tandr/virtq-1-ring branch from 141fc6b to e27dacb Compare April 27, 2026 14:06
ludfjig
ludfjig previously approved these changes May 5, 2026
Copy link
Copy Markdown
Contributor

@ludfjig ludfjig left a comment

Choose a reason for hiding this comment

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

This PR is very clean, big fan!

I know we don't really have policy on asserts vs errors etc in hyperlight (I personally like asserts...), but ring.rs has 3 debug_assert! that could be useful in release mode as well, and they're already in methods that return Results. Do you think it makes sense to convert them to errros instead?

Comment thread src/hyperlight_common/src/virtq/ring.rs
Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
Comment thread src/hyperlight_common/src/virtq/ring.rs
Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
Comment thread src/hyperlight_common/src/virtq/mod.rs
Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
Comment on lines +502 to +503
desc.write_release(&self.mem, addr)
.map_err(|_| RingError::MemError)?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: lot's of map_err lose their context when going to RingError::MemError, maybe can parametrize RingError<E> on generic E which is some M::Error? Not sure, feel free to ignore

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tried this initially yes, and make it generic creates much more API churn especially for the layers on top of virtq. But let me think a bit more how we could preserve context without making ring error generic.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've added more context to the MemError variant:

    #[error("Backend memory error while {op} at address 0x{addr:x}, len {len}")]
    MemError {
        /// Memory operation that failed.
        op: MemOp,
        /// Address passed to the memory backend.
        addr: u64,
        /// Number of bytes requested for the operation.
        len: usize,
    },

I guess the alternative would be to use type erasure like Box<dyn Error> but that will require extra bounds on M::Error: Error + Send + Sync + 'static and will allocate on error path?

Comment thread src/hyperlight_common/src/virtq/desc.rs
Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
Copy link
Copy Markdown
Member

@simongdavies simongdavies left a comment

Choose a reason for hiding this comment

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

Great stuff a few minor comments but looks good to me

Comment thread src/hyperlight_common/src/virtq/access.rs Outdated
Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
avail == wrap && used == wrap
}

/// Is this descriptor writable by the device?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The comments here use the terms "guest" "host" and "device" and "driver" I read the HIP again at https://github.com/andreiltd/hyperlight/blob/tandr/rng-rfc/proposals/0001-rng-buf/README.md#design-details

but still wasn't sure how to interpret these terms here? The HIP suggests that the driver is always the guest and the device is always the host but then goes on to say that for bi-directional communication these roles are reversed , I think it might help us in the future to define explicitly what we are referring to in the code comments.

Copy link
Copy Markdown
Member Author

@andreiltd andreiltd May 6, 2026

Choose a reason for hiding this comment

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

Yes, HIP needs to be updated. Initial version of HIP assumed the roles are swapped depending on direction -- but that idea has been later discarded following allocation discussion in the heap. The most recent agreement is: driver - always a guest, device - always host. I also unified naming here so that it refers to virtio semantics so driver/device.

Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
Comment thread src/hyperlight_common/src/virtq/event.rs
@andreiltd andreiltd requested a review from jsturtevant as a code owner May 6, 2026 12:28
@andreiltd andreiltd force-pushed the tandr/virtq-1-ring branch 2 times, most recently from c384e6f to 6541fea Compare May 7, 2026 08:41
andreiltd added 8 commits May 7, 2026 10:54
Add low-level packed virtqueue ring implementation in
hyperlight_common::virtq, based on the virtio packed ring format.

Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
@andreiltd andreiltd force-pushed the tandr/virtq-1-ring branch from 6541fea to 519472d Compare May 7, 2026 08:55
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement For PRs adding features, improving functionality, docs, tests, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants