Skip to main content

Context

Struct Context 

Source
pub struct Context { /* private fields */ }
Expand description

A handle to an open RDMA device context.

The Context represents an active user-space session with a specific RDMA device. It serves as the root factory for creating all other RDMA resources.

§Resource Management & Shared Ownership

The Context uses shared ownership (via Arc) to manage the underlying device connection. This design simplifies resource management by allowing multiple handles to the same hardware context.

All resources created from a Context (such as ProtectionDomain, CompletionQueue, etc.) implicitly hold a clone of this Arc. This creates a robust ownership hierarchy:

  1. Child Keeps Parent Alive: Even if you drop your main Context handle, the underlying hardware connection remains open as long as any child resource (PD, QP, MR) is still alive.
  2. Automatic Cleanup: The actual ibv_close_device call only happens when the last reference to the context is dropped.

§Example: The Resource Lifecycle

use ibverbs_rs::ibverbs;

let context = ibverbs::open_device("mlx5_0")?;

// Create resources — they hold a reference to the context internally.
let pd = context.allocate_pd()?;
let cq = context.create_cq(16)?;

// Drop the context explicitly (optional).
// The device connection remains OPEN because 'pd' and 'cq' are still alive.
drop(context);

// End of scope: 'pd' and 'cq' are dropped, ref count hits zero, context closes.

Implementations§

Source§

impl Context

Source

pub fn create_cq(&self, min_cq_entries: u32) -> IbvResult<CompletionQueue>

Creates a Completion Queue (CQ) on this device.

The CQ is used to receive completion notifications for work requests posted to Queue Pairs. The returned CompletionQueue will hold a clone of this Context, keeping the device connection alive.

§Arguments
  • min_cq_entries — The minimum number of entries the CQ must support. The hardware may allocate a larger queue.
§Errors
Source

pub fn allocate_pd(&self) -> IbvResult<ProtectionDomain>

Allocates a Protection Domain (PD) for this context.

A PD is a container for grouping Queue Pairs and Memory Regions. The returned ProtectionDomain will hold a strong reference to this Context, ensuring the underlying device connection remains open even if the original Context handle is dropped.

§Errors
  • Returns IbvError::Resource if the PD limit for the device has been reached or if memory allocation fails.
Source§

impl Context

Source

pub fn from_device(dev: &Device<'_>) -> IbvResult<Self>

Opens a context for the given device and verifies port connectivity.

This function performs the following steps:

  1. Calls ibv_open_device to establish a context.
  2. Verifies the RDMA port is in ACTIVE or ARMED state.
§Errors
Source

pub fn device(&self) -> Device<'_>

Returns a Device handle for the hardware backing this context.

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.