Skip to main content

Vsock

Struct Vsock 

Source
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

Source

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

Source

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.

Source

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.

Source

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.

Source

pub fn bind(port: u32) -> Result<Self>

Bind to a Vsock socket with a given port and return a vsock handle.

§Arguments
  • port - Port to bind to.
§Returns

A Vsock instance representing the bound socket, or an error if the bind operation fails.

Source

pub fn listen(&self) -> Result<()>

Listen for incoming connections on a Vsock socket.

Source

pub fn accept(&self) -> Result<Self>

Accept an incoming connection on a Vsock socket and return a new Vsock instance representing the accepted connection.

Source

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.

Source

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 to chunk_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

Source§

fn as_fd(&self) -> BorrowedFd<'_>

Return the Vsock file descriptor as a BorrowedFd for use with nix socket operations.

Source§

impl AsRawFd for Vsock

Source§

fn as_raw_fd(&self) -> RawFd

Return the raw file descriptor of the Vsock socket.

Source§

impl Read for Vsock

Available on crate feature std-io only.
Source§

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>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
where Self: Sized,

🔬This is a nightly-only experimental API. (read_array)
Read and return a fixed array of bytes from this source. Read more
Source§

impl Write for Vsock

Available on crate feature std-io only.
Source§

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<()>

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.

1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl Freeze for Vsock

§

impl RefUnwindSafe for Vsock

§

impl Send for Vsock

§

impl Sync for Vsock

§

impl Unpin for Vsock

§

impl UnsafeUnpin for Vsock

§

impl UnwindSafe for Vsock

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more