rdma-io 0.0.2

Safe async Rust bindings for RDMA programming over libibverbs and librdmacm
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! RDMA Connection Manager (rdma_cm).
//!
//! Provides TCP-like connection semantics over RDMA. This is the primary
//! way to use iWARP (siw) and the recommended approach for RoCE.

use std::net::SocketAddr;
use std::os::unix::io::RawFd;
use std::sync::Arc;

use rdma_io_sys::ibverbs::*;
use rdma_io_sys::rdmacm::*;

use crate::Result;
use crate::cq::CompletionQueue;
use crate::device::Context;
use crate::error::{from_ptr, from_ret_errno};
use crate::pd::ProtectionDomain;
use crate::qp::QpInitAttr;

/// Port space for CM connections.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PortSpace {
    Tcp,
    Udp,
    Ib,
    Ipoib,
}

impl PortSpace {
    fn as_raw(self) -> u32 {
        match self {
            Self::Tcp => RDMA_PS_TCP,
            Self::Udp => RDMA_PS_UDP,
            Self::Ib => RDMA_PS_IB,
            Self::Ipoib => RDMA_PS_IPOIB,
        }
    }
}

/// CM event types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CmEventType {
    AddrResolved,
    AddrError,
    RouteResolved,
    RouteError,
    ConnectRequest,
    ConnectResponse,
    ConnectError,
    Unreachable,
    Rejected,
    Established,
    Disconnected,
    DeviceRemoval,
    MulticastJoin,
    MulticastError,
    AddrChange,
    TimewaitExit,
    Unknown(u32),
}

impl CmEventType {
    fn from_raw(v: u32) -> Self {
        match v {
            RDMA_CM_EVENT_ADDR_RESOLVED => Self::AddrResolved,
            RDMA_CM_EVENT_ADDR_ERROR => Self::AddrError,
            RDMA_CM_EVENT_ROUTE_RESOLVED => Self::RouteResolved,
            RDMA_CM_EVENT_ROUTE_ERROR => Self::RouteError,
            RDMA_CM_EVENT_CONNECT_REQUEST => Self::ConnectRequest,
            RDMA_CM_EVENT_CONNECT_RESPONSE => Self::ConnectResponse,
            RDMA_CM_EVENT_CONNECT_ERROR => Self::ConnectError,
            RDMA_CM_EVENT_UNREACHABLE => Self::Unreachable,
            RDMA_CM_EVENT_REJECTED => Self::Rejected,
            RDMA_CM_EVENT_ESTABLISHED => Self::Established,
            RDMA_CM_EVENT_DISCONNECTED => Self::Disconnected,
            RDMA_CM_EVENT_DEVICE_REMOVAL => Self::DeviceRemoval,
            RDMA_CM_EVENT_MULTICAST_JOIN => Self::MulticastJoin,
            RDMA_CM_EVENT_MULTICAST_ERROR => Self::MulticastError,
            RDMA_CM_EVENT_ADDR_CHANGE => Self::AddrChange,
            RDMA_CM_EVENT_TIMEWAIT_EXIT => Self::TimewaitExit,
            other => Self::Unknown(other),
        }
    }
}

/// Connection parameters for `connect` / `accept`.
#[derive(Debug, Clone)]
pub struct ConnParam {
    /// Responder resources (max incoming RDMA read/atomic).
    pub responder_resources: u8,
    /// Initiator depth (max outstanding RDMA read/atomic).
    pub initiator_depth: u8,
    /// Retry count.
    pub retry_count: u8,
    /// RNR retry count (7 = infinite).
    pub rnr_retry_count: u8,
}

impl Default for ConnParam {
    fn default() -> Self {
        Self {
            responder_resources: 1,
            initiator_depth: 1,
            retry_count: 7,
            rnr_retry_count: 7,
        }
    }
}

impl ConnParam {
    fn to_raw(&self) -> rdma_conn_param {
        rdma_conn_param {
            responder_resources: self.responder_resources,
            initiator_depth: self.initiator_depth,
            retry_count: self.retry_count,
            rnr_retry_count: self.rnr_retry_count,
            ..Default::default()
        }
    }
}

/// An rdma_cm event channel.
///
/// Used to receive CM events (address resolved, connected, etc.).
pub struct EventChannel {
    inner: *mut rdma_event_channel,
}

// Safety: The event channel fd is process-global; get_event serialized by caller.
unsafe impl Send for EventChannel {}
unsafe impl Sync for EventChannel {}

