Skip to content

Commit 7c4320a

Browse files
committed
feat(rdmacm): support setting private data in rdmacm conn param
Signed-off-by: Luke Yue <lukedyue@gmail.com>
1 parent 73ea8e1 commit 7c4320a

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

src/rdmacm/communication_manager.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,42 @@ impl Event {
517517
unsafe { self.event.as_ref().status }
518518
}
519519

520+
/// Get the private data sent by the remote peer.
521+
///
522+
/// This is typically available for [`EventType::ConnectRequest`] and
523+
/// [`EventType::ConnectResponse`] events, where the remote peer may have
524+
/// sent private data as part of the connection setup.
525+
///
526+
/// # Returns
527+
/// - `Some(&[u8])` - The private data slice if any was provided
528+
/// - `None` - If no private data was sent or the length is 0
529+
///
530+
/// # Example
531+
/// ```ignore
532+
/// match event.event_type() {
533+
/// EventType::ConnectRequest => {
534+
/// if let Some(data) = event.get_private_data() {
535+
/// println!("Received {} bytes of private data", data.len());
536+
/// }
537+
/// }
538+
/// _ => {}
539+
/// }
540+
/// ```
541+
pub fn get_private_data(&self) -> Option<&[u8]> {
542+
unsafe {
543+
let param = &self.event.as_ref().param.conn;
544+
let len = param.private_data_len as usize;
545+
if len == 0 || param.private_data.is_null() {
546+
None
547+
} else {
548+
Some(std::slice::from_raw_parts(
549+
param.private_data as *const u8,
550+
len,
551+
))
552+
}
553+
}
554+
}
555+
520556
/// Acknowledge and free the communication event.
521557
///
522558
/// # Note
@@ -1002,6 +1038,61 @@ impl ConnectionParameter {
10021038
self.0.qp_num = qp_number;
10031039
self
10041040
}
1041+
1042+
/// Setup the private data to be sent with connect or accept.
1043+
///
1044+
/// # Arguments
1045+
/// * `data` - The private data slice. Maximum 56 bytes for RC/UC connections,
1046+
/// 180 bytes for UD. Data exceeding the limit will be truncated.
1047+
///
1048+
/// # Safety
1049+
/// The caller must ensure the data slice remains valid until the connect/accept
1050+
/// operation completes.
1051+
///
1052+
/// # Example
1053+
/// ```ignore
1054+
/// let my_data = [1u8, 2, 3, 4];
1055+
/// param.setup_private_data(&my_data);
1056+
/// id.connect(param)?;
1057+
/// ```
1058+
pub fn setup_private_data(&mut self, data: &[u8]) -> &mut Self {
1059+
// Maximum private_data for RC/UC is 56 bytes, for UD is 180 bytes
1060+
// We use 56 as the safe limit for RC connections
1061+
let len = data.len().min(56);
1062+
self.0.private_data = data.as_ptr() as *const _;
1063+
self.0.private_data_len = len as u8;
1064+
self
1065+
}
1066+
1067+
/// Setup responder resources for the connection.
1068+
/// This is the maximum number of outstanding RDMA read/atomic operations
1069+
/// the local side will accept from the remote side.
1070+
pub fn setup_responder_resources(&mut self, resources: u8) -> &mut Self {
1071+
self.0.responder_resources = resources;
1072+
self
1073+
}
1074+
1075+
/// Setup initiator depth for the connection.
1076+
/// This is the maximum number of outstanding RDMA read/atomic operations
1077+
/// that the local side will have pending to the remote side.
1078+
pub fn setup_initiator_depth(&mut self, depth: u8) -> &mut Self {
1079+
self.0.initiator_depth = depth;
1080+
self
1081+
}
1082+
1083+
/// Setup retry count for the connection.
1084+
/// The number of times to retry a connection request or response.
1085+
pub fn setup_retry_count(&mut self, count: u8) -> &mut Self {
1086+
self.0.retry_count = count;
1087+
self
1088+
}
1089+
1090+
/// Setup RNR retry count for the connection.
1091+
/// The number of times to retry a receiver-not-ready error.
1092+
pub fn setup_rnr_retry_count(&mut self, count: u8) -> &mut Self {
1093+
self.0.rnr_retry_count = count;
1094+
self
1095+
}
10051096
}
10061097

10071098
#[cfg(test)]

0 commit comments

Comments
 (0)