1010#define FLAGCX_IB_COMMON_H_
1111
1212#include "flagcx_net.h"
13+ #include "net.h"
1314#include "ibvcore.h"
1415#include <pthread.h>
1516#include <stdint.h>
1617
17- // Common constants for IB adaptors
1818#define MAXNAMESIZE 64
1919#define MAX_IB_DEVS 32
2020#define FLAGCX_IB_MAX_DEVS_PER_NIC 2
2323 (MAXNAMESIZE * FLAGCX_IB_MAX_DEVS_PER_NIC) + FLAGCX_IB_MAX_DEVS_PER_NIC
2424#define MAX_IB_VDEVS MAX_IB_DEVS * 8
2525
26- // Common enums and constants
26+ #define ENABLE_TIMER 0
27+ #define FLAGCX_IB_MAX_QPS 128
28+ #define FLAGCX_NET_IB_MAX_RECVS 8
29+ #define MAX_REQUESTS (FLAGCX_NET_MAX_REQUESTS * FLAGCX_NET_IB_MAX_RECVS)
30+
2731enum flagcxIbProvider {
2832 IB_PROVIDER_NONE = 0 ,
2933 IB_PROVIDER_MLX5 = 1 ,
@@ -33,12 +37,10 @@ enum flagcxIbProvider {
3337static const char * ibProviderName []
3438 __attribute__((unused )) = {"NONE" , "MLX5" , "MLX4" };
3539
36- // Common parameter function declarations
3740extern int64_t flagcxParamIbMergeVfs (void );
3841extern int64_t flagcxParamIbAdaptiveRouting (void );
3942extern int64_t flagcxParamIbMergeNics (void );
4043
41- // Common IB structures
4244struct flagcxIbMr {
4345 uintptr_t addr ;
4446 size_t pages ;
@@ -55,9 +57,7 @@ struct flagcxIbStats {
5557 int fatalErrorCount ;
5658};
5759
58- // Common device properties structure is now defined in flagcx_net.h
5960
60- // Unified IB device structure (combines features from both adaptors)
6161struct flagcxIbDev {
6262 pthread_mutex_t lock ;
6363 int device ;
@@ -86,30 +86,21 @@ struct flagcxIbDev {
8686 int dmaBufSupported ;
8787} __attribute__((aligned (64 )));
8888
89- // Unified merged device structure (used by both IBRC and UCX)
90- // Contains both direct fields (for IBRC) and structured fields (for UCX)
9189struct flagcxIbMergedDev {
92- // Direct fields (used by IBRC)
9390 int ndevs ;
9491 int devs [FLAGCX_IB_MAX_DEVS_PER_NIC ];
95-
96- // Structured fields (used by UCX) - now uses flagcx_net.h definition
9792 flagcxNetVDeviceProps_t vProps ;
98-
99- // Common fields
10093 int speed ;
10194 char devName [MAX_MERGED_DEV_NAME ];
10295} __attribute__((aligned (64 )));
10396
104- // Common QP info structure (used by both IBRC and IBUC)
10597struct flagcxIbQpInfo {
10698 uint32_t qpn ;
10799 struct ibv_ece ece ;
108100 int ece_supported ;
109101 int devIndex ;
110102};
111103
112- // Common device info structure (used by both IBRC and IBUC)
113104struct flagcxIbDevInfo {
114105 uint32_t lid ;
115106 uint8_t ib_port ;
@@ -121,29 +112,232 @@ struct flagcxIbDevInfo {
121112 union ibv_gid remoteGid ;
122113};
123114
124- // Common GID info structure (used by both IBRC and IBUC)
125115struct flagcxIbGidInfo {
126116 uint8_t link_layer ;
127117 union ibv_gid localGid ;
128118 int32_t localGidIndex ;
129119};
130120
131- // Common MR handle structure (used by both IBRC and IBUC)
132121struct flagcxIbMrHandle {
133122 ibv_mr * mrs [FLAGCX_IB_MAX_DEVS_PER_NIC ];
134123};
135124
136- // Common request type constants (used by both IBRC and IBUC)
137125#define FLAGCX_NET_IB_REQ_UNUSED 0
138126#define FLAGCX_NET_IB_REQ_SEND 1
139127#define FLAGCX_NET_IB_REQ_RECV 2
140128#define FLAGCX_NET_IB_REQ_FLUSH 3
141129
142- // Common request type string array (used by both IBRC and IBUC)
143130extern const char * reqTypeStr [];
144131
132+ struct flagcxIbQp {
133+ struct ibv_qp * qp ;
134+ int devIndex ;
135+ int remDevIdx ;
136+ };
137+
138+ struct flagcxIbSendFifo {
139+ uint64_t addr ;
140+ size_t size ;
141+ uint32_t rkeys [FLAGCX_IB_MAX_DEVS_PER_NIC ];
142+ uint32_t nreqs ;
143+ uint32_t tag ;
144+ uint64_t idx ;
145+ char padding [24 ];
146+ };
147+
148+ struct flagcxIbRequest {
149+ struct flagcxIbNetCommBase * base ;
150+ int type ;
151+ struct flagcxSocket * sock ;
152+ int events [FLAGCX_IB_MAX_DEVS_PER_NIC ];
153+ struct flagcxIbNetCommDevBase * devBases [FLAGCX_IB_MAX_DEVS_PER_NIC ];
154+ int nreqs ;
155+ union {
156+ struct {
157+ int size ;
158+ void * data ;
159+ uint32_t lkeys [FLAGCX_IB_MAX_DEVS_PER_NIC ];
160+ int offset ;
161+ } send ;
162+ struct {
163+ int * sizes ;
164+ } recv ;
165+ };
166+ };
167+
168+ struct flagcxIbListenComm {
169+ int dev ;
170+ struct flagcxSocket sock ;
171+ struct flagcxIbCommStage stage ;
172+ };
173+
174+ struct flagcxIbConnectionMetadata {
175+ struct flagcxIbQpInfo qpInfo [FLAGCX_IB_MAX_QPS ];
176+ struct flagcxIbDevInfo devs [FLAGCX_IB_MAX_DEVS_PER_NIC ];
177+ char devName [MAX_MERGED_DEV_NAME ];
178+ uint64_t fifoAddr ;
179+ int ndevs ;
180+ };
181+
182+ struct flagcxIbNetCommDevBase {
183+ int ibDevN ;
184+ struct ibv_pd * pd ;
185+ struct ibv_cq * cq ;
186+ uint64_t pad [2 ];
187+ struct flagcxIbGidInfo gidInfo ;
188+ };
189+
190+ struct flagcxIbRemSizesFifo {
191+ int elems [MAX_REQUESTS ][FLAGCX_NET_IB_MAX_RECVS ];
192+ uint64_t fifoTail ;
193+ uint64_t addr ;
194+ uint32_t rkeys [FLAGCX_IB_MAX_DEVS_PER_NIC ];
195+ uint32_t flags ;
196+ struct ibv_mr * mrs [FLAGCX_IB_MAX_DEVS_PER_NIC ];
197+ struct ibv_sge sge ;
198+ };
199+
200+ struct flagcxIbSendCommDev {
201+ struct flagcxIbNetCommDevBase base ;
202+ struct ibv_mr * fifoMr ;
203+ };
204+
205+ struct alignas (32 ) flagcxIbNetCommBase {
206+ int ndevs ;
207+ bool isSend ;
208+ struct flagcxIbRequest reqs [MAX_REQUESTS ];
209+ struct flagcxIbQp qps [FLAGCX_IB_MAX_QPS ];
210+ int nqps ;
211+ int qpIndex ;
212+ int devIndex ;
213+ struct flagcxSocket sock ;
214+ int ready ;
215+ // Track necessary remDevInfo here
216+ int nRemDevs ;
217+ struct flagcxIbDevInfo remDevs [FLAGCX_IB_MAX_DEVS_PER_NIC ];
218+ };
219+
220+ struct flagcxIbSendComm {
221+ struct flagcxIbNetCommBase base ;
222+ struct flagcxIbSendFifo fifo [MAX_REQUESTS ][FLAGCX_NET_IB_MAX_RECVS ];
223+ // Each dev correlates to a mergedIbDev
224+ struct flagcxIbSendCommDev devs [FLAGCX_IB_MAX_DEVS_PER_NIC ];
225+ struct flagcxIbRequest * fifoReqs [MAX_REQUESTS ][FLAGCX_NET_IB_MAX_RECVS ];
226+ struct ibv_sge sges [FLAGCX_NET_IB_MAX_RECVS ];
227+ struct ibv_send_wr wrs [FLAGCX_NET_IB_MAX_RECVS + 1 ];
228+ struct flagcxIbRemSizesFifo remSizesFifo ;
229+ uint64_t fifoHead ;
230+ int ar ; // Use adaptive routing when all merged devices have it enabled
231+ };
232+
233+ struct flagcxIbGpuFlush {
234+ struct ibv_mr * hostMr ;
235+ struct ibv_sge sge ;
236+ struct flagcxIbQp qp ;
237+ };
238+
239+ struct alignas (32 ) flagcxIbRemFifo {
240+ struct flagcxIbSendFifo elems [MAX_REQUESTS ][FLAGCX_NET_IB_MAX_RECVS ];
241+ uint64_t fifoTail ;
242+ uint64_t addr ;
243+ uint32_t flags ;
244+ };
245+
246+ struct alignas (16 ) flagcxIbRecvCommDev {
247+ struct flagcxIbNetCommDevBase base ;
248+ struct flagcxIbGpuFlush gpuFlush ;
249+ uint32_t fifoRkey ;
250+ struct ibv_mr * fifoMr ;
251+ struct ibv_sge fifoSge ;
252+ struct ibv_mr * sizesFifoMr ;
253+ };
254+
255+ struct alignas (32 ) flagcxIbRecvComm {
256+ struct flagcxIbNetCommBase base ;
257+ struct flagcxIbRecvCommDev devs [FLAGCX_IB_MAX_DEVS_PER_NIC ];
258+ struct flagcxIbRemFifo remFifo ;
259+ int sizesFifo [MAX_REQUESTS ][FLAGCX_NET_IB_MAX_RECVS ];
260+ int gpuFlushHostMem ;
261+ int flushEnabled ;
262+ };
263+
145264// Global arrays (declared as extern, defined in adaptor files)
146265extern struct flagcxIbDev flagcxIbDevs [MAX_IB_DEVS ];
147266extern struct flagcxIbMergedDev flagcxIbMergedDevs [MAX_IB_VDEVS ];
148267
268+ // Global variables (declared as extern, defined in adaptor files)
269+ extern char flagcxIbIfName [MAX_IF_NAME_SIZE + 1 ];
270+ extern union flagcxSocketAddress flagcxIbIfAddr ;
271+ extern int flagcxNMergedIbDevs ;
272+ extern int flagcxNIbDevs ;
273+ extern pthread_mutex_t flagcxIbLock ;
274+ extern int flagcxIbRelaxedOrderingEnabled ;
275+ extern pthread_t flagcxIbAsyncThread ;
276+
277+
278+ // Parameter functions
279+ extern int64_t flagcxParamIbGidIndex (void );
280+ extern int64_t flagcxParamIbRoceVersionNum (void );
281+ extern int64_t flagcxParamIbTimeout (void );
282+ extern int64_t flagcxParamIbRetryCnt (void );
283+ extern int64_t flagcxParamIbPkey (void );
284+ extern int64_t flagcxParamIbUseInline (void );
285+ extern int64_t flagcxParamIbSl (void );
286+ extern int64_t flagcxParamIbTc (void );
287+ extern int64_t flagcxParamIbArThreshold (void );
288+ extern int64_t flagcxParamIbPciRelaxedOrdering (void );
289+ extern int64_t flagcxParamIbAdaptiveRouting (void );
290+ extern int64_t flagcxParamIbDisable (void );
291+ extern int64_t flagcxParamIbMergeVfs (void );
292+ extern int64_t flagcxParamIbMergeNics (void );
293+ extern int64_t flagcxParamIbQpsPerConn (void );
294+
295+ extern sa_family_t envIbAddrFamily (void );
296+ extern void * envIbAddrRange (sa_family_t af , int * mask );
297+ extern sa_family_t getGidAddrFamily (union ibv_gid * gid );
298+ extern bool matchGidAddrPrefix (sa_family_t af , void * prefix , int prefixlen , union ibv_gid * gid );
299+ extern bool configuredGid (union ibv_gid * gid );
300+ extern bool linkLocalGid (union ibv_gid * gid );
301+ extern bool validGid (union ibv_gid * gid );
302+ extern flagcxResult_t flagcxIbRoceGetVersionNum (const char * deviceName , int portNum , int gidIndex , int * version );
303+ extern flagcxResult_t flagcxUpdateGidIndex (struct ibv_context * context , uint8_t portNum , sa_family_t af , void * prefix , int prefixlen , int roceVer , int gidIndexCandidate , int * gidIndex );
304+ extern flagcxResult_t flagcxIbGetGidIndex (struct ibv_context * context , uint8_t portNum , int gidTblLen , int * gidIndex );
305+ extern flagcxResult_t flagcxIbGetPciPath (char * devName , char * * path , int * realPort );
306+ extern int flagcxIbWidth (int width );
307+ extern int flagcxIbSpeed (int speed );
308+ extern int flagcxIbRelaxedOrderingCapable (void );
309+ extern int flagcxIbFindMatchingDev (int dev );
310+ extern void * flagcxIbAsyncThreadMain (void * args );
311+
312+ extern int ibvWidths [];
313+ extern int ibvSpeeds [];
314+
315+ extern int firstBitSet (int val , int max );
316+
317+ extern flagcxResult_t flagcxIbDevices (int * ndev );
318+ extern flagcxResult_t flagcxIbGdrSupport (void );
319+ extern flagcxResult_t flagcxIbDmaBufSupport (int dev );
320+ extern flagcxResult_t flagcxIbFreeRequest (struct flagcxIbRequest * r );
321+
322+
323+ static_assert ((sizeof (struct flagcxIbNetCommBase ) % 32 ) == 0 ,
324+ "flagcxIbNetCommBase size must be 32-byte multiple to ensure "
325+ "fifo is at proper offset" );
326+ static_assert ((offsetof(struct flagcxIbSendComm , fifo ) % 32 ) == 0 ,
327+ "flagcxIbSendComm fifo must be 32-byte aligned" );
328+ static_assert ((sizeof (struct flagcxIbSendFifo ) % 32 ) == 0 ,
329+ "flagcxIbSendFifo element size must be 32-byte multiples" );
330+ static_assert ((offsetof(struct flagcxIbSendComm , sges ) % 32 ) == 0 ,
331+ "sges must be 32-byte aligned" );
332+ static_assert ((offsetof(struct flagcxIbSendComm , wrs ) % 32 ) == 0 ,
333+ "wrs must be 32-byte aligned" );
334+ static_assert ((offsetof(struct flagcxIbRecvComm , remFifo ) % 32 ) == 0 ,
335+ "flagcxIbRecvComm fifo must be 32-byte aligned" );
336+ static_assert (sizeof (struct flagcxIbHandle ) < FLAGCX_NET_HANDLE_MAXSIZE ,
337+ "flagcxIbHandle size must be smaller than FLAGCX_NET_HANDLE_MAXSIZE" );
338+
339+
340+ static_assert (MAX_REQUESTS <= 256 , "request id are encoded in wr_id and we "
341+ "need up to 8 requests ids per completion" );
342+
149343#endif // FLAGCX_IB_COMMON_H_
0 commit comments