aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/netliba/v6/ib_cs.cpp
blob: 1b904e335845ad82308eef64f7f6fcb6216b30f5 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#include "stdafx.h"
#include "ib_cs.h"
#include "ib_buffers.h"
#include "ib_mem.h"
#include <util/generic/deque.h>
#include <util/digest/murmur.h>

/*
Questions
 does rdma work?
 what is RC latency?
   3us if measured by completion event arrival
   2.3us if bind to socket 0 & use inline send
 memory region - can we use memory from some offset?
   yes
 is send_inplace supported and is it faster?
   yes, supported, 1024 bytes limit, inline is faster (2.4 vs 2.9)
 is srq a penalty compared to regular rq?
   rdma is faster anyway, so why bother

collective ops
  support asymmetric configurations by additional transfers (overlap 1 or 2 hosts is allowed)

remove commented stuff all around

next gen
  shared+registered large mem blocks for easy transfer
  no crc calcs
  direct channel exposure
    make ui64 packet id? otherwise we could get duplicate id (highly improbable but possible)
  lock free allocation in ib_mem
*/

namespace NNetliba {
    const int WELCOME_QKEY = 0x13081976;

    const int MAX_SEND_COUNT = (128 - 10) / 4;
    const int QP_SEND_QUEUE_SIZE = (MAX_SEND_COUNT * 2 + 10) + 10;
    const int WELCOME_QP_SEND_SIZE = 10000;

    const int MAX_SRQ_WORK_REQUESTS = 10000;
    const int MAX_CQ_EVENTS = MAX_SRQ_WORK_REQUESTS; //1000;

    const double CHANNEL_CHECK_INTERVAL = 1.;

    const int TRAFFIC_SL = 4; // 4 is mandatory for RoCE to work, it's the only lossless priority(?)
    const int CONNECT_SL = 1;

    class TIBClientServer: public IIBClientServer {
        enum ECmd {
            CMD_HANDSHAKE,
            CMD_HANDSHAKE_ACK,
            CMD_CONFIRM,
            CMD_DATA_TINY,
            CMD_DATA_INIT,
            CMD_BUFFER_READY,
            CMD_DATA_COMPLETE,
            CMD_KEEP_ALIVE,
        };
#pragma pack(1)
        struct TCmdHandshake {
            char Command;
            int QPN, PSN;
            TGUID SocketId;
            TUdpAddress MyAddress; // address of the handshake sender as viewed from receiver
        };
        struct TCmdHandshakeAck {
            char Command;
            int QPN, PSN;
            int YourQPN;
        };
        struct TCmdConfirm {
            char Command;
        };
        struct TCmdDataTiny {
            struct THeader {
                char Command;
                ui16 Size;
                TGUID PacketGuid;
            } Header;
            typedef char TDataVec[SMALL_PKT_SIZE - sizeof(THeader)];
            TDataVec Data;

            static int GetMaxDataSize() {
                return sizeof(TDataVec);
            }
        };
        struct TCmdDataInit {
            char Command;
            size_t Size;
            TGUID PacketGuid;
        };
        struct TCmdBufferReady {
            char Command;
            TGUID PacketGuid;
            ui64 RemoteAddr;
            ui32 RemoteKey;
        };
        struct TCmdDataComplete {
            char Command;
            TGUID PacketGuid;
            ui64 DataHash;
        };
        struct TCmdKeepAlive {
            char Command;
        };
#pragma pack()

        struct TCompleteInfo {
            enum {
                CI_DATA_TINY,
                CI_RDMA_COMPLETE,
                CI_DATA_SENT,
                CI_KEEP_ALIVE,
                CI_IGNORE,
            };
            int Type;
            int BufId;
            TIBMsgHandle MsgHandle;

            TCompleteInfo(int t, int bufId, TIBMsgHandle msg)
                : Type(t)
                , BufId(bufId)
                , MsgHandle(msg)
            {
            }
        };
        struct TPendingQueuedSend {
            TGUID PacketGuid;
            TIBMsgHandle MsgHandle;
            TRopeDataPacket* Data;

