Skip to content

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

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

feat(virtq): add packed virtio ring primitives#1382
andreiltd wants to merge 8 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
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>
@andreiltd andreiltd force-pushed the tandr/virtq-1-ring branch from 141fc6b to e27dacb Compare April 27, 2026 14:06
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
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 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.

const NEXT = 1 << 0;
/// This marks a buffer as device write-only (otherwise device read-only).
const WRITE = 1 << 1;
/// This means the buffer contains a list of buffer descriptors (unsupported here).
Copy link
Copy Markdown
Contributor

@ludfjig ludfjig May 5, 2026

Choose a reason for hiding this comment

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

nit: should we add runtimes check for this unsupported?

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.

We could, but I think it's perfectly safe if we just ignore this bit (treat as reserved) and document that current implementation does not follow direct tables. The reason I'm skeptical to add runtime checks is that buffer chains are processed in the loop so we will need the check inside the loop for no real benefits.

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

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.

Comment thread src/hyperlight_common/src/virtq/ring.rs Outdated
Comment thread src/hyperlight_common/src/virtq/event.rs
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
@andreiltd andreiltd force-pushed the tandr/virtq-1-ring branch from 7514b84 to c384e6f Compare May 6, 2026 12:30
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