mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
virtio-devices: trim qualified paths
Import the modules used in the crate instead of spelling the full paths at every use site, and drop the now-unnecessary crate-level #![expect(clippy::absolute_paths)]. Signed-off-by: Henry Hrvoje Tonkovac <htonkovac@gmail.com> Assisted-by: Claude:Opus-4.8
This commit is contained in:
committed by
Rob Bradford
parent
2f2f709a0e
commit
74a749b960
@@ -84,6 +84,7 @@ use std::io::{ErrorKind, Read, Write};
|
||||
use std::num::Wrapping;
|
||||
use std::os::unix::io::{AsRawFd, RawFd};
|
||||
use std::time::{Duration, Instant};
|
||||
use std::{cmp, io};
|
||||
|
||||
use log::{debug, error, info, warn};
|
||||
use vm_memory::{ReadVolatile, WriteVolatile};
|
||||
@@ -230,7 +231,7 @@ where
|
||||
|
||||
// The maximum amount of data we can read in is limited by both the RX buffer size and
|
||||
// the peer available buffer space.
|
||||
let max_len = std::cmp::min(buf_capacity, self.peer_avail_credit());
|
||||
let max_len = cmp::min(buf_capacity, self.peer_avail_credit());
|
||||
|
||||
// Read data from the stream straight to the RX buffer, for maximum throughput.
|
||||
match pkt.read_volatile_from(&mut self.stream, max_len) {
|
||||
@@ -739,7 +740,7 @@ where
|
||||
"vsock: error shutting down host write side (lp={}, pp={}): {:?}",
|
||||
self.local_port,
|
||||
self.peer_port,
|
||||
std::io::Error::last_os_error()
|
||||
io::Error::last_os_error()
|
||||
);
|
||||
}
|
||||
self.host_write_shutdown = true;
|
||||
@@ -790,6 +791,7 @@ where
|
||||
#[cfg(test)]
|
||||
mod unit_tests {
|
||||
use std::io::{Error as IoError, Result as IoResult};
|
||||
use std::{result, thread};
|
||||
|
||||
use libc::EFD_NONBLOCK;
|
||||
use virtio_queue::QueueOwnedT;
|
||||
@@ -853,7 +855,7 @@ mod unit_tests {
|
||||
if self.read_buf.is_empty() {
|
||||
return Err(IoError::new(ErrorKind::WouldBlock, "EAGAIN"));
|
||||
}
|
||||
let len = std::cmp::min(data.len(), self.read_buf.len());
|
||||
let len = cmp::min(data.len(), self.read_buf.len());
|
||||
assert_ne!(len, 0);
|
||||
data[..len].copy_from_slice(&self.read_buf[..len]);
|
||||
self.read_buf = self.read_buf.split_off(len);
|
||||
@@ -885,7 +887,7 @@ mod unit_tests {
|
||||
fn read_volatile<B: BitmapSlice>(
|
||||
&mut self,
|
||||
data: &mut VolatileSlice<B>,
|
||||
) -> std::result::Result<usize, VolatileMemoryError> {
|
||||
) -> result::Result<usize, VolatileMemoryError> {
|
||||
let mut buf = vec![0u8; data.len()];
|
||||
let len = self.read(&mut buf).map_err(VolatileMemoryError::IOError)?;
|
||||
data.copy_from(&buf[..len]);
|
||||
@@ -897,7 +899,7 @@ mod unit_tests {
|
||||
fn write_volatile<B: BitmapSlice>(
|
||||
&mut self,
|
||||
data: &VolatileSlice<B>,
|
||||
) -> std::result::Result<usize, VolatileMemoryError> {
|
||||
) -> result::Result<usize, VolatileMemoryError> {
|
||||
let mut buf = vec![0u8; data.len()];
|
||||
data.copy_to(&mut buf);
|
||||
self.write(&buf).map_err(VolatileMemoryError::IOError)
|
||||
@@ -1101,9 +1103,7 @@ mod unit_tests {
|
||||
assert_eq!(ctx.pkt.op(), uapi::VSOCK_OP_REQUEST);
|
||||
assert!(ctx.conn.will_expire());
|
||||
assert!(!ctx.conn.has_expired());
|
||||
std::thread::sleep(std::time::Duration::from_millis(
|
||||
defs::CONN_REQUEST_TIMEOUT_MS,
|
||||
));
|
||||
thread::sleep(Duration::from_millis(defs::CONN_REQUEST_TIMEOUT_MS));
|
||||
assert!(ctx.conn.has_expired());
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
//! This module implements our vsock connection state machine. The heavy lifting is done by
|
||||
//! `connection::VsockConnection`, while this file only defines some constants and helper structs.
|
||||
|
||||
use std::{io, result};
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
mod connection;
|
||||
@@ -33,16 +35,16 @@ pub enum Error {
|
||||
TxBufFull,
|
||||
/// An I/O error occurred, when attempting to flush the connection TX buffer.
|
||||
#[error("Error flushing TX buffer")]
|
||||
TxBufFlush(#[source] std::io::Error),
|
||||
TxBufFlush(#[source] io::Error),
|
||||
/// An I/O error occurred, when attempting to write data to the host-side stream.
|
||||
#[error("Error writing to host side stream")]
|
||||
StreamWrite(#[source] std::io::Error),
|
||||
StreamWrite(#[source] io::Error),
|
||||
/// An I/O error occurred, when reading packet data.
|
||||
#[error("Error reading packet buffer")]
|
||||
PktBufRead,
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
type Result<T> = result::Result<T, Error>;
|
||||
|
||||
/// A vsock connection state.
|
||||
///
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
use std::cmp;
|
||||
use std::io::Write;
|
||||
use std::num::Wrapping;
|
||||
|
||||
@@ -85,7 +86,7 @@ impl TxBuf {
|
||||
// ring-buffer head wraps around.
|
||||
|
||||
// First copy length: we can only go from the head offset up to the total buffer size.
|
||||
let first_len = std::cmp::min(Self::SIZE - head_ofs, len);
|
||||
let first_len = cmp::min(Self::SIZE - head_ofs, len);
|
||||
src.copy_to_tx_buf(offset, &mut data[head_ofs..(head_ofs + first_len)])?;
|
||||
|
||||
// If the data didn't fit, the buffer head will wrap around, and pushing continues
|
||||
@@ -125,7 +126,7 @@ impl TxBuf {
|
||||
// head.
|
||||
|
||||
// First write length: the lesser of tail to slice end, or tail to head.
|
||||
let len_to_write = std::cmp::min(Self::SIZE - tail_ofs, self.len());
|
||||
let len_to_write = cmp::min(Self::SIZE - tail_ofs, self.len());
|
||||
|
||||
// It's safe to unwrap here, since we've already checked if the buffer was empty.
|
||||
let data = self.data.as_ref().unwrap();
|
||||
@@ -201,7 +202,7 @@ mod unit_tests {
|
||||
if self.err.is_some() {
|
||||
return Err(self.err.take().unwrap());
|
||||
}
|
||||
let len_to_push = std::cmp::min(self.capacity - self.data.len(), src.len());
|
||||
let len_to_push = cmp::min(self.capacity - self.data.len(), src.len());
|
||||
self.data.extend_from_slice(&src[..len_to_push]);
|
||||
Ok(len_to_push)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use std::os::unix::io::AsRawFd;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::{Arc, Barrier, RwLock};
|
||||
use std::{io, result};
|
||||
use std::{fs, io, result};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
@@ -47,6 +47,7 @@ use vmm_sys_util::eventfd::EventFd;
|
||||
/// - a backend FD.
|
||||
///
|
||||
use super::{VsockBackend, VsockPacket};
|
||||
use crate::device::ActivationContext;
|
||||
use crate::seccomp_filters::Thread;
|
||||
use crate::{
|
||||
ActivateResult, EPOLL_HELPER_EVENT_LAST, EpollHelper, EpollHelperError, EpollHelperHandler,
|
||||
@@ -461,8 +462,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn activate(&mut self, context: crate::device::ActivationContext) -> ActivateResult {
|
||||
let crate::device::ActivationContext {
|
||||
fn activate(&mut self, context: ActivationContext) -> ActivateResult {
|
||||
let ActivationContext {
|
||||
mem,
|
||||
interrupt_cb,
|
||||
queues,
|
||||
@@ -512,7 +513,7 @@ where
|
||||
}
|
||||
|
||||
fn shutdown(&mut self) {
|
||||
std::fs::remove_file(&self.path).ok();
|
||||
fs::remove_file(&self.path).ok();
|
||||
}
|
||||
|
||||
fn set_access_platform(&mut self, access_platform: Arc<dyn AccessPlatform>) {
|
||||
@@ -545,7 +546,7 @@ where
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
|
||||
fn snapshot(&mut self) -> result::Result<Snapshot, MigratableError> {
|
||||
Snapshot::new_from_state(&self.state())
|
||||
}
|
||||
}
|
||||
@@ -554,6 +555,8 @@ impl<B> Migratable for Vsock<B> where B: VsockBackend + Sync + 'static {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod unit_tests {
|
||||
use std::sync::atomic::AtomicU8;
|
||||
|
||||
use libc::EFD_NONBLOCK;
|
||||
|
||||
use super::super::unit_tests::{NoopVirtioInterrupt, TestContext};
|
||||
@@ -621,11 +624,11 @@ mod unit_tests {
|
||||
let memory = GuestMemoryAtomic::new(ctx.mem.clone());
|
||||
|
||||
// Test a bad activation.
|
||||
let bad_activate = ctx.device.activate(crate::device::ActivationContext {
|
||||
let bad_activate = ctx.device.activate(ActivationContext {
|
||||
mem: memory.clone(),
|
||||
interrupt_cb: Arc::new(NoopVirtioInterrupt {}),
|
||||
queues: Vec::new(),
|
||||
device_status: Arc::new(std::sync::atomic::AtomicU8::new(0)),
|
||||
device_status: Arc::new(AtomicU8::new(0)),
|
||||
});
|
||||
match bad_activate {
|
||||
Err(ActivateError::BadActivate) => (),
|
||||
@@ -634,7 +637,7 @@ mod unit_tests {
|
||||
|
||||
// Test a correct activation.
|
||||
ctx.device
|
||||
.activate(crate::device::ActivationContext {
|
||||
.activate(ActivationContext {
|
||||
mem: memory,
|
||||
interrupt_cb: Arc::new(NoopVirtioInterrupt {}),
|
||||
queues: vec![
|
||||
@@ -654,7 +657,7 @@ mod unit_tests {
|
||||
EventFd::new(EFD_NONBLOCK).unwrap(),
|
||||
),
|
||||
],
|
||||
device_status: Arc::new(std::sync::atomic::AtomicU8::new(0)),
|
||||
device_status: Arc::new(AtomicU8::new(0)),
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ mod packet;
|
||||
mod unix;
|
||||
|
||||
use std::os::unix::io::RawFd;
|
||||
use std::result;
|
||||
|
||||
use packet::VsockPacket;
|
||||
use thiserror::Error;
|
||||
@@ -103,7 +104,7 @@ pub enum VsockError {
|
||||
#[error("Encountered an unexpected read-only virtio descriptor")]
|
||||
UnwritableDescriptor,
|
||||
}
|
||||
type Result<T> = std::result::Result<T, VsockError>;
|
||||
type Result<T> = result::Result<T, VsockError>;
|
||||
|
||||
/// A passive, event-driven object, that needs to be notified whenever an epoll-able event occurs.
|
||||
///
|
||||
@@ -158,6 +159,7 @@ pub trait VsockBackend: VsockChannel + VsockEpollListener + Send {
|
||||
|
||||
#[cfg(any(test, fuzzing))]
|
||||
pub mod unit_tests {
|
||||
use std::io;
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, RwLock};
|
||||
@@ -178,10 +180,7 @@ pub mod unit_tests {
|
||||
pub struct NoopVirtioInterrupt {}
|
||||
|
||||
impl VirtioInterrupt for NoopVirtioInterrupt {
|
||||
fn trigger(
|
||||
&self,
|
||||
_int_type: VirtioInterruptType,
|
||||
) -> std::result::Result<(), std::io::Error> {
|
||||
fn trigger(&self, _int_type: VirtioInterruptType) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -190,7 +189,7 @@ pub mod unit_tests {
|
||||
_interrupt: u32,
|
||||
_eventfd: Option<EventFd>,
|
||||
_vm: &dyn hypervisor::Vm,
|
||||
) -> std::io::Result<()> {
|
||||
) -> io::Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
//! checked range, so it can be moved with volatile I/O without exposing raw host pointers.
|
||||
//! Multi-descriptor TX packets use a local bounce buffer.
|
||||
|
||||
use std::cmp;
|
||||
use std::io::{self, ErrorKind, Read, Write};
|
||||
use std::ops::Deref;
|
||||
|
||||
@@ -357,7 +358,7 @@ impl VsockPacket {
|
||||
|
||||
let desc_len = desc.len() as usize;
|
||||
if desc_len > 0 && offset < total_len {
|
||||
let to_copy = std::cmp::min(desc_len, total_len - offset);
|
||||
let to_copy = cmp::min(desc_len, total_len - offset);
|
||||
desc_chain
|
||||
.memory()
|
||||
.read_slice(&mut owned[offset..offset + to_copy], desc.addr())
|
||||
@@ -678,7 +679,7 @@ impl VsockPacket {
|
||||
|
||||
#[cfg(test)]
|
||||
mod unit_tests {
|
||||
use virtio_bindings::virtio_ring::VRING_DESC_F_WRITE;
|
||||
use virtio_bindings::virtio_ring::{VRING_DESC_F_NEXT, VRING_DESC_F_WRITE};
|
||||
use virtio_queue::QueueOwnedT;
|
||||
use vm_memory::GuestAddress;
|
||||
use vm_virtio::queue::testing::{VirtQueue as GuestQ, VirtqDesc as GuestQDesc};
|
||||
@@ -823,17 +824,13 @@ mod unit_tests {
|
||||
guest_txvq.dtable[0].set(
|
||||
0x0061_0000,
|
||||
VSOCK_PKT_HDR_SIZE as u32,
|
||||
virtio_bindings::virtio_ring::VRING_DESC_F_NEXT
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
VRING_DESC_F_NEXT.try_into().unwrap(),
|
||||
1,
|
||||
);
|
||||
guest_txvq.dtable[1].set(
|
||||
0x0061_1000,
|
||||
4 * 1024,
|
||||
virtio_bindings::virtio_ring::VRING_DESC_F_NEXT
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
VRING_DESC_F_NEXT.try_into().unwrap(),
|
||||
2,
|
||||
);
|
||||
guest_txvq.dtable[2].set(0x0061_2000, 4 * 1024, 0, 0);
|
||||
|
||||
@@ -13,6 +13,9 @@ mod muxer;
|
||||
mod muxer_killq;
|
||||
mod muxer_rxq;
|
||||
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::{io, num, result, str};
|
||||
|
||||
pub use Error as VsockUnixError;
|
||||
pub use muxer::VsockMuxer as VsockUnixBackend;
|
||||
use thiserror::Error;
|
||||
@@ -32,38 +35,38 @@ mod defs {
|
||||
pub enum Error {
|
||||
/// Error converting from UTF-8
|
||||
#[error("Error converting from UTF-8")]
|
||||
ConvertFromUtf8(#[source] std::str::Utf8Error),
|
||||
ConvertFromUtf8(#[source] str::Utf8Error),
|
||||
/// Error registering a new epoll-listening FD.
|
||||
#[error("Error registering a new epoll-listening FD")]
|
||||
EpollAdd(#[source] std::io::Error),
|
||||
EpollAdd(#[source] io::Error),
|
||||
/// Error creating an epoll FD.
|
||||
#[error("Error creating an epoll FD")]
|
||||
EpollFdCreate(#[source] std::io::Error),
|
||||
EpollFdCreate(#[source] io::Error),
|
||||
/// The host made an invalid vsock port connection request.
|
||||
#[error("The host made an invalid vsock port connection request")]
|
||||
InvalidPortRequest,
|
||||
/// Error parsing integer.
|
||||
#[error("Error parsing integer")]
|
||||
ParseInteger(#[source] std::num::ParseIntError),
|
||||
ParseInteger(#[source] num::ParseIntError),
|
||||
/// Error reading stream port.
|
||||
#[error("Error reading stream port")]
|
||||
ReadStreamPort(#[source] Box<Error>),
|
||||
/// Error accepting a new connection from the host-side Unix socket.
|
||||
#[error("Error accepting a new connection from the host-side Unix socket")]
|
||||
UnixAccept(#[source] std::io::Error),
|
||||
UnixAccept(#[source] io::Error),
|
||||
/// Error binding to the host-side Unix socket.
|
||||
#[error("Error binding to the host-side Unix socket")]
|
||||
UnixBind(#[source] std::io::Error),
|
||||
UnixBind(#[source] io::Error),
|
||||
/// Error connecting to a host-side Unix socket.
|
||||
#[error("Error connecting to a host-side Unix socket")]
|
||||
UnixConnect(#[source] std::io::Error),
|
||||
UnixConnect(#[source] io::Error),
|
||||
/// Error reading from host-side Unix socket.
|
||||
#[error("Error reading from host-side Unix socket")]
|
||||
UnixRead(#[source] std::io::Error),
|
||||
UnixRead(#[source] io::Error),
|
||||
/// Muxer connection limit reached.
|
||||
#[error("Muxer connection limit reached")]
|
||||
TooManyConnections,
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
type MuxerConnection = super::csm::VsockConnection<std::os::unix::net::UnixStream>;
|
||||
type Result<T> = result::Result<T, Error>;
|
||||
type MuxerConnection = super::csm::VsockConnection<UnixStream>;
|
||||
|
||||
@@ -44,6 +44,7 @@ use std::fs::File;
|
||||
use std::io::{self, ErrorKind, Read};
|
||||
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
||||
use std::os::unix::net::{UnixListener, UnixStream};
|
||||
use std::str;
|
||||
|
||||
use log::{debug, error, info, warn};
|
||||
|
||||
@@ -514,7 +515,7 @@ impl VsockMuxer {
|
||||
if command.len < connect_prefix.len() {
|
||||
return match opt_new_line_position {
|
||||
Some(_) => Err(Error::InvalidPortRequest),
|
||||
None => Err(Error::UnixRead(std::io::ErrorKind::WouldBlock.into())),
|
||||
None => Err(Error::UnixRead(io::ErrorKind::WouldBlock.into())),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -530,12 +531,12 @@ impl VsockMuxer {
|
||||
|
||||
// we parsed correctly `connect ` but need to wait for `\n`
|
||||
let new_line_position =
|
||||
opt_new_line_position.ok_or(Error::UnixRead(std::io::ErrorKind::WouldBlock.into()))?;
|
||||
opt_new_line_position.ok_or(Error::UnixRead(io::ErrorKind::WouldBlock.into()))?;
|
||||
|
||||
// we now have the newline, we will treat everything in between as the port
|
||||
let port_string_as_bytes = &command.buf[connect_prefix.len()..new_line_position];
|
||||
|
||||
std::str::from_utf8(port_string_as_bytes)
|
||||
str::from_utf8(port_string_as_bytes)
|
||||
.map_err(|_| Error::InvalidPortRequest)?
|
||||
.trim()
|
||||
.parse::<u32>()
|
||||
@@ -887,10 +888,11 @@ impl VsockMuxer {
|
||||
#[cfg(test)]
|
||||
mod unit_tests {
|
||||
use std::cmp::min;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::net::Shutdown;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
use std::{fs, thread};
|
||||
|
||||
use virtio_queue::QueueOwnedT;
|
||||
|
||||
@@ -921,7 +923,7 @@ mod unit_tests {
|
||||
|
||||
impl Drop for MuxerTestContext {
|
||||
fn drop(&mut self) {
|
||||
std::fs::remove_file(self.muxer.host_sock_path.as_str()).unwrap();
|
||||
fs::remove_file(self.muxer.host_sock_path.as_str()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1094,7 +1096,7 @@ mod unit_tests {
|
||||
}
|
||||
impl Drop for LocalListener {
|
||||
fn drop(&mut self) {
|
||||
std::fs::remove_file(&self.path).unwrap();
|
||||
fs::remove_file(&self.path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1499,9 +1501,7 @@ mod unit_tests {
|
||||
assert!(!ctx.muxer.has_pending_rx());
|
||||
|
||||
// Wait for the kill timers to expire.
|
||||
std::thread::sleep(std::time::Duration::from_millis(
|
||||
csm_defs::CONN_SHUTDOWN_TIMEOUT_MS,
|
||||
));
|
||||
thread::sleep(Duration::from_millis(csm_defs::CONN_SHUTDOWN_TIMEOUT_MS));
|
||||
|
||||
// Trigger a kill queue sweep, by requesting a new connection.
|
||||
ctx.init_pkt(
|
||||
|
||||
Reference in New Issue
Block a user