            TPendingQueuedSend()
                : MsgHandle(0)
            {
            }
            TPendingQueuedSend(const TGUID& packetGuid, TIBMsgHandle msgHandle, TRopeDataPacket* data)
                : PacketGuid(packetGuid)
                , MsgHandle(msgHandle)
                , Data(data)
            {
            }
        };
        struct TQueuedSend {
            TGUID PacketGuid;
            TIBMsgHandle MsgHandle;
            TIntrusivePtr<TIBMemBlock> MemBlock;
            ui64 RemoteAddr;
            ui32 RemoteKey;

            TQueuedSend() = default;
            TQueuedSend(const TGUID& packetGuid, TIBMsgHandle msgHandle)
                : PacketGuid(packetGuid)
                , MsgHandle(msgHandle)
                , RemoteAddr(0)
                , RemoteKey(0)
            {
            }
        };
        struct TQueuedRecv {
            TGUID PacketGuid;
            TIntrusivePtr<TIBMemBlock> Data;

            TQueuedRecv() = default;
            TQueuedRecv(const TGUID& packetGuid, TPtrArg<TIBMemBlock> data)
                : PacketGuid(packetGuid)
                , Data(data)
            {
            }
        };
        struct TIBPeer: public IIBPeer {
            TUdpAddress PeerAddress;
            TIntrusivePtr<TRCQueuePair> QP;
            EState State;
            int SendCount;
            NHPTimer::STime LastRecv;
            TDeque<TPendingQueuedSend> PendingSendQueue;
            // these lists have limited size and potentially just circle buffers
            TDeque<TQueuedSend> SendQueue;
            TDeque<TQueuedRecv> RecvQueue;
            TDeque<TCompleteInfo> OutMsgs;

            TIBPeer(const TUdpAddress& peerAddress, TPtrArg<TRCQueuePair> qp)
                : PeerAddress(peerAddress)
                , QP(qp)
                , State(CONNECTING)
                , SendCount(0)
            {
                NHPTimer::GetTime(&LastRecv);
            }
            ~TIBPeer() override {
                //printf("IBPeer destroyed\n");
            }
            EState GetState() override {
                return State;
            }
            TDeque<TQueuedSend>::iterator GetSend(const TGUID& packetGuid) {
                for (TDeque<TQueuedSend>::iterator z = SendQueue.begin(); z != SendQueue.end(); ++z) {
                    if (z->PacketGuid == packetGuid) {
                        return z;
                    }
                }
                Y_VERIFY(0, "no send by guid");
                return SendQueue.begin();
            }
            TDeque<TQueuedSend>::iterator GetSend(TIBMsgHandle msgHandle) {
                for (TDeque<TQueuedSend>::iterator z = SendQueue.begin(); z != SendQueue.end(); ++z) {
                    if (z->MsgHandle == msgHandle) {
                        return z;
                    }
                }
                Y_VERIFY(0, "no send by handle");
                return SendQueue.begin();
            }
            TDeque<TQueuedRecv>::iterator GetRecv(const TGUID& packetGuid) {
                for (TDeque<TQueuedRecv>::iterator z = RecvQueue.begin(); z != RecvQueue.end(); ++z) {
                    if (z->PacketGuid == packetGuid) {
                        return z;
                    }
                }
                Y_VERIFY(0, "no recv by guid");
                return RecvQueue.begin();
            }
            void PostRDMA(TQueuedSend& qs) {
                Y_ASSERT(qs.RemoteAddr != 0 && qs.MemBlock.Get() != nullptr);
                QP->PostRDMAWrite(qs.RemoteAddr, qs.RemoteKey,
                                  qs.MemBlock->GetMemRegion(), 0, qs.MemBlock->GetData(), qs.MemBlock->GetSize());
                OutMsgs.push_back(TCompleteInfo(TCompleteInfo::CI_RDMA_COMPLETE, 0, qs.MsgHandle));
                //printf("Post rdma write, size %d\n", qs.Data->GetSize());
            }
            void PostSend(TIBBufferPool& bp, const void* data, size_t len, int t, TIBMsgHandle msgHandle) {
                int bufId = bp.PostSend(QP, data, len);
                OutMsgs.push_back(TCompleteInfo(t, bufId, msgHandle));
            }
        };

