Skip to main content

VsockConnection

Struct VsockConnection 

Source
pub struct VsockConnection {
    pub id: VsockConnectionId,
    pub internal_fd: OwnedFd,
    pub injected_notify: Option<Sender<()>>,
    pub guest_cid: u64,
    pub connect: bool,
    pub rx_queue: RxOps,
    pub fwd_cnt: Wrapping<u32>,
    pub peer_buf_alloc: u32,
    pub peer_fwd_cnt: Wrapping<u32>,
    pub rx_cnt: Wrapping<u32>,
    /* private fields */
}
Expand description

A single host↔guest vsock connection.

Owns the internal end of the socketpair. When this entry is removed from the manager (or the manager is dropped), OwnedFd::drop closes the fd.

The state machine is implicit:

  • connect == false: handshake in progress
  • connect == true: data transfer enabled
  • rx_queue contains RxOps::RESET: connection is being torn down

Fields§

§id: VsockConnectionId§internal_fd: OwnedFd§injected_notify: Option<Sender<()>>

Fired by vCPU thread’s poll_vsock_rx after OP_REQUEST is written to guest memory. The daemon blocks on this before returning the fd — guarantees the guest will see the OP_REQUEST and respond (RST or RESPONSE) so the daemon’s read won’t hang indefinitely.

§guest_cid: u64§connect: bool

Whether the connection handshake is complete.

§rx_queue: RxOps

Per-connection pending RX operations (bitmask priority queue).

§fwd_cnt: Wrapping<u32>

Total bytes forwarded from host tx_buf to the actual host stream. Sent to guest in every packet so it knows how much host buffer is free.

§peer_buf_alloc: u32

Guest’s advertised buffer allocation (extracted from every incoming pkt).

§peer_fwd_cnt: Wrapping<u32>

Guest’s forwarded count (extracted from every incoming packet).

§rx_cnt: Wrapping<u32>

Total bytes sent TO the guest via RX virtqueue.

Implementations§

Source§

impl VsockConnection

Source

pub fn new_local_init( id: VsockConnectionId, guest_cid: u64, fd: OwnedFd, injected_notify: Sender<()>, ) -> VsockConnection

Creates a new connection for a host-initiated connect (OP_REQUEST).

Source

pub fn peer_avail_credit(&self) -> usize

Returns the number of bytes the guest can still receive.

peer_buf_alloc - (rx_cnt - peer_fwd_cnt) = total guest buffer minus bytes currently in-flight (sent but not yet consumed by the guest).

Source

pub fn update_peer_credit(&mut self, buf_alloc: u32, fwd_cnt: u32)

Updates peer credit state from an incoming guest packet. Also clears any in-flight CREDIT_REQUEST marker: the peer has just told us the fresh state, so whatever we asked about is answered.

Source

pub fn maybe_request_credit(&mut self)

Enqueues a CREDIT_REQUEST op if peer credit has fallen below half the peer’s advertised buffer and no request is already in flight.

Call from the RX path after sending data the peer now has to process. Sending the request proactively — rather than only when credit hits zero — means we refresh our (possibly stale) view of peer_fwd_cnt before we actually deplete our window, avoiding a full TX stall.

Source

pub fn note_credit_request_sent(&mut self)

Marks a CREDIT_REQUEST as in-flight without going through the RxOps queue. Used when the caller emits the request packet directly in the RW-with-zero-credit fallback path — we still want the pending flag set so maybe_request_credit doesn’t duplicate us.

Source

pub fn credit_request_pending(&self) -> bool

True iff we’re waiting on the peer for a credit update.

Source

pub fn mark_peer_no_recv(&mut self)

Peer sent OP_SHUTDOWN with F_RECEIVE. Record the half-close so the RX injection path stops trying to deliver more RW packets.

Source

pub const fn peer_no_recv(&self) -> bool

True iff the peer has half-closed its receive side.

Source

pub const fn accepts_data(&self) -> bool

Whether we may send more data to the peer. False once the handshake hasn’t completed or the peer has told us it won’t accept more.

Source

pub fn advance_fwd_cnt(&mut self, bytes: u32)

Called after data is written to the host stream (from guest OP_RW). Advances fwd_cnt and enqueues a CreditUpdate if buffer is getting low.

Source

pub fn record_rx(&mut self, bytes: u32)

Records bytes sent to the guest and returns the new rx_cnt.

Source

pub fn mark_credit_sent(&mut self)

Marks that a CreditUpdate was sent to the guest (syncs last_fwd_cnt).

Auto Trait Implementations§

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.

Source§

impl<T> Instrument for T

Source§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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