impl Drop for EventChannel {
    fn drop(&mut self) {
        unsafe { rdma_destroy_event_channel(self.inner) };
    }
}

impl EventChannel {
    /// Create a new event channel.
    pub fn new() -> Result<Self> {
        let ch = from_ptr(unsafe { rdma_create_event_channel() })?;
        Ok(Self { inner: ch })
    }

    /// Block until the next CM event arrives.
    pub fn get_event(&self) -> Result<CmEvent> {
        let mut event: *mut rdma_cm_event = std::ptr::null_mut();
        from_ret_errno(unsafe { rdma_get_cm_event(self.inner, &mut event) })?;
        Ok(CmEvent { inner: event })
    }

    /// Non-blocking get_event. Returns `Error::WouldBlock` if no event is pending.
    pub fn try_get_event(&self) -> Result<CmEvent> {
        let mut event: *mut rdma_cm_event = std::ptr::null_mut();
        let ret = unsafe { rdma_get_cm_event(self.inner, &mut event) };
        if ret != 0 {
            let e = std::io::Error::last_os_error();
            if e.kind() == std::io::ErrorKind::WouldBlock {
                return Err(crate::Error::WouldBlock);
            }
            return Err(crate::Error::Verbs(e));
        }
        Ok(CmEvent { inner: event })
    }

    /// Raw file descriptor for async reactor registration.
    pub fn fd(&self) -> RawFd {
        unsafe { (*self.inner).fd }
    }

    /// Set the event channel fd to non-blocking mode.
    pub fn set_nonblocking(&self) -> Result<()> {
        let fd = self.fd();
        let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
        if flags < 0 {
            return Err(crate::Error::Verbs(std::io::Error::last_os_error()));
        }
        let ret = unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) };
        if ret < 0 {
            return Err(crate::Error::Verbs(std::io::Error::last_os_error()));
        }
        Ok(())
    }

    /// Raw pointer.
    pub fn as_raw(&self) -> *mut rdma_event_channel {
        self.inner
    }
}

/// A CM event received from an [`EventChannel`].
///
/// **Must be acknowledged** via [`ack`](CmEvent::ack) (consumed on ack).
pub struct CmEvent {
    inner: *mut rdma_cm_event,
}

// Safety: CM events are tied to a channel; single-threaded access pattern.
unsafe impl Send for CmEvent {}

impl CmEvent {
    /// The event type.
    pub fn event_type(&self) -> CmEventType {
        CmEventType::from_raw(unsafe { (*self.inner).event })
    }

    /// Status code (0 = success for most events).
    pub fn status(&self) -> i32 {
        unsafe { (*self.inner).status }
    }

    /// For `ConnectRequest` events, the CM ID of the new incoming connection.
    ///
    /// The returned `CmId` is **not** owned — it must be accepted or rejected.
    /// The caller takes ownership after `accept`.
    pub fn cm_id_raw(&self) -> *mut rdma_cm_id {
        unsafe { (*self.inner).id }
    }

    /// Acknowledge and consume this event. Must be called for every event.
    pub fn ack(self) {
        unsafe { rdma_ack_cm_event(self.inner) };
        std::mem::forget(self); // prevent double-free in Drop
    }
}

impl Drop for CmEvent {
    fn drop(&mut self) {
        // If not ack'd, ack now to avoid leaking the event.
        let ret = unsafe { rdma_ack_cm_event(self.inner) };
        if ret != 0 {
            tracing::error!(
                "rdma_ack_cm_event failed: {}",
                std::io::Error::last_os_error()
            );
        }
    }
}

/// An RDMA CM identifier — the core handle for connection management.
///
/// Wraps `rdma_cm_id*`. Use for both client (active) and server (passive) sides.
pub struct CmId {
    pub(crate) inner: *mut rdma_cm_id,
    /// Whether this CmId owns the underlying rdma_cm_id (should call rdma_destroy_id).
    owned: bool,
}

// Safety: rdma_cm_id operations are serialized by the caller.
unsafe impl Send for CmId {}
unsafe impl Sync for CmId {}

impl Drop for CmId {
    fn drop(&mut self) {
        if self.owned {
            let ret = unsafe { rdma_destroy_id(self.inner) };
            if ret != 0 {
                tracing::error!(
                    "rdma_destroy_id failed: {}",
                    std::io::Error::last_os_error()
                );
            }
        }
    }
}

