Skip to content

Commit 332fb58

Browse files
committed
chore: Update naming of VsockError variants
Some of the variants still explicitly reflected the "two descriptors per packet" assumptions. Change naming here to instead refer to the length of the entire descriptor chain. Also include some more information about what the actual lengths and expections are. Signed-off-by: Patrick Roy <[email protected]>
1 parent efe8f72 commit 332fb58

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/vmm/src/devices/virtio/vsock/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ mod defs {
107107

108108
/// Vsock device related errors.
109109
#[derive(Debug, thiserror::Error, displaydoc::Display)]
110+
#[rustfmt::skip]
110111
pub enum VsockError {
111-
/// The vsock data/buffer virtio descriptor length is smaller than expected.
112-
BufDescTooSmall,
112+
/** The total length of the descriptor chain ({0}) is too short to hold a packet of length {1} + header */
113+
DescChainTooShortForPacket(usize, u32),
113114
/// Empty queue
114115
EmptyQueue,
115116
/// EventFd error: {0}
@@ -118,8 +119,9 @@ pub enum VsockError {
118119
GuestMemoryMmap(GuestMemoryError),
119120
/// Bounds check failed on guest memory pointer.
120121
GuestMemoryBounds,
121-
/// The vsock header descriptor length is too small: {0}
122-
HdrDescTooSmall(usize),
122+
/** The total length of the descriptor chain ({0}) is less than the number of bytes required\
123+
to hold a vsock packet header.*/
124+
DescChainTooShortForHeader(usize),
123125
/// The vsock header `len` field holds an invalid value: {0}
124126
InvalidPktLen(u32),
125127
/// A data fetch was attempted when no data was available.

src/vmm/src/devices/virtio/vsock/packet.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,30 @@ impl VsockPacket {
116116
/// Returns
117117
/// - [`VsockError::UnreadableDescriptor`] if the provided descriptor chain contains any
118118
/// descriptor not marked as writable.
119-
/// - [`VsockError::HdrDescTooSmall`] if the descriptor chain's total buffer length is
120-
/// insufficient to hold the 44 byte vsock header
119+
/// - [`VsockError::DescChainTooShortForHeader`] if the descriptor chain's total buffer length
120+
/// is insufficient to hold the 44 byte vsock header
121121
/// - [`VsockError::InvalidPktLen`] if the contained vsock header describes a vsock packet whose
122122
/// length would exceed [`defs::MAX_PKT_BUR_SIZE`].
123-
/// - [`VsockError::BufDescTooSmall`] if the contained vsock header describes a vsock packet
124-
/// whose length exceeds the descriptor chain's actual total buffer length.
123+
/// - [`VsockError::DescChainTooShortForPacket`] if the contained vsock header describes a vsock
124+
/// packet whose length exceeds the descriptor chain's actual total buffer length.
125125
pub fn from_tx_virtq_head(chain: DescriptorChain) -> Result<Self, VsockError> {
126126
let buffer = IoVecBuffer::from_descriptor_chain(chain)?;
127127

128128
let mut hdr = VsockPacketHeader::default();
129129
let header_bytes_read = buffer.read_at(hdr.as_mut_slice(), 0).unwrap_or(0);
130130
if header_bytes_read < VSOCK_PKT_HDR_SIZE as usize {
131-
return Err(VsockError::HdrDescTooSmall(header_bytes_read));
131+
return Err(VsockError::DescChainTooShortForHeader(header_bytes_read));
132132
}
133133

134134
if hdr.len > defs::MAX_PKT_BUF_SIZE {
135135
return Err(VsockError::InvalidPktLen(hdr.len));
136136
}
137137

138138
if (hdr.len as usize) > buffer.len() - VSOCK_PKT_HDR_SIZE as usize {
139-
return Err(VsockError::BufDescTooSmall);
139+
return Err(VsockError::DescChainTooShortForPacket(
140+
buffer.len(),
141+
hdr.len,
142+
));
140143
}
141144

142145
Ok(VsockPacket {
@@ -148,13 +151,13 @@ impl VsockPacket {
148151
/// Create the packet wrapper from an RX virtq chain head.
149152
///
150153
/// ## Errors
151-
/// Returns [`VsockError::HdrDescTooSmall`] if the descriptor chain's total buffer length is
152-
/// insufficient to hold the 44 byte vsock header
154+
/// Returns [`VsockError::DescChainTooShortForHeader`] if the descriptor chain's total buffer
155+
/// length is insufficient to hold the 44 byte vsock header
153156
pub fn from_rx_virtq_head(chain: DescriptorChain) -> Result<Self, VsockError> {
154157
let buffer = IoVecBufferMut::from_descriptor_chain(chain)?;
155158

156159
if buffer.len() < VSOCK_PKT_HDR_SIZE as usize {
157-
return Err(VsockError::HdrDescTooSmall(buffer.len()));
160+
return Err(VsockError::DescChainTooShortForHeader(buffer.len()));
158161
}
159162

160163
Ok(Self {
@@ -444,7 +447,12 @@ mod tests {
444447
.len
445448
.set(VSOCK_PKT_HDR_SIZE - 1);
446449
handler_ctx.guest_txvq.dtable[1].len.set(0);
447-
expect_asm_error!(tx, test_ctx, handler_ctx, VsockError::HdrDescTooSmall(_));
450+
expect_asm_error!(
451+
tx,
452+
test_ctx,
453+
handler_ctx,
454+
VsockError::DescChainTooShortForHeader(_)
455+
);
448456
}
449457

450458
// Test case: zero-length TX packet.
@@ -477,7 +485,12 @@ mod tests {
477485
create_context!(test_ctx, handler_ctx);
478486
set_pkt_len(1024, &handler_ctx.guest_txvq.dtable[0], &test_ctx.mem);
479487
handler_ctx.guest_txvq.dtable[0].flags.set(0);
480-
expect_asm_error!(tx, test_ctx, handler_ctx, VsockError::BufDescTooSmall);
488+
expect_asm_error!(
489+
tx,
490+
test_ctx,
491+
handler_ctx,
492+
VsockError::DescChainTooShortForPacket(44, 1024)
493+
);
481494
}
482495

483496
// Test case: error on write-only buf descriptor.
@@ -495,7 +508,12 @@ mod tests {
495508
create_context!(test_ctx, handler_ctx);
496509
set_pkt_len(8 * 1024, &handler_ctx.guest_txvq.dtable[0], &test_ctx.mem);
497510
handler_ctx.guest_txvq.dtable[1].len.set(4 * 1024);
498-
expect_asm_error!(tx, test_ctx, handler_ctx, VsockError::BufDescTooSmall);
511+
expect_asm_error!(
512+
tx,
513+
test_ctx,
514+
handler_ctx,
515+
VsockError::DescChainTooShortForPacket(4140, 8192)
516+
);
499517
}
500518
}
501519

@@ -530,7 +548,12 @@ mod tests {
530548
.len
531549
.set(VSOCK_PKT_HDR_SIZE - 1);
532550
handler_ctx.guest_rxvq.dtable[1].len.set(0);
533-
expect_asm_error!(rx, test_ctx, handler_ctx, VsockError::HdrDescTooSmall(_));
551+
expect_asm_error!(
552+
rx,
553+
test_ctx,
554+
handler_ctx,
555+
VsockError::DescChainTooShortForHeader(_)
556+
);
534557
}
535558
}
536559

0 commit comments

Comments
 (0)