pub struct VsockConnectionManager<H: Hal, T: Transport> { /* private fields */ }
Expand description

A higher level interface for VirtIO socket (vsock) devices.

This keeps track of multiple vsock connections.

Example

use virtio_drivers::device::socket::{VirtIOSocket, VsockAddr, VsockConnectionManager};

let mut socket = VsockConnectionManager::new(VirtIOSocket::<HalImpl, _>::new(transport)?);

// Start a thread to call `socket.poll()` and handle events.

let remote_address = VsockAddr { cid: 2, port: 42 };
let local_port = 1234;
socket.connect(remote_address, local_port)?;

// Wait until `socket.poll()` returns an event indicating that the socket is connected.

socket.send(remote_address, local_port, "Hello world".as_bytes())?;

socket.shutdown(remote_address, local_port)?;

Implementations§

source§

impl<H: Hal, T: Transport> VsockConnectionManager<H, T>

source

pub fn new(driver: VirtIOSocket<H, T>) -> Self

Construct a new connection manager wrapping the given low-level VirtIO socket driver.

source

pub fn guest_cid(&self) -> u64

Returns the CID which has been assigned to this guest.

source

pub fn listen(&mut self, port: u32)

Allows incoming connections on the given port number.

source

pub fn unlisten(&mut self, port: u32)

Stops allowing incoming connections on the given port number.

source

pub fn connect(&mut self, destination: VsockAddr, src_port: u32) -> Result

Sends a request to connect to the given destination.

This returns as soon as the request is sent; you should wait until poll returns a VsockEventType::Connected event indicating that the peer has accepted the connection before sending data.

source

pub fn send( &mut self, destination: VsockAddr, src_port: u32, buffer: &[u8] ) -> Result

Sends the buffer to the destination.

source

pub fn poll(&mut self) -> Result<Option<VsockEvent>>

Polls the vsock device to receive data or other updates.

source

pub fn recv( &mut self, peer: VsockAddr, src_port: u32, buffer: &mut [u8] ) -> Result<usize>

Reads data received from the given connection.

source

pub fn recv_buffer_available_bytes( &mut self, peer: VsockAddr, src_port: u32 ) -> Result<usize>

Returns the number of bytes currently available in the recv buffer.

source

pub fn update_credit(&mut self, peer: VsockAddr, src_port: u32) -> Result

Sends a credit update to the given peer.

source

pub fn wait_for_event(&mut self) -> Result<VsockEvent>

Blocks until we get some event from the vsock device.

source

pub fn shutdown(&mut self, destination: VsockAddr, src_port: u32) -> Result

Requests to shut down the connection cleanly.

This returns as soon as the request is sent; you should wait until poll returns a VsockEventType::Disconnected event if you want to know that the peer has acknowledged the shutdown.

source

pub fn force_close(&mut self, destination: VsockAddr, src_port: u32) -> Result

Forcibly closes the connection without waiting for the peer.

Auto Trait Implementations§

§

impl<H, T> RefUnwindSafe for VsockConnectionManager<H, T>where H: RefUnwindSafe, T: RefUnwindSafe,

§

impl<H, T> !Send for VsockConnectionManager<H, T>

§

impl<H, T> !Sync for VsockConnectionManager<H, T>

§

impl<H, T> Unpin for VsockConnectionManager<H, T>where H: Unpin, T: Unpin,

§

impl<H, T> UnwindSafe for VsockConnectionManager<H, T>where H: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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.

source§

impl<T, U> Into<U> for Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.