impl CmId {
    /// Create a new CM ID on the given event channel.
    pub fn new(channel: &EventChannel, port_space: PortSpace) -> Result<Self> {
        let mut id: *mut rdma_cm_id = std::ptr::null_mut();
        from_ret_errno(unsafe {
            rdma_create_id(
                channel.inner,
                &mut id,
                std::ptr::null_mut(),
                port_space.as_raw(),
            )
        })?;
        Ok(Self {
            inner: id,
            owned: true,
        })
    }

    /// Wrap a raw `rdma_cm_id` pointer (e.g. from a connect request event).
    ///
    /// # Safety
    /// The caller must ensure the pointer is valid and that ownership semantics
    /// are correctly handled.
    pub unsafe fn from_raw(id: *mut rdma_cm_id, owned: bool) -> Self {
        Self { inner: id, owned }
    }

    /// Resolve the destination address.
    pub fn resolve_addr(
        &self,
        src: Option<&SocketAddr>,
        dst: &SocketAddr,
        timeout_ms: i32,
    ) -> Result<()> {
        let (src_ptr, dst_sa) = sockaddr_args(src, dst);
        from_ret_errno(unsafe {
            rdma_resolve_addr(self.inner, src_ptr, dst_sa.as_ptr() as *mut _, timeout_ms)
        })
    }

    /// Resolve the route to the destination.
    pub fn resolve_route(&self, timeout_ms: i32) -> Result<()> {
        from_ret_errno(unsafe { rdma_resolve_route(self.inner, timeout_ms) })
    }

    /// Bind to a local address and start listening.
    pub fn listen(&self, addr: &SocketAddr, backlog: i32) -> Result<()> {
        let sa = to_sockaddr_storage(addr);
        from_ret_errno(unsafe { rdma_bind_addr(self.inner, sa.as_ptr() as *mut _) })?;
        from_ret_errno(unsafe { rdma_listen(self.inner, backlog) })
    }

    /// Create a QP on this CM ID, returning an owned [`CmQueuePair`].
    ///
    /// When send_cq/recv_cq are None, rdma_cm creates internal CQs.
    pub fn create_qp(
        &self,
        pd: &Arc<ProtectionDomain>,
        init_attr: &QpInitAttr,
    ) -> Result<CmQueuePair> {
        self.create_qp_with_cq(pd, init_attr, None, None)
    }

    /// Create a QP on this CM ID with explicit CQs, returning an owned [`CmQueuePair`].
    ///
    /// Use this when you need to control CQ creation (e.g., for completion
    /// channel notification). Pass `None` to let rdma_cm create internal CQs.
    pub fn create_qp_with_cq(
        &self,
        pd: &Arc<ProtectionDomain>,
        init_attr: &QpInitAttr,
        send_cq: Option<&Arc<CompletionQueue>>,
        recv_cq: Option<&Arc<CompletionQueue>>,
    ) -> Result<CmQueuePair> {
        let mut raw_attr = ibv_qp_init_attr {
            send_cq: send_cq.map_or(std::ptr::null_mut(), |cq| cq.inner),
            recv_cq: recv_cq.map_or(std::ptr::null_mut(), |cq| cq.inner),
            cap: ibv_qp_cap {
                max_send_wr: init_attr.max_send_wr,
                max_recv_wr: init_attr.max_recv_wr,
                max_send_sge: init_attr.max_send_sge,
                max_recv_sge: init_attr.max_recv_sge,
                max_inline_data: init_attr.max_inline_data,
            },
            qp_type: init_attr.qp_type.as_raw(),
            sq_sig_all: i32::from(init_attr.sq_sig_all),
            ..Default::default()
        };
        from_ret_errno(unsafe { rdma_create_qp(self.inner, pd.inner, &mut raw_attr) })?;
        Ok(CmQueuePair {
            qp: self.qp_raw(),
            cm_id_raw: self.inner,
            _pd: Arc::clone(pd),
            _send_cq: send_cq.map(Arc::clone),
            _recv_cq: recv_cq.map(Arc::clone),
        })
    }

    /// Connect to a remote peer (client side).
    pub fn connect(&self, param: &ConnParam) -> Result<()> {
        let mut raw = param.to_raw();
        from_ret_errno(unsafe { rdma_connect(self.inner, &mut raw) })
    }

    /// Accept an incoming connection (server side).
    pub fn accept(&self, param: &ConnParam) -> Result<()> {
        let mut raw = param.to_raw();
        from_ret_errno(unsafe { rdma_accept(self.inner, &mut raw) })
    }

