Skip to content
Draft

Vsock #5595

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ or_fun_call = "warn"

[profile.dev]
panic = "abort"
incremental = true

[profile.release]
panic = "abort"
Expand Down
1 change: 1 addition & 0 deletions src/vmm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ serde_json = "1.0.145"
slab = "0.4.11"
thiserror = "2.0.17"
timerfd = "1.5.0"
uds = "0.4.2"
userfaultfd = "0.9.0"
utils = { path = "../utils" }
uuid = "1.18.1"
Expand Down
3 changes: 2 additions & 1 deletion src/vmm/src/device_manager/pci_mngr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ mod tests {
use crate::vmm_config::entropy::EntropyDeviceConfig;
use crate::vmm_config::net::NetworkInterfaceConfig;
use crate::vmm_config::pmem::PmemConfig;
use crate::vmm_config::vsock::VsockDeviceConfig;
use crate::vmm_config::vsock::{VsockDeviceConfig, VsockType};

#[test]
fn test_device_manager_persistence() {
Expand Down Expand Up @@ -679,6 +679,7 @@ mod tests {
vsock_id: Some(vsock_dev_id.to_string()),
guest_cid: 3,
uds_path: tmp_sock_file.as_path().to_str().unwrap().to_string(),
vsock_type: VsockType::Stream,
};
insert_vsock_device(&mut vmm, &mut cmdline, &mut event_manager, vsock_config);
// Add an entropy device.
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/devices/virtio/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ mod tests {
// Remove the file so the path can be used by the socket.
temp_uds_path.remove().unwrap();
let uds_path = String::from(temp_uds_path.as_path().to_str().unwrap());
let backend = VsockUnixBackend::new(guest_cid, uds_path).unwrap();
let backend = VsockUnixBackend::new(guest_cid, uds_path, VsockType::Stream).unwrap();
let vsock = Vsock::new(guest_cid, backend).unwrap();
let vsock = Arc::new(Mutex::new(vsock));
let mmio_transport =
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/devices/virtio/vsock/csm/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ use crate::utils::wrap_usize_to_u32;
/// Used as an alias for `ReadVolatile + Write + WriteVolatile + AsRawFd`
/// (sadly, trait aliases are not supported,
/// <https://github.com/rust-lang/rfcs/pull/1733#issuecomment-243840014>).
pub trait VsockConnectionBackend: ReadVolatile + Write + WriteVolatile + AsRawFd {}
pub trait VsockConnectionBackend: ReadVolatile + Write + WriteVolatile + AsRawFd + Debug {}

/// A self-managing connection object, that handles communication between a guest-side AF_VSOCK
/// socket and a host-side `ReadVolatile + Write + WriteVolatile + AsRawFd` stream.
Expand Down
5 changes: 5 additions & 0 deletions src/vmm/src/devices/virtio/vsock/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::devices::virtio::persist::VirtioDeviceState;
use crate::devices::virtio::queue::FIRECRACKER_MAX_QUEUE_SIZE;
use crate::devices::virtio::transport::VirtioInterrupt;
use crate::snapshot::Persist;
use crate::vmm_config::vsock::VsockType;
use crate::vstate::memory::GuestMemoryMmap;

/// The Vsock serializable state.
Expand Down Expand Up @@ -46,6 +47,7 @@ pub enum VsockBackendState {
pub struct VsockUdsState {
/// The path for the UDS socket.
pub(crate) path: String,
pub(crate) vsock_type: VsockType,
}

/// A helper structure that holds the constructor arguments for VsockUnixBackend
Expand All @@ -72,6 +74,7 @@ impl Persist<'_> for VsockUnixBackend {
fn save(&self) -> Self::State {
VsockBackendState::Uds(VsockUdsState {
path: self.host_sock_path.clone(),
vsock_type: self.vsock_type.clone(),
})
}

Expand All @@ -83,6 +86,7 @@ impl Persist<'_> for VsockUnixBackend {
VsockBackendState::Uds(uds_state) => Ok(VsockUnixBackend::new(
constructor_args.cid,
uds_state.path.clone(),
uds_state.vsock_type.clone(),
)?),
}
}
Expand Down Expand Up @@ -145,6 +149,7 @@ pub(crate) mod tests {
fn save(&self) -> Self::State {
VsockBackendState::Uds(VsockUdsState {
path: "test".to_owned(),
vsock_type: VsockType::Stream,
})
}

Expand Down
90 changes: 85 additions & 5 deletions src/vmm/src/devices/virtio/vsock/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,22 @@
mod muxer;
mod muxer_killq;
mod muxer_rxq;

mod seqpacket;
use crate::devices::VsockError;
use crate::devices::virtio::vsock::packet::VsockPacketTx;
use crate::devices::virtio::vsock::{
VsockChannel as _, VsockEpollListener,
csm::{ConnState, VsockConnectionBackend, VsockCsmError},
packet::VsockPacketRx,
unix::seqpacket::SeqPacketConn,
};
pub use muxer::VsockMuxer as VsockUnixBackend;

use crate::devices::virtio::vsock::csm::VsockConnectionBackend;
use std::{
os::{fd::AsRawFd as _, unix::net::UnixStream},
time::Instant,
};
use vm_memory::io::{ReadVolatile, WriteVolatile};
use vmm_sys_util::epoll::EventSet;

mod defs {
/// Maximum number of established connections that we can handle.
Expand Down Expand Up @@ -47,6 +59,74 @@ pub enum VsockUnixBackendError {
TooManyConnections,
}

type MuxerConnection = super::csm::VsockConnection<std::os::unix::net::UnixStream>;
type MuxerStreamConnection = super::csm::VsockConnection<UnixStream>;
type MuxerSeqpacketConnetion = super::csm::VsockConnection<SeqPacketConn>;

#[derive(Debug)]
enum MuxerConn {
Stream(MuxerStreamConnection),
Seqpacket(MuxerSeqpacketConnetion),
}

macro_rules! forward_to_inner {
($self:ident, $method:ident $(, $args:expr )* ) => {
match $self {
MuxerConn::Stream(inner) => inner.$method($($args),*),
MuxerConn::Seqpacket(inner) => inner.$method($($args),*),
}
};
}

impl MuxerConn {
fn has_pending_rx(&self) -> bool {
forward_to_inner!(self, has_pending_rx)
}

fn as_raw_fd(&self) -> i32 {
forward_to_inner!(self, as_raw_fd)
}

fn kill(&mut self) {
forward_to_inner!(self, kill)
}

fn get_polled_evset(&self) -> EventSet {
forward_to_inner!(self, get_polled_evset)
}

fn will_expire(&self) -> bool {
forward_to_inner!(self, will_expire)
}

fn has_expired(&self) -> bool {
forward_to_inner!(self, has_expired)
}

fn send_bytes_raw(&mut self, buf: &[u8]) -> Result<usize, VsockCsmError> {
forward_to_inner!(self, send_bytes_raw, buf)
}

fn state(&self) -> ConnState {
forward_to_inner!(self, state)
}

fn expiry(&self) -> Option<Instant> {
forward_to_inner!(self, expiry)
}

fn recv_pkt(&mut self, pkt: &mut VsockPacketRx) -> Result<(), VsockError> {
forward_to_inner!(self, recv_pkt, pkt)
}

fn send_pkt(&mut self, pkt: &VsockPacketTx) -> Result<(), VsockError> {
forward_to_inner!(self, send_pkt, pkt)
}

fn notify(&mut self, evset: EventSet) {
forward_to_inner!(self, notify, evset)
}
}

impl VsockConnectionBackend for UnixStream {}

impl VsockConnectionBackend for std::os::unix::net::UnixStream {}
impl VsockConnectionBackend for SeqPacketConn {}
Loading