        TIntrusivePtr<TIBPort> Port;
        TIntrusivePtr<TIBMemPool> MemPool;
        TIntrusivePtr<TIBMemPool::TCopyResultStorage> CopyResults;
        TIntrusivePtr<TComplectionQueue> CQ;
        TIBBufferPool BP;
        TIntrusivePtr<TUDQueuePair> WelcomeQP;
        int WelcomeQPN;
        TIBConnectInfo ConnectInfo;
        TDeque<TIBSendResult> SendResults;
        TDeque<TRequest*> ReceivedList;
        typedef THashMap<int, TIntrusivePtr<TIBPeer>> TPeerChannelHash;
        TPeerChannelHash Channels;
        TIBMsgHandle MsgCounter;
        NHPTimer::STime LastCheckTime;

        ~TIBClientServer() override {
            for (auto& z : ReceivedList) {
                delete z;
            }
        }
        TIBPeer* GetChannelByQPN(int qpn) {
            TPeerChannelHash::iterator z = Channels.find(qpn);
            if (z == Channels.end()) {
                return nullptr;
            }
            return z->second.Get();
        }

        // IIBClientServer
        TRequest* GetRequest() override {
            if (ReceivedList.empty()) {
                return nullptr;
            }
            TRequest* res = ReceivedList.front();
            ReceivedList.pop_front();
            return res;
        }
        bool GetSendResult(TIBSendResult* res) override {
            if (SendResults.empty()) {
                return false;
            }
            *res = SendResults.front();
            SendResults.pop_front();
            return true;
        }
        void StartSend(TPtrArg<TIBPeer> peer, const TGUID& packetGuid, TIBMsgHandle msgHandle, TRopeDataPacket* data) {
            int sz = data->GetSize();
            if (sz <= TCmdDataTiny::GetMaxDataSize()) {
                TCmdDataTiny dataTiny;
                dataTiny.Header.Command = CMD_DATA_TINY;
                dataTiny.Header.Size = (ui16)sz;
                dataTiny.Header.PacketGuid = packetGuid;
                TBlockChainIterator bc(data->GetChain());
                bc.Read(dataTiny.Data, sz);

                peer->PostSend(BP, &dataTiny, sizeof(dataTiny.Header) + sz, TCompleteInfo::CI_DATA_TINY, msgHandle);
                //printf("Send CMD_DATA_TINY\n");
            } else {
                MemPool->CopyData(data, msgHandle, peer, CopyResults);
                peer->SendQueue.push_back(TQueuedSend(packetGuid, msgHandle));
                {
                    TQueuedSend& msg = peer->SendQueue.back();
                    TCmdDataInit dataInit;
                    dataInit.Command = CMD_DATA_INIT;
                    dataInit.PacketGuid = msg.PacketGuid;
                    dataInit.Size = data->GetSize();
                    peer->PostSend(BP, &dataInit, sizeof(dataInit), TCompleteInfo::CI_IGNORE, 0);
                    //printf("Send CMD_DATA_INIT\n");
                }
            }
            ++peer->SendCount;
        }
        void SendCompleted(TPtrArg<TIBPeer> peer, TIBMsgHandle msgHandle) {
            SendResults.push_back(TIBSendResult(msgHandle, true));
            if (--peer->SendCount < MAX_SEND_COUNT) {
                if (!peer->PendingSendQueue.empty()) {
                    TPendingQueuedSend& qs = peer->PendingSendQueue.front();
                    StartSend(peer, qs.PacketGuid, qs.MsgHandle, qs.Data);
                    //printf("Sending pending %d\n", qs.MsgHandle);
                    peer->PendingSendQueue.pop_front();
                }
            }
        }
        void SendFailed(TPtrArg<TIBPeer> peer, TIBMsgHandle msgHandle) {
            //printf("IB SendFailed()\n");
            SendResults.push_back(TIBSendResult(msgHandle, false));
            --peer->SendCount;
        }
        void PeerFailed(TPtrArg<TIBPeer> peer) {
            //printf("PeerFailed(), peer %p, state %d (%d pending, %d queued, %d out, %d sendcount)\n",
            //    peer.Get(), peer->State,
            //    (int)peer->PendingSendQueue.size(),
            //    (int)peer->SendQueue.size(),
            //    (int)peer->OutMsgs.size(),
            //    peer->SendCount);
            peer->State = IIBPeer::FAILED;
            while (!peer->PendingSendQueue.empty()) {
                TPendingQueuedSend& qs = peer->PendingSendQueue.front();
                SendResults.push_back(TIBSendResult(qs.MsgHandle, false));
                peer->PendingSendQueue.pop_front();
            }
            while (!peer->SendQueue.empty()) {
                TQueuedSend& qs = peer->SendQueue.front();
                SendFailed(peer, qs.MsgHandle);
                peer->SendQueue.pop_front();
            }
            while (!peer->OutMsgs.empty()) {
                TCompleteInfo& cc = peer->OutMsgs.front();
                //printf("Don't wait completion for sent packet (QPN %d), bufId %d\n", peer->QP->GetQPN(), cc.BufId);
                if (cc.Type == TCompleteInfo::CI_DATA_TINY) {
                    SendFailed(peer, cc.MsgHandle);
                }
                BP.FreeBuf(cc.BufId);
                peer->OutMsgs.pop_front();
            }
            {
                Y_ASSERT(peer->SendCount == 0);
                //printf("Remove peer %p from hash (QPN %d)\n", peer.Get(), peer->QP->GetQPN());
                TPeerChannelHash::iterator z = Channels.find(peer->QP->GetQPN());
                if (z == Channels.end()) {
                    Y_VERIFY(0, "peer failed for unregistered peer");
                }
                Channels.erase(z);
            }
        }
        TIBMsgHandle Send(TPtrArg<IIBPeer> peerArg, TRopeDataPacket* data, const TGUID& packetGuid) override {
            TIBPeer* peer = static_cast<TIBPeer*>(peerArg.Get()); // trust me, I'm professional
            if (peer == nullptr || peer->State != IIBPeer::OK) {
                return -1;
            }
            Y_ASSERT(Channels.find(peer->QP->GetQPN())->second == peer);
            TIBMsgHandle msgHandle = ++MsgCounter;
            if (peer->SendCount >= MAX_SEND_COUNT) {
                peer->PendingSendQueue.push_back(TPendingQueuedSend(packetGuid, msgHandle, data));
            } else {
                //printf("Sending direct %d\n", msgHandle);
                StartSend(peer, packetGuid, msgHandle, data);
            }
            return msgHandle;
        }
        void ParsePacket(ibv_wc* wc, NHPTimer::STime tCurrent) {
            if (wc->status != IBV_WC_SUCCESS) {
                TIBPeer* peer = GetChannelByQPN(wc->qp_num);
                if (peer) {
                    //printf("failed recv packet (status %d)\n", wc->status);
                    PeerFailed(peer);
                } else {
                    //printf("Ignoring recv error for closed/non existing QPN %d\n", wc->qp_num);
                }
                return;
            }

            TIBRecvPacketProcess pkt(BP, *wc);

            TIBPeer* peer = GetChannelByQPN(wc->qp_num);
            if (peer) {
                Y_ASSERT(peer->State != IIBPeer::FAILED);
                peer->LastRecv = tCurrent;
                char cmdId = *(const char*)pkt.GetData();
                switch (cmdId) {
                    case CMD_CONFIRM:
                        //printf("got confirm\n");
                        Y_ASSERT(peer->State == IIBPeer::CONNECTING);
                        peer->State = IIBPeer::OK;
                        break;
                    case CMD_DATA_TINY:
                        //printf("Recv CMD_DATA_TINY\n");
                        {
                            const TCmdDataTiny& dataTiny = *(TCmdDataTiny*)pkt.GetData();
                            TRequest* req = new TRequest;
                            req->Address = peer->PeerAddress;
                            req->Guid = dataTiny.Header.PacketGuid;
                            req->Data = new TRopeDataPacket;
                            req->Data->Write(dataTiny.Data, dataTiny.Header.Size);
                            ReceivedList.push_back(req);
                        }
                        break;
                    case CMD_DATA_INIT:
                        //printf("Recv CMD_DATA_INIT\n");
                        {
                            const TCmdDataInit& data = *(TCmdDataInit*)pkt.GetData();
                            TIntrusivePtr<TIBMemBlock> blk = MemPool->Alloc(data.Size);
                            peer->RecvQueue.push_back(TQueuedRecv(data.PacketGuid, blk));
                            TCmdBufferReady ready;
                            ready.Command = CMD_BUFFER_READY;
                            ready.PacketGuid = data.PacketGuid;
                            ready.RemoteAddr = reinterpret_cast<ui64>(blk->GetData()) / sizeof(char);
                            ready.RemoteKey = blk->GetMemRegion()->GetRKey();

                            peer->PostSend(BP, &ready, sizeof(ready), TCompleteInfo::CI_IGNORE, 0);
                            //printf("Send CMD_BUFFER_READY\n");
                        }
                        break;
                    case CMD_BUFFER_READY:
                        //printf("Recv CMD_BUFFER_READY\n");
                        {
                            const TCmdBufferReady& ready = *(TCmdBufferReady*)pkt.GetData();
                            TDeque<TQueuedSend>::iterator z = peer->GetSend(ready.PacketGuid);
                            TQueuedSend& qs = *z;
                            qs.RemoteAddr = ready.RemoteAddr;
                            qs.RemoteKey = ready.RemoteKey;
                            if (qs.MemBlock.Get()) {
                                peer->PostRDMA(qs);
                            }
                        }
                        break;
                    case CMD_DATA_COMPLETE:
                        //printf("Recv CMD_DATA_COMPLETE\n");
                        {
                            const TCmdDataComplete& cmd = *(TCmdDataComplete*)pkt.GetData();
                            TDeque<TQueuedRecv>::iterator z = peer->GetRecv(cmd.PacketGuid);
                            TQueuedRecv& qr = *z;
#ifdef _DEBUG
                            Y_VERIFY(MurmurHash<ui64>(qr.Data->GetData(), qr.Data->GetSize()) == cmd.DataHash || cmd.DataHash == 0, "RDMA data hash mismatch");
#endif
                            TRequest* req = new TRequest;
                            req->Address = peer->PeerAddress;
                            req->Guid = qr.PacketGuid;
                            req->Data = new TRopeDataPacket;
                            req->Data->AddBlock(qr.Data.Get(), qr.Data->GetData(), qr.Data->GetSize());
                            ReceivedList.push_back(req);
                            peer->RecvQueue.erase(z);
                        }
                        break;
                    case CMD_KEEP_ALIVE:
                        break;
                    default:
                        Y_ASSERT(0);
                        break;
                }
            } else {
                // can get here
                //printf("Ignoring packet for closed/non existing QPN %d\n", wc->qp_num);
            }
        }
        void OnComplete(ibv_wc* wc, NHPTimer::STime tCurrent) {
            TIBPeer* peer = GetChannelByQPN(wc->qp_num);
            if (peer) {
                if (!peer->OutMsgs.empty()) {
                    peer->LastRecv = tCurrent;
                    if (wc->status != IBV_WC_SUCCESS) {
                        //printf("completed with status %d\n", wc->status);
                        PeerFailed(peer);
                    } else {
                        const TCompleteInfo& cc = peer->OutMsgs.front();
                        switch (cc.Type) {
                            case TCompleteInfo::CI_DATA_TINY:
                                //printf("Completed data_tiny\n");
                                SendCompleted(peer, cc.MsgHandle);
                                break;
                            case TCompleteInfo::CI_RDMA_COMPLETE:
                                //printf("Completed rdma_complete\n");
                                {
                                    TDeque<TQueuedSend>::iterator z = peer->GetSend(cc.MsgHandle);
                                    TQueuedSend& qs = *z;

                                    TCmdDataComplete complete;
                                    complete.Command = CMD_DATA_COMPLETE;
                                    complete.PacketGuid = qs.PacketGuid;
#ifdef _DEBUG
                                    complete.DataHash = MurmurHash<ui64>(qs.MemBlock->GetData(), qs.MemBlock->GetSize());
#else
                                    complete.DataHash = 0;
#endif

                                    peer->PostSend(BP, &complete, sizeof(complete), TCompleteInfo::CI_DATA_SENT, qs.MsgHandle);
                                    //printf("Send CMD_DATA_COMPLETE\n");
                                }
                                break;
                            case TCompleteInfo::CI_DATA_SENT:
                                //printf("Completed data_sent\n");
                                {
                                    TDeque<TQueuedSend>::iterator z = peer->GetSend(cc.MsgHandle);
                                    TIBMsgHandle msgHandle = z->MsgHandle;
                                    peer->SendQueue.erase(z);
                                    SendCompleted(peer, msgHandle);
                                }
                                break;
                            case TCompleteInfo::CI_KEEP_ALIVE:
                                break;
                            case TCompleteInfo::CI_IGNORE:
                                //printf("Completed ignored\n");
                                break;
                            default:
                                Y_ASSERT(0);
                                break;
                        }
                        peer->OutMsgs.pop_front();
                        BP.FreeBuf(wc->wr_id);
                    }
                } else {
                    Y_VERIFY(0, "got completion without outstanding messages");
                }
            } else {
                //printf("Got completion for non existing qpn %d, bufId %d (status %d)\n", wc->qp_num, (int)wc->wr_id, (int)wc->status);
                if (wc->status == IBV_WC_SUCCESS) {
                    Y_VERIFY(0, "only errors should go unmatched");
                }
                // no need to free buf since it has to be freed in PeerFailed()
            }
        }
        void ParseWelcomePacket(ibv_wc* wc) {
            TIBRecvPacketProcess pkt(BP, *wc);

            char cmdId = *(const char*)pkt.GetUDData();
            switch (cmdId) {
                case CMD_HANDSHAKE: {
                    //printf("got handshake\n");
                    const TCmdHandshake& handshake = *(TCmdHandshake*)pkt.GetUDData();
                    if (handshake.SocketId != ConnectInfo.SocketId) {
                        // connection attempt from wrong IB subnet
                        break;
                    }
                    TIntrusivePtr<TRCQueuePair> rcQP;
                    rcQP = new TRCQueuePair(Port->GetCtx(), CQ, BP.GetSRQ(), QP_SEND_QUEUE_SIZE);

                    int qpn = rcQP->GetQPN();
                    Y_ASSERT(Channels.find(qpn) == Channels.end());
                    TIntrusivePtr<TIBPeer>& peer = Channels[qpn];
                    peer = new TIBPeer(handshake.MyAddress, rcQP);

                    ibv_ah_attr peerAddr;
                    TIntrusivePtr<TAddressHandle> ahPeer;
                    Port->GetAHAttr(wc, pkt.GetGRH(), &peerAddr);
                    ahPeer = new TAddressHandle(Port->GetCtx(), &peerAddr);

                    peerAddr.sl = TRAFFIC_SL;
                    rcQP->Init(peerAddr, handshake.QPN, handshake.PSN);

                    TCmdHandshakeAck handshakeAck;
                    handshakeAck.Command = CMD_HANDSHAKE_ACK;
                    handshakeAck.PSN = rcQP->GetPSN();
                    handshakeAck.QPN = rcQP->GetQPN();
                    handshakeAck.YourQPN = handshake.QPN;
                    // if ack gets lost we'll create new Peer Channel
                    // and this one will be erased in Step() by timeout counted from LastRecv
                    BP.PostSend(WelcomeQP, ahPeer, wc->src_qp, WELCOME_QKEY, &handshakeAck, sizeof(handshakeAck));
                    //printf("send handshake_ack\n");
                } break;
                case CMD_HANDSHAKE_ACK: {
                    //printf("got handshake_ack\n");
                    const TCmdHandshakeAck& handshakeAck = *(TCmdHandshakeAck*)pkt.GetUDData();
                    TIBPeer* peer = GetChannelByQPN(handshakeAck.YourQPN);
                    if (peer) {
                        ibv_ah_attr peerAddr;
                        Port->GetAHAttr(wc, pkt.GetGRH(), &peerAddr);

                        peerAddr.sl = TRAFFIC_SL;
                        peer->QP->Init(peerAddr, handshakeAck.QPN, handshakeAck.PSN);

                        peer->State = IIBPeer::OK;

                        TCmdConfirm confirm;
                        confirm.Command = CMD_CONFIRM;
                        peer->PostSend(BP, &confirm, sizeof(confirm), TCompleteInfo::CI_IGNORE, 0);
                        //printf("send confirm\n");
                    } else {
                        // respective QPN was deleted or never existed
                        // silently ignore and peer channel on remote side
                        // will not get into confirmed state and will be deleted
                    }
                } break;
                default:
                    Y_ASSERT(0);
                    break;
            }
        }
        bool Step(NHPTimer::STime tCurrent) override {
            bool rv = false;
            // only have to process completions, everything is done on completion of something
            ibv_wc wcArr[10];
            for (;;) {
                int wcCount = CQ->Poll(wcArr, Y_ARRAY_SIZE(wcArr));
                if (wcCount == 0) {
                    break;
                }
                rv = true;
                for (int z = 0; z < wcCount; ++z) {
                    ibv_wc& wc = wcArr[z];
                    if (wc.opcode & IBV_WC_RECV) {
                        // received msg
                        if ((int)wc.qp_num == WelcomeQPN) {
                            if (wc.status != IBV_WC_SUCCESS) {
                                Y_VERIFY(0, "ud recv op completed with error %d\n", (int)wc.status);
                            }
                            Y_ASSERT(wc.opcode == IBV_WC_RECV | IBV_WC_SEND);
                            ParseWelcomePacket(&wc);
                        } else {
                            ParsePacket(&wc, tCurrent);
                        }
                    } else {
                        // send completion
                        if ((int)wc.qp_num == WelcomeQPN) {
                            // ok
                            BP.FreeBuf(wc.wr_id);
                        } else {
                            OnComplete(&wc, tCurrent);
                        }
                    }
                }
            }
            {
                TIntrusivePtr<TIBMemBlock> memBlock;
                i64 msgHandle;
                TIntrusivePtr<TIBPeer> peer;
                while (CopyResults->GetCopyResult(&memBlock, &msgHandle, &peer)) {
                    if (peer->GetState() != IIBPeer::OK) {
                        continue;
                    }
                    TDeque<TQueuedSend>::iterator z = peer->GetSend(msgHandle);
                    if (z == peer->SendQueue.end()) {
                        Y_VERIFY(0, "peer %p, copy completed, msg %d not found?\n", peer.Get(), (int)msgHandle);
                        continue;
                    }
                    TQueuedSend& qs = *z;
                    qs.MemBlock = memBlock;
                    if (qs.RemoteAddr != 0) {
                        peer->PostRDMA(qs);
                    }
                    rv = true;
                }
            }
            {
                NHPTimer::STime t1 = LastCheckTime;
                if (NHPTimer::GetTimePassed(&t1) > CHANNEL_CHECK_INTERVAL) {
                    for (TPeerChannelHash::iterator z = Channels.begin(); z != Channels.end();) {
                        TIntrusivePtr<TIBPeer> peer = z->second;
                        ++z; // peer can be removed from Channels
                        Y_ASSERT(peer->State != IIBPeer::FAILED);
                        NHPTimer::STime t2 = peer->LastRecv;
                        double timeSinceLastRecv = NHPTimer::GetTimePassed(&t2);
                        if (timeSinceLastRecv > CHANNEL_CHECK_INTERVAL) {
                            if (peer->State == IIBPeer::CONNECTING) {
                                Y_ASSERT(peer->OutMsgs.empty() && peer->SendCount == 0);
                                // if handshake does not seem to work out - close connection
                                //printf("IB connecting timed out\n");
                                PeerFailed(peer);
                            } else {
                                // if we have outmsg we hope that IB will report us if there are any problems
                                // with connectivity
                                if (peer->OutMsgs.empty()) {
                                    //printf("Sending keep alive\n");
                                    TCmdKeepAlive keep;
                                    keep.Command = CMD_KEEP_ALIVE;
                                    peer->PostSend(BP, &keep, sizeof(keep), TCompleteInfo::CI_KEEP_ALIVE, 0);
                                }
                            }
                        }
                    }
                    LastCheckTime = t1;
                }
            }
            return rv;
        }
        IIBPeer* ConnectPeer(const TIBConnectInfo& info, const TUdpAddress& peerAddr, const TUdpAddress& myAddr) override {
            for (auto& channel : Channels) {
                TIntrusivePtr<TIBPeer> peer = channel.second;
                if (peer->PeerAddress == peerAddr) {
                    return peer.Get();
                }
            }
            TIntrusivePtr<TRCQueuePair> rcQP;
            rcQP = new TRCQueuePair(Port->GetCtx(), CQ, BP.GetSRQ(), QP_SEND_QUEUE_SIZE);

            int qpn = rcQP->GetQPN();
            Y_ASSERT(Channels.find(qpn) == Channels.end());
            TIntrusivePtr<TIBPeer>& peer = Channels[qpn];
            peer = new TIBPeer(peerAddr, rcQP);

            TCmdHandshake handshake;
            handshake.Command = CMD_HANDSHAKE;
            handshake.PSN = rcQP->GetPSN();
            handshake.QPN = rcQP->GetQPN();
            handshake.SocketId = info.SocketId;
            handshake.MyAddress = myAddr;

            TIntrusivePtr<TAddressHandle> serverAH;
            if (info.LID != 0) {
                serverAH = new TAddressHandle(Port, info.LID, CONNECT_SL);
            } else {
                //ibv_gid addr;
                //addr.global.subnet_prefix = info.Subnet;
                //addr.global.interface_id = info.Interface;
                //serverAH = new TAddressHandle(Port, addr, CONNECT_SL);

                TUdpAddress local = myAddr;
                local.Port = 0;
                TUdpAddress remote = peerAddr;
                remote.Port = 0;
                //printf("local Addr %s\n", GetAddressAsString(local).c_str());
                //printf("remote Addr %s\n", GetAddressAsString(remote).c_str());
                // CRAP - somehow prevent connecting machines from different RoCE isles
                serverAH = new TAddressHandle(Port, remote, local, CONNECT_SL);
                if (!serverAH->IsValid()) {
                    return nullptr;
                }
            }
            BP.PostSend(WelcomeQP, serverAH, info.QPN, WELCOME_QKEY, &handshake, sizeof(handshake));
            //printf("send handshake\n");

            return peer.Get();
        }
        const TIBConnectInfo& GetConnectInfo() override {
            return ConnectInfo;
        }