    /// Disconnect from the remote peer.
    pub fn disconnect(&self) -> Result<()> {
        from_ret_errno(unsafe { rdma_disconnect(self.inner) })
    }

    /// Get the QP number (if a QP has been created on this CM ID).
    pub fn qp_num(&self) -> Option<u32> {
        let qp = unsafe { (*self.inner).qp };
        if qp.is_null() {
            None
        } else {
            Some(unsafe { (*qp).qp_num })
        }
    }

    /// Get the raw QP pointer (if a QP has been created on this CM ID).
    pub fn qp_raw(&self) -> *mut ibv_qp {
        unsafe { (*self.inner).qp }
    }

    /// Get the ibverbs context associated with this CM ID (set after addr resolution).
    ///
    /// Returns `None` if the address hasn't been resolved yet.
    pub fn verbs_context(&self) -> Option<Arc<Context>> {
        let ctx = unsafe { (*self.inner).verbs };
        if ctx.is_null() {
            None
        } else {
            // rdma_cm owns this context — don't close on drop
            Some(Arc::new(unsafe { Context::from_raw(ctx, false) }))
        }
    }

    /// Allocate a PD from this CM ID's verbs context.
    ///
    /// Convenience for `ProtectionDomain::new(cm_id.verbs_context())`.
    pub fn alloc_pd(&self) -> Result<Arc<ProtectionDomain>> {
        let ctx = self.verbs_context().ok_or(crate::Error::InvalidArg(
            "CM ID has no verbs context (resolve_addr first)".into(),
        ))?;
        ProtectionDomain::new(ctx)
    }

    /// Raw pointer.
    pub fn as_raw(&self) -> *mut rdma_cm_id {
        self.inner
    }

    /// Migrate this CM ID to a different event channel.
    ///
    /// After migration, events for this CM ID will be reported on the new channel.
    /// This is typically used after `accept()` to give the accepted connection
    /// its own event channel, independent of the listener.
    pub fn migrate(&self, new_channel: &EventChannel) -> Result<()> {
        from_ret_errno(unsafe { rdma_migrate_id(self.inner, new_channel.as_raw()) })
    }

    /// Get the peer's socket address (remote end of the connection).
    ///
    /// Returns `None` if the address is not available (e.g., not yet connected).
    pub fn peer_addr(&self) -> Option<SocketAddr> {
        // rdma_get_peer_addr is an inline function: &id->route.addr.dst_addr
        let sa = unsafe { &(*self.inner).route.addr.rdma_addr__anon_1.dst_addr };
        unsafe { sockaddr_to_std(sa as *const _ as *const _) }
    }

    /// Get the local socket address.
    ///
    /// Returns `None` if the address is not available.
    pub fn local_addr(&self) -> Option<SocketAddr> {
        // rdma_get_local_addr is an inline function: &id->route.addr.src_addr
        let sa = unsafe { &(*self.inner).route.addr.rdma_addr__anon_0.src_addr };
        unsafe { sockaddr_to_std(sa as *const _ as *const _) }
    }
}

// ---------------------------------------------------------------------------
// CmQueuePair — safe wrapper for CM-managed QPs
// ---------------------------------------------------------------------------

/// A Queue Pair created via `rdma_create_qp` on a [`CmId`].
///
/// Owns the QP lifecycle. [`Drop`] calls `rdma_destroy_qp`.
/// Captures `Arc` references to PD and CQs to prevent premature destruction
/// of resources the QP depends on.
///
/// # Teardown
///
/// Must be dropped **before** the [`CmId`] that created it (since
/// `rdma_destroy_id` requires the QP to be destroyed first). When used
/// inside [`AsyncQp`], field declaration order guarantees this.
pub struct CmQueuePair {
    qp: *mut ibv_qp,
    cm_id_raw: *mut rdma_cm_id,
    _pd: Arc<ProtectionDomain>,
    _send_cq: Option<Arc<CompletionQueue>>,
    _recv_cq: Option<Arc<CompletionQueue>>,
}

// Safety: ibv_qp is thread-safe (protected by internal locking in libibverbs).
unsafe impl Send for CmQueuePair {}
unsafe impl Sync for CmQueuePair {}

impl Drop for CmQueuePair {
    fn drop(&mut self) {
        // Safety: QP was created by rdma_create_qp on this cm_id.
        // Arc refs to PD/CQs keep them alive until after this returns.
        unsafe { rdma_destroy_qp(self.cm_id_raw) };
    }
}

