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:
- Child Keeps Parent Alive: Even if you drop your main
Contexthandle, the underlying hardware connection remains open as long as any child resource (PD, QP, MR) is still alive. - Automatic Cleanup: The actual
ibv_close_devicecall 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
impl Context
Sourcepub fn create_cq(&self, min_cq_entries: u32) -> IbvResult<CompletionQueue>
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
- Returns
IbvError::InvalidInputifmin_cq_entriesexceeds the device’s capabilities. - Returns
IbvError::Resourceif the system cannot allocate the queue resources (e.g., out of memory).
Sourcepub fn allocate_pd(&self) -> IbvResult<ProtectionDomain>
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::Resourceif the PD limit for the device has been reached or if memory allocation fails.
Source§impl Context
impl Context
Sourcepub fn from_device(dev: &Device<'_>) -> IbvResult<Self>
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:
- Calls
ibv_open_deviceto establish a context. - Verifies the RDMA port is in
ACTIVEorARMEDstate.
§Errors
- Returns
IbvError::Permissionif the process lacks permission to access RDMA devices. - Returns
IbvError::Driveriflibibverbsfails to open the device for OS-specific reasons. - Returns
IbvError::Resourceif the RDMA port is in any state other thanACTIVEorARMED.