Skip to content

Commit

Permalink
Don't panic when setting invalid attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
YtvwlD committed Jul 27, 2024
1 parent fbdca10 commit de2a20c
Showing 1 changed file with 31 additions and 41 deletions.
72 changes: 31 additions & 41 deletions ibverbs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,29 +634,27 @@ impl<'res> QueuePairBuilder<'res> {
///
/// Defaults to `IBV_ACCESS_LOCAL_WRITE`.
pub fn set_access(&mut self, access: ffi::ibv_access_flags) -> &mut Self {
if self.qp_type != ffi::ibv_qp_type::IBV_QPT_RC
&& self.qp_type != ffi::ibv_qp_type::IBV_QPT_UC
if self.qp_type == ffi::ibv_qp_type::IBV_QPT_RC
|| self.qp_type == ffi::ibv_qp_type::IBV_QPT_UC
{
panic!("Setting the access flags is only possible on RC and UC Queue Pairs.");
self.access = Some(access);
}
self.access = Some(access);
self
}

/// Set the access flags of the new `QueuePair` such that it allows remote reads and writes.
///
/// Valid only for RC and UC QPs.
pub fn allow_remote_rw(&mut self) -> &mut Self {
if self.qp_type != ffi::ibv_qp_type::IBV_QPT_RC
&& self.qp_type != ffi::ibv_qp_type::IBV_QPT_UC
if self.qp_type == ffi::ibv_qp_type::IBV_QPT_RC
|| self.qp_type == ffi::ibv_qp_type::IBV_QPT_UC
{
panic!("Setting the access flags is only possible on RC and UC Queue Pairs.");
self.access = Some(
self.access.unwrap()
| ffi::ibv_access_flags::IBV_ACCESS_REMOTE_WRITE
| ffi::ibv_access_flags::IBV_ACCESS_REMOTE_READ,
);
}
self.access = Some(
self.access.unwrap()
| ffi::ibv_access_flags::IBV_ACCESS_REMOTE_WRITE
| ffi::ibv_access_flags::IBV_ACCESS_REMOTE_READ,
);
self
}

Expand Down Expand Up @@ -703,10 +701,9 @@ impl<'res> QueuePairBuilder<'res> {
/// - 30 - 327.68 ms delay
/// - 31 - 491.52 ms delay
pub fn set_min_rnr_timer(&mut self, timer: u8) -> &mut Self {
if self.qp_type != ffi::ibv_qp_type::IBV_QPT_RC {
panic!("Setting the RNR timer value is only possible on RC Queue Pairs.");
if self.qp_type == ffi::ibv_qp_type::IBV_QPT_RC {
self.min_rnr_timer = Some(timer);
}
self.min_rnr_timer = Some(timer);
self
}

Expand Down Expand Up @@ -755,10 +752,9 @@ impl<'res> QueuePairBuilder<'res> {
/// - 30 - 4400 s
/// - 31 - 8800 s
pub fn set_timeout(&mut self, timeout: u8) -> &mut Self {
if self.qp_type != ffi::ibv_qp_type::IBV_QPT_RC {
panic!("Setting the timeout is only possible on RC Queue Pairs.");
if self.qp_type == ffi::ibv_qp_type::IBV_QPT_RC {
self.timeout = Some(timeout);
}
self.timeout = Some(timeout);
self
}

Expand All @@ -772,11 +768,10 @@ impl<'res> QueuePairBuilder<'res> {
///
/// Panics if a count higher than 7 is given.
pub fn set_retry_count(&mut self, count: u8) -> &mut Self {
if self.qp_type != ffi::ibv_qp_type::IBV_QPT_RC {
panic!("Setting the retry count is only possible on RC Queue Pairs.");
if self.qp_type == ffi::ibv_qp_type::IBV_QPT_RC {
assert!(count <= 7);
self.retry_count = Some(count);
}
assert!(count <= 7);
self.retry_count = Some(count);
self
}

Expand All @@ -791,11 +786,10 @@ impl<'res> QueuePairBuilder<'res> {
///
/// Panics if a limit higher than 7 is given.
pub fn set_rnr_retry(&mut self, n: u8) -> &mut Self {
if self.qp_type != ffi::ibv_qp_type::IBV_QPT_RC {
panic!("Setting the RNR retry count is only possible on RC Queue Pairs.");
if self.qp_type == ffi::ibv_qp_type::IBV_QPT_RC {
assert!(n <= 7);
self.rnr_retry = Some(n);
}
assert!(n <= 7);
self.rnr_retry = Some(n);
self
}

Expand All @@ -804,21 +798,19 @@ impl<'res> QueuePairBuilder<'res> {
/// This defaults to 1.
/// Valid only for RC QPs.
pub fn set_max_rd_atomic(&mut self, max_rd_atomic: u8) -> &mut Self {
if self.qp_type != ffi::ibv_qp_type::IBV_QPT_RC {
panic!("Setting the number of outstanding RDMA reads & atomic operations is only possible on RC Queue Pairs.");
if self.qp_type == ffi::ibv_qp_type::IBV_QPT_RC {
self.max_rd_atomic = Some(max_rd_atomic);
}
self.max_rd_atomic = Some(max_rd_atomic);
self
}

/// Set the number of responder resources for handling incoming RDMA reads & atomic operations.
///
/// This defaults to 1.
pub fn set_max_dest_rd_atomic(&mut self, max_dest_rd_atomic: u8) -> &mut Self {
if self.qp_type != ffi::ibv_qp_type::IBV_QPT_RC {
panic!("Setting the responder resources for handling incoming RDMA reads & atomic operations is only possible on RC Queue Pairs.");
if self.qp_type == ffi::ibv_qp_type::IBV_QPT_RC {
self.max_dest_rd_atomic = Some(max_dest_rd_atomic);
}
self.max_dest_rd_atomic = Some(max_dest_rd_atomic);
self
}

Expand All @@ -832,26 +824,24 @@ impl<'res> QueuePairBuilder<'res> {
/// - 4: 2048
/// - 5: 4096
pub fn set_path_mtu(&mut self, path_mtu: u32) -> &mut Self {
if self.qp_type != ffi::ibv_qp_type::IBV_QPT_RC
&& self.qp_type != ffi::ibv_qp_type::IBV_QPT_UC
if self.qp_type == ffi::ibv_qp_type::IBV_QPT_RC
|| self.qp_type == ffi::ibv_qp_type::IBV_QPT_UC
{
panic!("Setting the path MTU is only possible on RC and UC Queue Pairs.");
assert!((1..=5).contains(&path_mtu));
self.path_mtu = Some(path_mtu);
}
assert!((1..=5).contains(&path_mtu));
self.path_mtu = Some(path_mtu);
self
}

/// Set the PSN for the receive queue.
///
/// Defaults to 0.
pub fn set_rq_psn(&mut self, rq_psn: u32) -> &mut Self {
if self.qp_type != ffi::ibv_qp_type::IBV_QPT_RC
&& self.qp_type != ffi::ibv_qp_type::IBV_QPT_UC
if self.qp_type == ffi::ibv_qp_type::IBV_QPT_RC
|| self.qp_type == ffi::ibv_qp_type::IBV_QPT_UC
{
panic!("Setting the receive queue's PSN is only possible on RC and UC Queue Pairs.");
self.rq_psn = Some(rq_psn);
}
self.rq_psn = Some(rq_psn);
self
}

Expand Down

0 comments on commit de2a20c

Please sign in to comment.