rdmaxcel_sys/
lib.rs

1/*
2 * Portions Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9// sections of code adapted from https://github.com/jonhoo/rust-ibverbs
10// Copyright (c) 2016 Jon Gjengset under MIT License (MIT)
11
12mod inner {
13    #![allow(non_upper_case_globals)]
14    #![allow(non_camel_case_types)]
15    #![allow(non_snake_case)]
16    #![allow(unused_attributes)]
17    #[cfg(not(cargo))]
18    use crate::ibv_wc_flags;
19    #[cfg(not(cargo))]
20    use crate::ibv_wc_opcode;
21    #[cfg(not(cargo))]
22    use crate::ibv_wc_status;
23    #[cfg(cargo)]
24    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
25
26    #[repr(C, packed(1))]
27    #[derive(Debug, Default, Clone, Copy)]
28    pub struct mlx5_wqe_ctrl_seg {
29        pub opmod_idx_opcode: u32,
30        pub qpn_ds: u32,
31        pub signature: u8,
32        pub dci_stream_channel_id: u16,
33        pub fm_ce_se: u8,
34        pub imm: u32,
35    }
36
37    #[repr(C)]
38    #[derive(Debug, Copy, Clone)]
39    pub struct ibv_wc {
40        wr_id: u64,
41        status: ibv_wc_status::Type,
42        opcode: ibv_wc_opcode::Type,
43        vendor_err: u32,
44        byte_len: u32,
45
46        /// Immediate data OR the local RKey that was invalidated depending on `wc_flags`.
47        /// See `man ibv_poll_cq` for details.
48        pub imm_data: u32,
49        /// Local QP number of completed WR.
50        ///
51        /// Relevant for Receive Work Completions that are associated with an SRQ.
52        pub qp_num: u32,
53        /// Source QP number (remote QP number) of completed WR.
54        ///
55        /// Relevant for Receive Work Completions of a UD QP.
56        pub src_qp: u32,
57        /// Flags of the Work Completion. It is either 0 or the bitwise OR of one or more of the
58        /// following flags:
59        ///
60        ///  - `IBV_WC_GRH`: Indicator that GRH is present for a Receive Work Completions of a UD QP.
61        ///    If this bit is set, the first 40 bytes of the buffered that were referred to in the
62        ///    Receive request will contain the GRH of the incoming message. If this bit is cleared,
63        ///    the content of those first 40 bytes is undefined
64        ///  - `IBV_WC_WITH_IMM`: Indicator that imm_data is valid. Relevant for Receive Work
65        ///    Completions
66        pub wc_flags: ibv_wc_flags,
67        /// P_Key index (valid only for GSI QPs).
68        pub pkey_index: u16,
69        /// Source LID (the base LID that this message was sent from).
70        ///
71        /// Relevant for Receive Work Completions of a UD QP.
72        pub slid: u16,
73        /// Service Level (the SL LID that this message was sent with).
74        ///
75        /// Relevant for Receive Work Completions of a UD QP.
76        pub sl: u8,
77        /// Destination LID path bits.
78        ///
79        /// Relevant for Receive Work Completions of a UD QP (not applicable for multicast messages).
80        pub dlid_path_bits: u8,
81    }
82
83    #[allow(clippy::len_without_is_empty)]
84    impl ibv_wc {
85        /// Returns the 64 bit value that was associated with the corresponding Work Request.
86        pub fn wr_id(&self) -> u64 {
87            self.wr_id
88        }
89
90        /// Returns the number of bytes transferred.
91        ///
92        /// Relevant if the Receive Queue for incoming Send or RDMA Write with immediate operations.
93        /// This value doesn't include the length of the immediate data, if such exists. Relevant in
94        /// the Send Queue for RDMA Read and Atomic operations.
95        ///
96        /// For the Receive Queue of a UD QP that is not associated with an SRQ or for an SRQ that is
97        /// associated with a UD QP this value equals to the payload of the message plus the 40 bytes
98        /// reserved for the GRH. The number of bytes transferred is the payload of the message plus
99        /// the 40 bytes reserved for the GRH, whether or not the GRH is present
100        pub fn len(&self) -> usize {
101            self.byte_len as usize
102        }
103
104        /// Check if this work requested completed successfully.
105        ///
106        /// A successful work completion (`IBV_WC_SUCCESS`) means that the corresponding Work Request
107        /// (and all of the unsignaled Work Requests that were posted previous to it) ended, and the
108        /// memory buffers that this Work Request refers to are ready to be (re)used.
109        pub fn is_valid(&self) -> bool {
110            self.status == ibv_wc_status::IBV_WC_SUCCESS
111        }
112
113        /// Returns the work completion status and vendor error syndrome (`vendor_err`) if the work
114        /// request did not completed successfully.
115        ///
116        /// Possible statuses include:
117        ///
118        ///  - `IBV_WC_LOC_LEN_ERR`: Local Length Error: this happens if a Work Request that was posted
119        ///    in a local Send Queue contains a message that is greater than the maximum message size
120        ///    that is supported by the RDMA device port that should send the message or an Atomic
121        ///    operation which its size is different than 8 bytes was sent. This also may happen if a
122        ///    Work Request that was posted in a local Receive Queue isn't big enough for holding the
123        ///    incoming message or if the incoming message size if greater the maximum message size
124        ///    supported by the RDMA device port that received the message.
125        ///  - `IBV_WC_LOC_QP_OP_ERR`: Local QP Operation Error: an internal QP consistency error was
126        ///    detected while processing this Work Request: this happens if a Work Request that was
127        ///    posted in a local Send Queue of a UD QP contains an Address Handle that is associated
128        ///    with a Protection Domain to a QP which is associated with a different Protection Domain
129        ///    or an opcode which isn't supported by the transport type of the QP isn't supported (for
130        ///    example:
131        ///    RDMA Write over a UD QP).
132        ///  - `IBV_WC_LOC_EEC_OP_ERR`: Local EE Context Operation Error: an internal EE Context
133        ///    consistency error was detected while processing this Work Request (unused, since its
134        ///    relevant only to RD QPs or EE Context, which aren’t supported).
135        ///  - `IBV_WC_LOC_PROT_ERR`: Local Protection Error: the locally posted Work Request’s buffers
136        ///    in the scatter/gather list does not reference a Memory Region that is valid for the
137        ///    requested operation.
138        ///  - `IBV_WC_WR_FLUSH_ERR`: Work Request Flushed Error: A Work Request was in process or
139        ///    outstanding when the QP transitioned into the Error State.
140        ///  - `IBV_WC_MW_BIND_ERR`: Memory Window Binding Error: A failure happened when tried to bind
141        ///    a MW to a MR.
142        ///  - `IBV_WC_BAD_RESP_ERR`: Bad Response Error: an unexpected transport layer opcode was
143        ///    returned by the responder. Relevant for RC QPs.
144        ///  - `IBV_WC_LOC_ACCESS_ERR`: Local Access Error: a protection error occurred on a local data
145        ///    buffer during the processing of a RDMA Write with Immediate operation sent from the
146        ///    remote node. Relevant for RC QPs.
147        ///  - `IBV_WC_REM_INV_REQ_ERR`: Remote Invalid Request Error: The responder detected an
148        ///    invalid message on the channel. Possible causes include the operation is not supported
149        ///    by this receive queue (qp_access_flags in remote QP wasn't configured to support this
150        ///    operation), insufficient buffering to receive a new RDMA or Atomic Operation request, or
151        ///    the length specified in a RDMA request is greater than 2^{31} bytes. Relevant for RC
152        ///    QPs.
153        ///  - `IBV_WC_REM_ACCESS_ERR`: Remote Access Error: a protection error occurred on a remote
154        ///    data buffer to be read by an RDMA Read, written by an RDMA Write or accessed by an
155        ///    atomic operation. This error is reported only on RDMA operations or atomic operations.
156        ///    Relevant for RC QPs.
157        ///  - `IBV_WC_REM_OP_ERR`: Remote Operation Error: the operation could not be completed
158        ///    successfully by the responder. Possible causes include a responder QP related error that
159        ///    prevented the responder from completing the request or a malformed WQE on the Receive
160        ///    Queue. Relevant for RC QPs.
161        ///  - `IBV_WC_RETRY_EXC_ERR`: Transport Retry Counter Exceeded: The local transport timeout
162        ///    retry counter was exceeded while trying to send this message. This means that the remote
163        ///    side didn't send any Ack or Nack. If this happens when sending the first message,
164        ///    usually this mean that the connection attributes are wrong or the remote side isn't in a
165        ///    state that it can respond to messages. If this happens after sending the first message,
166        ///    usually it means that the remote QP isn't available anymore. Relevant for RC QPs.
167        ///  - `IBV_WC_RNR_RETRY_EXC_ERR`: RNR Retry Counter Exceeded: The RNR NAK retry count was
168        ///    exceeded. This usually means that the remote side didn't post any WR to its Receive
169        ///    Queue. Relevant for RC QPs.
170        ///  - `IBV_WC_LOC_RDD_VIOL_ERR`: Local RDD Violation Error: The RDD associated with the QP
171        ///    does not match the RDD associated with the EE Context (unused, since its relevant only
172        ///    to RD QPs or EE Context, which aren't supported).
173        ///  - `IBV_WC_REM_INV_RD_REQ_ERR`: Remote Invalid RD Request: The responder detected an
174        ///    invalid incoming RD message. Causes include a Q_Key or RDD violation (unused, since its
175        ///    relevant only to RD QPs or EE Context, which aren't supported)
176        ///  - `IBV_WC_REM_ABORT_ERR`: Remote Aborted Error: For UD or UC QPs associated with a SRQ,
177        ///    the responder aborted the operation.
178        ///  - `IBV_WC_INV_EECN_ERR`: Invalid EE Context Number: An invalid EE Context number was
179        ///    detected (unused, since its relevant only to RD QPs or EE Context, which aren't
180        ///    supported).
181        ///  - `IBV_WC_INV_EEC_STATE_ERR`: Invalid EE Context State Error: Operation is not legal for
182        ///    the specified EE Context state (unused, since its relevant only to RD QPs or EE Context,
183        ///    which aren't supported).
184        ///  - `IBV_WC_FATAL_ERR`: Fatal Error.
185        ///  - `IBV_WC_RESP_TIMEOUT_ERR`: Response Timeout Error.
186        ///  - `IBV_WC_GENERAL_ERR`: General Error: other error which isn't one of the above errors.
187        pub fn error(&self) -> Option<(ibv_wc_status::Type, u32)> {
188            match self.status {
189                ibv_wc_status::IBV_WC_SUCCESS => None,
190                status => Some((status, self.vendor_err)),
191            }
192        }
193
194        /// Returns the operation that the corresponding Work Request performed.
195        ///
196        /// This value controls the way that data was sent, the direction of the data flow and the
197        /// valid attributes in the Work Completion.
198        pub fn opcode(&self) -> ibv_wc_opcode::Type {
199            self.opcode
200        }
201
202        /// Returns a 32 bits number, in network order, in an SEND or RDMA WRITE opcodes that is being
203        /// sent along with the payload to the remote side and placed in a Receive Work Completion and
204        /// not in a remote memory buffer
205        ///
206        /// Note that IMM is only returned if `IBV_WC_WITH_IMM` is set in `wc_flags`. If this is not
207        /// the case, no immediate value was provided, and `imm_data` should be interpreted
208        /// differently. See `man ibv_poll_cq` for details.
209        pub fn imm_data(&self) -> Option<u32> {
210            if self.is_valid() && ((self.wc_flags & ibv_wc_flags::IBV_WC_WITH_IMM).0 != 0) {
211                Some(self.imm_data)
212            } else {
213                None
214            }
215        }
216    }
217
218    impl Default for ibv_wc {
219        fn default() -> Self {
220            ibv_wc {
221                wr_id: 0,
222                status: ibv_wc_status::IBV_WC_GENERAL_ERR,
223                opcode: ibv_wc_opcode::IBV_WC_LOCAL_INV,
224                vendor_err: 0,
225                byte_len: 0,
226                imm_data: 0,
227                qp_num: 0,
228                src_qp: 0,
229                wc_flags: ibv_wc_flags(0),
230                pkey_index: 0,
231                slid: 0,
232                sl: 0,
233                dlid_path_bits: 0,
234            }
235        }
236    }
237}
238
239pub use inner::*;
240
241// RDMA error string function and CUDA utility functions
242unsafe extern "C" {
243    pub fn rdmaxcel_error_string(error_code: std::os::raw::c_int) -> *const std::os::raw::c_char;
244    pub fn get_cuda_pci_address_from_ptr(
245        cuda_ptr: u64,
246        pci_addr_out: *mut std::os::raw::c_char,
247        pci_addr_size: usize,
248    ) -> std::os::raw::c_int;
249
250    /// Debug: Print comprehensive device attributes
251    pub fn rdmaxcel_print_device_info(context: *mut ibv_context);
252}