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
/// Local Memory Region
pub(crate) mod local;
/// Raw Memory Region
mod raw;
/// Remote Memory Region
pub(crate) mod remote;
use derive_builder::Builder;
use enumflags2::BitFlags;
pub(crate) use raw::RawMemoryRegion;
use rdma_sys::ibv_access_flags;
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, ops::Add, time::SystemTime};

use crate::{
    access::{flags_into_ibv_access, ibv_access_into_flags},
    rmr_manager::DEFAULT_RMR_TIMEOUT,
    AccessFlag,
};

/// Rdma Memory Region Access
pub trait MrAccess: Sync + Send + Debug + IbvAccess {
    /// Get the start addr
    fn addr(&self) -> usize;

    /// Get the length
    fn length(&self) -> usize;

    /// Get the remote key
    fn rkey(&self) -> u32;

    /// Get the `BitFlags<AccessFlag>` of mr
    #[inline]
    fn access(&self) -> BitFlags<AccessFlag> {
        ibv_access_into_flags(self.ibv_access())
    }
}

/// Get `ibv_access_flags` of mr
pub trait IbvAccess {
    /// Get `ibv_access_flags` of mr
    fn ibv_access(&self) -> ibv_access_flags;
}

/// Memory region token used for the remote access
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug, Builder)]
pub struct MrToken {
    /// The start address of this memory region
    pub addr: usize,
    /// The length of this memory region
    pub len: usize,
    /// The remote key of this memory region
    pub rkey: u32,
    /// The Deadline of this memory region.
    /// After ddl, this memory region maybe unavailable due to timeout.
    #[builder(default = "SystemTime::now().add(DEFAULT_RMR_TIMEOUT)")]
    pub ddl: SystemTime,
    /// Remote mr `ibv_access_flags` inner
    #[builder(field(
        type = "BitFlags<AccessFlag>",
        build = "flags_into_ibv_access(self.access).0"
    ))]
    pub access: u32,
}