Skip to content

Commit 70de7f8

Browse files
committed
refactor(virtq) adjust virtio terminology
Use queue_size instead of queue_depth Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
1 parent 854dc57 commit 70de7f8

11 files changed

Lines changed: 132 additions & 133 deletions

File tree

src/hyperlight_common/src/layout.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ struct ScratchTopMetadata {
2929
/// Number of pages reserved for the H2G pool.
3030
h2g_pool_pages: u64,
3131
/// Host-published H2G descriptor count.
32-
h2g_queue_depth: u64,
32+
h2g_queue_size: u64,
3333
/// Host-published capacity of each G2H upper-tier buffer.
3434
g2h_buffer_size: u64,
3535
/// Number of pages reserved for the G2H pool.
3636
g2h_pool_pages: u64,
3737
/// Host-published G2H descriptor count.
38-
g2h_queue_depth: u64,
38+
g2h_queue_size: u64,
3939
/// Host-published GPA of the fixed transport arena.
4040
transport_arena_gpa: u64,
4141
/// Seed request for libc's pseudorandom number generator.
@@ -54,14 +54,14 @@ const fn scratch_top_offset(field_offset: usize) -> u64 {
5454
(size_of::<ScratchTopMetadata>() - field_offset) as u64
5555
}
5656

57-
pub const SCRATCH_TOP_G2H_QUEUE_DEPTH_OFFSET: u64 =
58-
scratch_top_offset(offset_of!(ScratchTopMetadata, g2h_queue_depth));
57+
pub const SCRATCH_TOP_G2H_QUEUE_SIZE_OFFSET: u64 =
58+
scratch_top_offset(offset_of!(ScratchTopMetadata, g2h_queue_size));
5959
pub const SCRATCH_TOP_G2H_POOL_PAGES_OFFSET: u64 =
6060
scratch_top_offset(offset_of!(ScratchTopMetadata, g2h_pool_pages));
6161
pub const SCRATCH_TOP_G2H_BUFFER_SIZE_OFFSET: u64 =
6262
scratch_top_offset(offset_of!(ScratchTopMetadata, g2h_buffer_size));
63-
pub const SCRATCH_TOP_H2G_QUEUE_DEPTH_OFFSET: u64 =
64-
scratch_top_offset(offset_of!(ScratchTopMetadata, h2g_queue_depth));
63+
pub const SCRATCH_TOP_H2G_QUEUE_SIZE_OFFSET: u64 =
64+
scratch_top_offset(offset_of!(ScratchTopMetadata, h2g_queue_size));
6565
pub const SCRATCH_TOP_H2G_POOL_PAGES_OFFSET: u64 =
6666
scratch_top_offset(offset_of!(ScratchTopMetadata, h2g_pool_pages));
6767
pub const SCRATCH_TOP_H2G_BUFFER_SIZE_OFFSET: u64 =
@@ -88,10 +88,10 @@ const _: () = {
8888
assert!(SCRATCH_TOP_SNAPSHOT_GENERATION_OFFSET == 0x20);
8989
assert!(SCRATCH_TOP_LIBC_RNG_SEED_OFFSET == 0x28);
9090
assert!(SCRATCH_TOP_TRANSPORT_ARENA_GPA_OFFSET == 0x30);
91-
assert!(SCRATCH_TOP_G2H_QUEUE_DEPTH_OFFSET == 0x38);
91+
assert!(SCRATCH_TOP_G2H_QUEUE_SIZE_OFFSET == 0x38);
9292
assert!(SCRATCH_TOP_G2H_POOL_PAGES_OFFSET == 0x40);
9393
assert!(SCRATCH_TOP_G2H_BUFFER_SIZE_OFFSET == 0x48);
94-
assert!(SCRATCH_TOP_H2G_QUEUE_DEPTH_OFFSET == 0x50);
94+
assert!(SCRATCH_TOP_H2G_QUEUE_SIZE_OFFSET == 0x50);
9595
assert!(SCRATCH_TOP_H2G_POOL_PAGES_OFFSET == 0x58);
9696
assert!(SCRATCH_TOP_H2G_BUFFER_SIZE_OFFSET == 0x60);
9797
assert!(SCRATCH_TOP_EXN_STACK_OFFSET == 0x70);
@@ -116,14 +116,14 @@ pub fn scratch_base_gva(size: usize) -> u64 {
116116
pub fn min_scratch_size(
117117
input_data_size: usize,
118118
output_data_size: usize,
119-
g2h_queue_depth: usize,
120-
h2g_queue_depth: usize,
119+
g2h_queue_size: usize,
120+
h2g_queue_size: usize,
121121
g2h_pool_pages: usize,
122122
h2g_pool_pages: usize,
123123
) -> usize {
124124
let size = arch::min_scratch_size(input_data_size, output_data_size).and_then(|fixed| {
125-
let g2h = QueueDims::new(g2h_queue_depth, g2h_pool_pages)?;
126-
let h2g = QueueDims::new(h2g_queue_depth, h2g_pool_pages)?;
125+
let g2h = QueueDims::new(g2h_queue_size, g2h_pool_pages)?;
126+
let h2g = QueueDims::new(h2g_queue_size, h2g_pool_pages)?;
127127

128128
let transport_len = TransportArena::checked_query_size(g2h, h2g)?;
129129
fixed.checked_add(transport_len)
@@ -135,27 +135,27 @@ pub fn min_scratch_size(
135135
/// Validated address independent dimensions for one transport queue.
136136
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
137137
pub struct QueueDims {
138-
depth: NonZeroU16,
138+
size: NonZeroU16,
139139
pool_pages: NonZeroUsize,
140140
}
141141

142142
impl QueueDims {
143143
/// Validate one queue descriptor count and pool page count.
144-
pub fn new(depth: usize, pool_pages: usize) -> Option<Self> {
145-
let depth = u16::try_from(depth).ok()?;
146-
let depth = NonZeroU16::new(depth)?;
144+
pub fn new(size: usize, pool_pages: usize) -> Option<Self> {
145+
let size = u16::try_from(size).ok()?;
146+
let size = NonZeroU16::new(size)?;
147147

148-
if !depth.get().is_power_of_two() {
148+
if !size.get().is_power_of_two() {
149149
return None;
150150
}
151151

152152
let pool_pages = NonZeroUsize::new(pool_pages)?;
153-
Some(Self { depth, pool_pages })
153+
Some(Self { size, pool_pages })
154154
}
155155

156156
/// Number of descriptors in the queue.
157-
pub const fn depth(&self) -> NonZeroU16 {
158-
self.depth
157+
pub const fn size(&self) -> NonZeroU16 {
158+
self.size
159159
}
160160

161161
/// Number of pages in the queue's buffer pool.
@@ -165,7 +165,7 @@ impl QueueDims {
165165

166166
/// Compute the ring length, returning `None` on arithmetic overflow.
167167
pub fn checked_ring_len(&self) -> Option<usize> {
168-
virtq::Layout::checked_query_size(usize::from(self.depth.get()))
168+
virtq::Layout::checked_query_size(usize::from(self.size.get()))
169169
}
170170

171171
/// Compute the pool length, returning `None` on arithmetic overflow.
@@ -281,7 +281,7 @@ impl TransportArena {
281281

282282
/// Convert the arena's absolute addresses into offsets from the arena base.
283283
pub fn to_offsets(&self) -> (usize, usize, usize, usize) {
284-
// Already validated by `TransportArena::new`.
284+
#[allow(clippy::unwrap_used)] // `new` proves every stored offset fits in `usize`.
285285
let to_offset = |addr| usize::try_from(addr - self.g2h_ring_addr).unwrap();
286286

287287
(

src/hyperlight_guest/src/layout.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ pub fn snapshot_pt_gpa_base_gva() -> *mut u64 {
2323
pub fn snapshot_generation_gva() -> *mut u64 {
2424
scratch_top_gva(hyperlight_common::layout::SCRATCH_TOP_SNAPSHOT_GENERATION_OFFSET)
2525
}
26-
pub fn g2h_queue_depth_gva() -> *mut u64 {
27-
scratch_top_gva(hyperlight_common::layout::SCRATCH_TOP_G2H_QUEUE_DEPTH_OFFSET)
26+
pub fn g2h_queue_size_gva() -> *mut u64 {
27+
scratch_top_gva(hyperlight_common::layout::SCRATCH_TOP_G2H_QUEUE_SIZE_OFFSET)
2828
}
2929
pub fn transport_arena_gpa_gva() -> *mut u64 {
3030
scratch_top_gva(hyperlight_common::layout::SCRATCH_TOP_TRANSPORT_ARENA_GPA_OFFSET)
@@ -35,8 +35,8 @@ pub fn g2h_pool_pages_gva() -> *mut u64 {
3535
pub fn g2h_buffer_size_gva() -> *mut u64 {
3636
scratch_top_gva(hyperlight_common::layout::SCRATCH_TOP_G2H_BUFFER_SIZE_OFFSET)
3737
}
38-
pub fn h2g_queue_depth_gva() -> *mut u64 {
39-
scratch_top_gva(hyperlight_common::layout::SCRATCH_TOP_H2G_QUEUE_DEPTH_OFFSET)
38+
pub fn h2g_queue_size_gva() -> *mut u64 {
39+
scratch_top_gva(hyperlight_common::layout::SCRATCH_TOP_H2G_QUEUE_SIZE_OFFSET)
4040
}
4141
pub fn h2g_pool_pages_gva() -> *mut u64 {
4242
scratch_top_gva(hyperlight_common::layout::SCRATCH_TOP_H2G_POOL_PAGES_OFFSET)

src/hyperlight_guest_bin/src/transport.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ pub(crate) fn initialize() {
1616
// SAFETY: Generic initialization has mapped writable scratch metadata.
1717
let transport_arena_gpa = unsafe { layout::transport_arena_gpa_gva().read_volatile() };
1818

19-
let (depth, pages, g2h_bufsz) = read_published_g2h();
20-
let g2h = QueueDims::new(depth, pages).expect("invalid G2H queue dimensions");
19+
let (size, pages, g2h_bufsz) = read_published_g2h();
20+
let g2h = QueueDims::new(size, pages).expect("invalid G2H queue dimensions");
2121

22-
let (depth, pages, h2g_bufsz) = read_published_h2g();
23-
let h2g = QueueDims::new(depth, pages).expect("invalid H2G queue dimensions");
22+
let (size, pages, h2g_bufsz) = read_published_h2g();
23+
let h2g = QueueDims::new(size, pages).expect("invalid H2G queue dimensions");
2424

2525
assert!(g2h_bufsz > 0 && h2g_bufsz > 0);
2626

@@ -34,9 +34,9 @@ pub(crate) fn initialize() {
3434
let h2g_pool_gva = scratch_gva(arena.h2g_pool_addr());
3535

3636
let g2h_layout =
37-
unsafe { Layout::from_base(g2h_ring_gva, g2h.depth()) }.expect("G2H layout is invalid");
37+
unsafe { Layout::from_base(g2h_ring_gva, g2h.size()) }.expect("G2H layout is invalid");
3838
let h2g_layout =
39-
unsafe { Layout::from_base(h2g_ring_gva, h2g.depth()) }.expect("H2G layout is invalid");
39+
unsafe { Layout::from_base(h2g_ring_gva, h2g.size()) }.expect("H2G layout is invalid");
4040

4141
// Build the queues and prefill H2G before exposing either queue to the host.
4242
let context = GuestContext::new(
@@ -65,26 +65,26 @@ fn scratch_gva(gpa: u64) -> u64 {
6565

6666
fn read_published_g2h() -> (usize, usize, usize) {
6767
// SAFETY: Generic initialization has mapped writable scratch metadata.
68-
let depth_raw = unsafe { layout::g2h_queue_depth_gva().read_volatile() };
68+
let size_raw = unsafe { layout::g2h_queue_size_gva().read_volatile() };
6969
let pages_raw = unsafe { layout::g2h_pool_pages_gva().read_volatile() };
7070
let bufsz_raw = unsafe { layout::g2h_buffer_size_gva().read_volatile() };
7171

72-
let depth = usize::try_from(depth_raw).expect("G2H queue depth exceeds usize");
72+
let size = usize::try_from(size_raw).expect("G2H queue size exceeds usize");
7373
let pages = usize::try_from(pages_raw).expect("G2H pool page count exceeds usize");
7474
let bufsz = usize::try_from(bufsz_raw).expect("G2H buffer size exceeds usize");
7575

76-
(depth, pages, bufsz)
76+
(size, pages, bufsz)
7777
}
7878

7979
fn read_published_h2g() -> (usize, usize, usize) {
8080
// SAFETY: Generic initialization has mapped writable scratch metadata.
81-
let depth_raw = unsafe { layout::h2g_queue_depth_gva().read_volatile() };
81+
let size_raw = unsafe { layout::h2g_queue_size_gva().read_volatile() };
8282
let pages_raw = unsafe { layout::h2g_pool_pages_gva().read_volatile() };
8383
let bufsz_raw = unsafe { layout::h2g_buffer_size_gva().read_volatile() };
8484

85-
let depth = usize::try_from(depth_raw).expect("H2G queue depth exceeds usize");
85+
let size = usize::try_from(size_raw).expect("H2G queue size exceeds usize");
8686
let pages = usize::try_from(pages_raw).expect("H2G pool page count exceeds usize");
8787
let bufsz = usize::try_from(bufsz_raw).expect("H2G buffer size exceeds usize");
8888

89-
(depth, pages, bufsz)
89+
(size, pages, bufsz)
9090
}

src/hyperlight_host/src/mem/layout.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ pub(crate) struct SandboxMemoryLayout {
256256
/// The size of the scratch region in physical memory.
257257
scratch_size: usize,
258258
/// Number of descriptors in the G2H virtqueue.
259-
g2h_queue_depth: usize,
259+
g2h_queue_size: usize,
260260
/// Number of descriptors in the H2G virtqueue.
261-
h2g_queue_depth: usize,
261+
h2g_queue_size: usize,
262262
/// Capacity of each G2H upper-tier buffer.
263263
g2h_buffer_size: usize,
264264
/// Capacity of each H2G buffer.
@@ -301,8 +301,8 @@ impl Debug for SandboxMemoryLayout {
301301
&format_args!("{:#x}", self.output_data_size),
302302
)
303303
.field("Scratch Size", &format_args!("{:#x}", self.scratch_size))
304-
.field("G2H Queue Depth", &self.g2h_queue_depth)
305-
.field("H2G Queue Depth", &self.h2g_queue_depth)
304+
.field("G2H Queue Size", &self.g2h_queue_size)
305+
.field("H2G Queue Size", &self.h2g_queue_size)
306306
.field("G2H Buffer Size", &self.g2h_buffer_size)
307307
.field("H2G Buffer Size", &self.h2g_buffer_size)
308308
.field("G2H Pool Pages", &self.g2h_pool_pages)
@@ -362,17 +362,17 @@ impl SandboxMemoryLayout {
362362
}
363363
let input_data_size = cfg.get_input_data_size();
364364
let output_data_size = cfg.get_output_data_size();
365-
let g2h_queue_depth = cfg.get_g2h_queue_depth();
366-
let h2g_queue_depth = cfg.get_h2g_queue_depth();
365+
let g2h_queue_size = cfg.get_g2h_queue_size();
366+
let h2g_queue_size = cfg.get_h2g_queue_size();
367367
let g2h_buffer_size = cfg.get_g2h_buffer_size();
368368
let h2g_buffer_size = cfg.get_h2g_buffer_size();
369369
let g2h_pool_pages = cfg.get_g2h_pool_pages();
370370
let h2g_pool_pages = cfg.get_h2g_pool_pages();
371371
let min_scratch_size = hyperlight_common::layout::min_scratch_size(
372372
input_data_size,
373373
output_data_size,
374-
g2h_queue_depth,
375-
h2g_queue_depth,
374+
g2h_queue_size,
375+
h2g_queue_size,
376376
g2h_pool_pages,
377377
h2g_pool_pages,
378378
);
@@ -389,8 +389,8 @@ impl SandboxMemoryLayout {
389389
init_data_permissions,
390390
pt_size: None,
391391
scratch_size,
392-
g2h_queue_depth,
393-
h2g_queue_depth,
392+
g2h_queue_size,
393+
h2g_queue_size,
394394
g2h_buffer_size,
395395
h2g_buffer_size,
396396
g2h_pool_pages,
@@ -430,13 +430,13 @@ impl SandboxMemoryLayout {
430430
}
431431

432432
#[allow(dead_code)]
433-
pub(crate) fn get_g2h_queue_depth(&self) -> usize {
434-
self.g2h_queue_depth
433+
pub(crate) fn get_g2h_queue_size(&self) -> usize {
434+
self.g2h_queue_size
435435
}
436436

437437
#[allow(dead_code)]
438-
pub(crate) fn get_h2g_queue_depth(&self) -> usize {
439-
self.h2g_queue_depth
438+
pub(crate) fn get_h2g_queue_size(&self) -> usize {
439+
self.h2g_queue_size
440440
}
441441

442442
#[allow(dead_code)]
@@ -460,12 +460,12 @@ impl SandboxMemoryLayout {
460460
}
461461

462462
pub(crate) fn get_g2h_queue_dims(&self) -> hyperlight_common::layout::QueueDims {
463-
hyperlight_common::layout::QueueDims::new(self.g2h_queue_depth, self.g2h_pool_pages)
463+
hyperlight_common::layout::QueueDims::new(self.g2h_queue_size, self.g2h_pool_pages)
464464
.expect("validated G2H queue dimensions")
465465
}
466466

467467
pub(crate) fn get_h2g_queue_dims(&self) -> hyperlight_common::layout::QueueDims {
468-
hyperlight_common::layout::QueueDims::new(self.h2g_queue_depth, self.h2g_pool_pages)
468+
hyperlight_common::layout::QueueDims::new(self.h2g_queue_size, self.h2g_pool_pages)
469469
.expect("validated H2G queue dimensions")
470470
}
471471

@@ -494,8 +494,8 @@ impl SandboxMemoryLayout {
494494
let min_fixed_scratch = hyperlight_common::layout::min_scratch_size(
495495
self.input_data_size,
496496
self.output_data_size,
497-
self.g2h_queue_depth,
498-
self.h2g_queue_depth,
497+
self.g2h_queue_size,
498+
self.h2g_queue_size,
499499
self.g2h_pool_pages,
500500
self.h2g_pool_pages,
501501
);
@@ -853,8 +853,8 @@ mod tests {
853853
let minimum = hyperlight_common::layout::min_scratch_size(
854854
cfg.get_input_data_size(),
855855
cfg.get_output_data_size(),
856-
cfg.get_g2h_queue_depth(),
857-
cfg.get_h2g_queue_depth(),
856+
cfg.get_g2h_queue_size(),
857+
cfg.get_h2g_queue_size(),
858858
cfg.get_g2h_pool_pages(),
859859
cfg.get_h2g_pool_pages(),
860860
);

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,10 @@ impl SandboxMemoryManager<HostSharedMemory> {
641641
self.snapshot_count,
642642
)?;
643643

644-
// Record the G2H and H2G queue depths, pool page counts, and buffer sizes.
644+
// Record the G2H and H2G queue sizes, pool page counts, and buffer sizes.
645645
self.update_scratch_bookkeeping_item(
646-
SCRATCH_TOP_G2H_QUEUE_DEPTH_OFFSET,
647-
u64::try_from(self.layout.get_g2h_queue_depth())?,
646+
SCRATCH_TOP_G2H_QUEUE_SIZE_OFFSET,
647+
u64::try_from(self.layout.get_g2h_queue_size())?,
648648
)?;
649649
self.update_scratch_bookkeeping_item(
650650
SCRATCH_TOP_G2H_POOL_PAGES_OFFSET,
@@ -655,8 +655,8 @@ impl SandboxMemoryManager<HostSharedMemory> {
655655
u64::try_from(self.layout.get_g2h_buffer_size())?,
656656
)?;
657657
self.update_scratch_bookkeeping_item(
658-
SCRATCH_TOP_H2G_QUEUE_DEPTH_OFFSET,
659-
u64::try_from(self.layout.get_h2g_queue_depth())?,
658+
SCRATCH_TOP_H2G_QUEUE_SIZE_OFFSET,
659+
u64::try_from(self.layout.get_h2g_queue_size())?,
660660
)?;
661661
self.update_scratch_bookkeeping_item(
662662
SCRATCH_TOP_H2G_POOL_PAGES_OFFSET,

src/hyperlight_host/src/mem/virtq.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl Config {
161161
let h2g = QueueConfig::new(layout.get_h2g_queue_dims(), layout.get_h2g_buffer_size())?;
162162

163163
let h2g_prefill_chains =
164-
usize::from(h2g.dims.depth().get()).min(h2g.pool_len / h2g.buffer_size);
164+
usize::from(h2g.dims.size().get()).min(h2g.pool_len / h2g.buffer_size);
165165
let arena = layout.get_transport_arena();
166166

167167
Ok(Self {
@@ -206,7 +206,7 @@ impl<'a> Validator<'a> {
206206
fn validate_g2h<M: MemOps>(&self, mem: &M, ring: Range<u64>) -> Result<VirtqLayout> {
207207
// SAFETY: `ring` spans the configured image and `mem` keeps that image
208208
// valid for the duration of validation.
209-
let layout = unsafe { VirtqLayout::from_base(ring.start, self.config.g2h.dims.depth()) }
209+
let layout = unsafe { VirtqLayout::from_base(ring.start, self.config.g2h.dims.size()) }
210210
.map_err(|error| new_error!("invalid G2H ring layout: {error}"))?;
211211

212212
validate_canon_image(mem, layout, 0, |_, _| false)
@@ -227,7 +227,7 @@ impl<'a> Validator<'a> {
227227
) -> Result<VirtqLayout> {
228228
// SAFETY: `ring` spans the configured image and `mem` keeps that image
229229
// valid for the duration of validation.
230-
let layout = unsafe { VirtqLayout::from_base(ring.start, self.config.h2g.dims.depth()) }
230+
let layout = unsafe { VirtqLayout::from_base(ring.start, self.config.h2g.dims.size()) }
231231
.map_err(|error| new_error!("invalid H2G ring layout: {error}"))?;
232232

233233
let bufsz = self.config.h2g.buffer_size;
@@ -434,8 +434,8 @@ mod tests {
434434
fn memory_layout() -> SandboxMemoryLayout {
435435
let mut config = SandboxConfiguration::default();
436436
config.set_scratch_size(SCRATCH_SIZE);
437-
config.set_g2h_queue_depth(G2H_DEPTH as usize);
438-
config.set_h2g_queue_depth(H2G_DEPTH as usize);
437+
config.set_g2h_queue_size(G2H_DEPTH as usize);
438+
config.set_h2g_queue_size(H2G_DEPTH as usize);
439439
config.set_h2g_buffer_size(H2G_BUFFER_SIZE);
440440
config.set_g2h_pool_pages(G2H_POOL_PAGES);
441441
config.set_h2g_pool_pages(H2G_POOL_PAGES);

0 commit comments

Comments
 (0)