impl CmQueuePair {
    /// Raw QP pointer for posting work requests.
    pub fn as_raw(&self) -> *mut ibv_qp {
        self.qp
    }

    /// QP number assigned by the HCA.
    pub fn qp_num(&self) -> u32 {
        unsafe { (*self.qp).qp_num }
    }
}

// --- Socket address helpers ---

const AF_INET: u16 = 2;
const AF_INET6: u16 = 10;

/// Convert a `SocketAddr` to a `sockaddr_storage`-sized buffer.
fn to_sockaddr_storage(addr: &SocketAddr) -> SockAddrBuf {
    let mut buf = [0u8; std::mem::size_of::<bnd_linux::libc::posix::socket::sockaddr_storage>()];
    match addr {
        SocketAddr::V4(v4) => {
            let sa = bnd_linux::libc::posix::inet::sockaddr_in {
                sin_family: AF_INET,
                sin_port: v4.port().to_be(),
                sin_addr: bnd_linux::libc::posix::inet::in_addr {
                    s_addr: u32::from_ne_bytes(v4.ip().octets()),
                },
                ..Default::default()
            };
            unsafe {
                std::ptr::copy_nonoverlapping(
                    &sa as *const _ as *const u8,
                    buf.as_mut_ptr(),
                    std::mem::size_of_val(&sa),
                );
            }
        }
        SocketAddr::V6(v6) => {
            let sa = bnd_linux::libc::posix::inet::sockaddr_in6 {
                sin6_family: AF_INET6,
                sin6_port: v6.port().to_be(),
                sin6_flowinfo: v6.flowinfo(),
                sin6_addr: bnd_linux::libc::posix::inet::in6_addr {
                    __in6_u: bnd_linux::libc::posix::inet::in6_addr___in6_u {
                        __u6_addr8: v6.ip().octets(),
                    },
                },
                sin6_scope_id: v6.scope_id(),
            };
            unsafe {
                std::ptr::copy_nonoverlapping(
                    &sa as *const _ as *const u8,
                    buf.as_mut_ptr(),
                    std::mem::size_of_val(&sa),
                );
            }
        }
    }
    SockAddrBuf(buf)
}

/// Stack-allocated sockaddr buffer.
struct SockAddrBuf([u8; std::mem::size_of::<bnd_linux::libc::posix::socket::sockaddr_storage>()]);

impl SockAddrBuf {
    fn as_ptr(&self) -> *const bnd_linux::libc::posix::socket::sockaddr {
        self.0.as_ptr().cast()
    }
}

fn sockaddr_args(
    src: Option<&SocketAddr>,
    dst: &SocketAddr,
) -> (*mut bnd_linux::libc::posix::socket::sockaddr, SockAddrBuf) {
    let dst_sa = to_sockaddr_storage(dst);
    let src_ptr = match src {
        // For simplicity, pass null for src (let the kernel choose).
        Some(_) => std::ptr::null_mut(),
        None => std::ptr::null_mut(),
    };
    (src_ptr, dst_sa)
}

/// Convert a raw `sockaddr*` to a `std::net::SocketAddr`.
///
/// # Safety
/// The pointer must be valid and point to a `sockaddr_in` or `sockaddr_in6`.
unsafe fn sockaddr_to_std(
    sa: *const bnd_linux::libc::posix::socket::sockaddr,
) -> Option<SocketAddr> {
    unsafe {
        let family = (*sa).sa_family;
        if family == AF_INET {
            let sin = &*(sa as *const bnd_linux::libc::posix::inet::sockaddr_in);
            let ip = std::net::Ipv4Addr::from(u32::from_be(sin.sin_addr.s_addr));
            let port = u16::from_be(sin.sin_port);
            Some(SocketAddr::V4(std::net::SocketAddrV4::new(ip, port)))
        } else if family == AF_INET6 {
            let sin6 = &*(sa as *const bnd_linux::libc::posix::inet::sockaddr_in6);
            let ip = std::net::Ipv6Addr::from(sin6.sin6_addr.__in6_u.__u6_addr8);
            let port = u16::from_be(sin6.sin6_port);
            Some(SocketAddr::V6(std::net::SocketAddrV6::new(
                ip,
                port,
                sin6.sin6_flowinfo,
                sin6.sin6_scope_id,
            )))
        } else {
            None
        }
    }
}