pub struct Vsock { /* private fields */ }Expand description
Abstraction over a vsock (AF_VSOCK) socket for communication between hosts and virtual machines
or enclaves.
Implementations§
Source§impl Vsock
impl Vsock
Sourcepub const ANY_CID_ADDR: u32 = u32::MAX
pub const ANY_CID_ADDR: u32 = u32::MAX
CID address to bind to for accepting connections from any CID. This is a convention in vsock
Sourcepub const PARENT_NE_CID_ADDR: u32 = 3
pub const PARENT_NE_CID_ADDR: u32 = 3
CID address used for communication with the Nitro Enclave parent instance. This is a convention in AWS Nitro Enclaves and is not meant to be used for general vsock communication outside of Nitro Enclaves.
Sourcepub fn connect(cid: u32, port: u32) -> Result<Self>
pub fn connect(cid: u32, port: u32) -> Result<Self>
Connect to a Vsock socket with a given CID and port and return a vsock handle.
§Arguments
cid- CID address to connect to.port- Port to connect to.
§Returns
A Vsock instance representing the connected socket or an error if the connection
fails after 5 attempts. The method implements an exponential backoff strategy for
retrying failed connection attempts.
Sourcepub fn connect_with_max_attempts(
cid: u32,
port: u32,
max_attempts: usize,
) -> Result<Self>
pub fn connect_with_max_attempts( cid: u32, port: u32, max_attempts: usize, ) -> Result<Self>
Connect to a Vsock socket with a given CID and port and return a vsock handle.
§Arguments
cid- CID address to connect to.port- Port to connect to.max_attempts- Maximum number of attempts to connect before giving up.
§Returns
A Vsock instance representing the connected socket or an error if the connection
fails after the specified number of attempts. The method implements an exponential
backoff strategy for retrying failed connection attempts.
Sourcepub fn accept(&self) -> Result<Self>
pub fn accept(&self) -> Result<Self>
Accept an incoming connection on a Vsock socket and return a new Vsock instance representing the accepted connection.
Sourcepub fn send(&self, data: &[u8], chunk_size: usize) -> Result<()>
pub fn send(&self, data: &[u8], chunk_size: usize) -> Result<()>
Send a slice of bytes through a Vsock socket. The method makes assumptions about the transport chunk size to optimize performance.
§Arguments
data- Slice of bytes to be sent through the Vsock socket entirely.chunk_size- Size of each chunk to send through the socket.
§Returns
Empty result indicating success or error if the operation fails.
Sourcepub fn receive(
&self,
max_data_size: usize,
chunk_size: usize,
) -> Result<Vec<u8>>
pub fn receive( &self, max_data_size: usize, chunk_size: usize, ) -> Result<Vec<u8>>
Receive bytes from a Vsock socket. The method reads data in chunks up to the specified maximum buffer size. The chunk size can be configured for optimal performance.
§Arguments
max_data_size- Total capacity of the receive buffer in bytes; must be greater than or equal tochunk_size.chunk_size- Size of each chunk to read from the socket in bytes.
§Returns
A Vec<u8> containing the received bytes on success, truncated to the number of bytes
actually received. Returns an error if max_data_size is less than chunk_size, if the
buffer fills completely before the peer closes the connection, or if the underlying socket
operation fails.
Trait Implementations§
Source§impl AsFd for Vsock
impl AsFd for Vsock
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Return the Vsock file descriptor as a BorrowedFd for use with nix socket operations.
Source§impl Read for Vsock
Available on crate feature std-io only.
impl Read for Vsock
std-io only.Source§fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>
Read bytes from the Vsock socket into a provided buffer. The method assumes that the buffer is large
enough to hold the incoming data for optimal performance. For larger buffers, the receive method
with chunking and data size cap should be used instead.
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
Source§impl Write for Vsock
Available on crate feature std-io only.
impl Write for Vsock
std-io only.Source§fn write(&mut self, buf: &[u8]) -> IoResult<usize>
fn write(&mut self, buf: &[u8]) -> IoResult<usize>
Write a slice of bytes to the Vsock socket. The method assumes that the entire buffer can be sent
in one call for optimal performance. For larger buffers, the send method with chunking should
be used instead.
Source§fn flush(&mut self) -> IoResult<()>
fn flush(&mut self) -> IoResult<()>
Shut down the write side of the Vsock socket, signaling EOF to the peer.
This allows the peer’s read_to_end or read_to_string calls to return cleanly.
After this call, any further attempt to write to the socket will fail.
Note: This operation is irreversible — the socket cannot be written to afterwards.
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)