Skip to content

Commit cc5013a

Browse files
committed
feat(virtq): estimate completion capacity based on ret type
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
1 parent a79694d commit cc5013a

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

src/hyperlight_common/src/virtq/pool.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,17 @@ impl<const L: usize, const U: usize> BufferPool<L, U> {
416416
inner: SyncWrap(Rc::new(RefCell::new(inner))),
417417
})
418418
}
419+
}
419420

421+
impl BufferPool {
420422
/// Upper slab slot size in bytes.
421423
pub const fn upper_slot_size() -> usize {
422-
U
424+
4096
425+
}
426+
427+
/// Lower slab slot size in bytes.
428+
pub const fn lower_slot_size() -> usize {
429+
256
423430
}
424431
}
425432

src/hyperlight_guest/src/virtq/context.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ pub struct GuestContext {
7272
g2h_producer: G2hProducer,
7373
/// host-to-guest driver
7474
h2g_producer: H2gProducer,
75-
/// Max writable bytes the host can write into a G2H completion.
76-
/// Derived from the G2H pool upper slab slot size.
77-
g2h_response_cap: usize,
7875
/// H2G slot size in bytes (each prefilled writable descriptor).
7976
h2g_slot_size: usize,
8077
/// snapshot generation counter
@@ -91,7 +88,6 @@ impl GuestContext {
9188
let size = g2h.pool_pages * PAGE_SIZE_USIZE;
9289
let g2h_pool =
9390
BufferPool::new(g2h.pool_gva, size).expect("failed to create G2H buffer pool");
94-
let g2h_response_cap = BufferPool::<256, 4096>::upper_slot_size();
9591
let g2h_producer =
9692
VirtqProducer::new(g2h.layout, GuestMemOps, GuestNotifier, g2h_pool.clone());
9793

@@ -105,7 +101,6 @@ impl GuestContext {
105101
let mut ctx = Self {
106102
g2h_producer,
107103
h2g_producer,
108-
g2h_response_cap,
109104
h2g_slot_size,
110105
generation,
111106
pending_replies: 0,
@@ -118,21 +113,20 @@ impl GuestContext {
118113

119114
/// Call a host function via the G2H virtqueue.
120115
///
121-
/// Uses the default completion capacity (4096 bytes) for the response
122-
/// buffer. For host functions known to return large payloads, use
116+
/// For host functions known to return payloads larger than 4096 bytes, use
123117
/// [`call_host_function_with_hint`](Self::call_host_function_with_hint).
124118
pub fn call_host_function<T: TryFrom<ReturnValue>>(
125119
&mut self,
126120
function_name: &str,
127121
parameters: Option<Vec<ParameterValue>>,
128122
return_type: ReturnType,
129123
) -> Result<T> {
130-
self.call_host_function_with_hint(
131-
function_name,
132-
parameters,
133-
return_type,
134-
self.g2h_response_cap,
135-
)
124+
let hint = if matches!(return_type, ReturnType::String | ReturnType::VecBytes) {
125+
BufferPool::upper_slot_size()
126+
} else {
127+
BufferPool::lower_slot_size()
128+
};
129+
self.call_host_function_with_hint(function_name, parameters, return_type, hint)
136130
}
137131

138132
/// Call a host function with an explicit response capacity hint.

0 commit comments

Comments
 (0)