    public:
        TIBClientServer(TPtrArg<TIBPort> port)
            : Port(port)
            , MemPool(GetIBMemPool())
            , CQ(new TComplectionQueue(port->GetCtx(), MAX_CQ_EVENTS))
            , BP(port->GetCtx(), MAX_SRQ_WORK_REQUESTS)
            , WelcomeQP(new TUDQueuePair(port, CQ, BP.GetSRQ(), WELCOME_QP_SEND_SIZE))
            , WelcomeQPN(WelcomeQP->GetQPN())
            , MsgCounter(1)
        {
            CopyResults = new TIBMemPool::TCopyResultStorage;
            CreateGuid(&ConnectInfo.SocketId);
            ibv_gid addr;
            port->GetGID(&addr);
            ConnectInfo.Interface = addr.global.interface_id;
            ConnectInfo.Subnet = addr.global.subnet_prefix;
            //printf("connect addr subnet %lx, iface %lx\n", addr.global.subnet_prefix, addr.global.interface_id);
            ConnectInfo.LID = port->GetLID();
            ConnectInfo.QPN = WelcomeQPN;

            WelcomeQP->Init(WELCOME_QKEY);

            NHPTimer::GetTime(&LastCheckTime);
        }
    };

    IIBClientServer* CreateIBClientServer() {
        TIntrusivePtr<TIBPort> port = GetIBDevice();
        if (port.Get() == nullptr) {
            return nullptr;
        }
        return new TIBClientServer(port);
    }
}