forked from wxdwfc/rlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdma_ctrl_impl.hpp
607 lines (503 loc) · 15.8 KB
/
rdma_ctrl_impl.hpp
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
#include <pthread.h>
#include <map>
#include <mutex>
namespace rdmaio {
/**
* Simple critical section
* It uses a single global block to guard RdmaCtrl.
* This is acceptable, since RdmaCtrl is only the control plane.
*/
class SCS {
public:
SCS() {
get_lock().lock();
}
~SCS() {
get_lock().unlock();
}
private:
static std::mutex &get_lock() {
static std::mutex lock;
return lock;
}
};
/**
* convert qp idx(node,worker,idx) -> key
*/
inline uint32_t get_rc_key (const QPIdx idx) {
return ::rdmaio::encode_qp_id(idx.node_id,RC_ID_BASE + idx.worker_id * 64 + idx.index);
}
inline uint32_t get_ud_key(const QPIdx idx) {
return ::rdmaio::encode_qp_id(idx.worker_id,UD_ID_BASE + idx.index);
}
/**
* Control plane of RLib
*/
class RdmaCtrl::RdmaCtrlImpl {
public:
RdmaCtrlImpl(int node_id, int tcp_base_port,connection_callback_t callback,std::string local_ip):
node_id_(node_id),
tcp_base_port_(tcp_base_port),
local_ip_(local_ip),
qp_callback_(callback)
{
// start the background thread to handle QP connection request
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&handler_tid_, &attr, &RdmaCtrlImpl::connection_handler_wrapper,this);
}
~RdmaCtrlImpl() {
running_ = false; // wait for the handler to join
pthread_join(handler_tid_,NULL);
RDMA_LOG(INFO) << "rdma controler close: does not handle any future connections.";
}
RNicHandler *open_thread_local_device(DevIdx idx) {
// already openend device
if(rnic_instance() != nullptr)
return rnic_instance();
auto handler = open_device(idx);
rnic_instance() = handler;
return rnic_instance();
}
RNicHandler *open_device(DevIdx idx) {
RNicHandler *rnic = nullptr;
struct ibv_device **dev_list = nullptr; struct ibv_context *ib_ctx = nullptr; struct ibv_pd *pd = nullptr; int num_devices;
int rc; // return code
dev_list = ibv_get_device_list(&num_devices);
if(idx.dev_id >= num_devices || idx.dev_id < 0) {
RDMA_LOG(WARNING) << "wrong dev_id: " << idx.dev_id << "; total " << num_devices <<" found";
goto OPEN_END;
}
// alloc ctx
ib_ctx = ibv_open_device(dev_list[idx.dev_id]);
if(ib_ctx == nullptr) {
RDMA_LOG(WARNING) << "failed to open ib ctx w error: " << strerror(errno);
goto OPEN_END;
}
// alloc pd
pd = ibv_alloc_pd(ib_ctx);
if(pd == nullptr) {
RDMA_LOG(WARNING) << "failed to alloc pd w error: " << strerror(errno);
RDMA_VERIFY(INFO,ibv_close_device(ib_ctx) == 0) << "failed to close device " << idx.dev_id;
goto OPEN_END;
}
// fill the lid
ibv_port_attr port_attr;
rc = ibv_query_port (ib_ctx, idx.port_id, &port_attr);
if(rc < 0) {
RDMA_LOG(WARNING) << "failed to query port status w error: " << strerror(errno);
RDMA_VERIFY(INFO,ibv_close_device(ib_ctx) == 0) << "failed to close device " << idx.dev_id;
RDMA_VERIFY(INFO,ibv_dealloc_pd(pd) == 0) << "failed to dealloc pd";
goto OPEN_END;
}
// success open
{
rnic = new RNicHandler(idx.dev_id,idx.port_id,ib_ctx,pd,port_attr.lid);
}
OPEN_END:
if(dev_list != nullptr)
ibv_free_device_list(dev_list);
return rnic;
}
RCQP *get_rc_qp(QPIdx idx) {
RCQP *res = nullptr;
{
SCS s;
res = get_qp<RCQP,get_rc_key>(idx);
};
return res;
}
UDQP *get_ud_qp(QPIdx idx) {
UDQP *res = nullptr;
{
SCS s;
res = get_qp<UDQP,get_ud_key>(idx);
};
return res;
}
/**
* Note! this is not a thread-safe function
*/
template<class T,uint32_t (*F)(QPIdx)>
T *get_qp(QPIdx idx) {
uint32_t key = F(idx);
if(qps_.find(key) == qps_.end())
return nullptr;
else
return dynamic_cast<T *>(qps_[key]);
}
RCQP *create_rc_qp(QPIdx idx, RNicHandler *dev,MemoryAttr *attr) {
RCQP *res = nullptr;
{
SCS s;
uint64_t qid = get_rc_key(idx);
if(qps_.find(qid) != qps_.end()) {
res = dynamic_cast<RCQP *>(qps_[qid]);
} else {
if(attr == NULL)
res = new RCQP(dev,idx);
else
res = new RCQP(dev,idx,*attr);
qps_.insert(std::make_pair(qid,res));
}
};
return res;
}
UDQP *create_ud_qp(QPIdx idx, RNicHandler *dev,MemoryAttr *attr) {
UDQP *res = nullptr;
uint64_t qid = get_ud_key(idx);
{
SCS s;
if(qps_.find(qid) != qps_.end()) {
res = dynamic_cast<UDQP *>(qps_[qid]);
} else {
if(attr == NULL)
res = new UDQP(dev,idx);
else
res = new UDQP(dev,idx,*attr);
qps_.insert(std::make_pair(qid,res));
}
};
return res;
}
bool register_memory(int mr_id,const char *buf,uint64_t size,RNicHandler *rnic,int flag) {
Memory *m = new Memory(buf,size,rnic->pd,flag);
if(!m->valid()) {
RDMA_LOG(WARNING) << "register mr to rnic error: " << strerror(errno);
delete m;
return false;
}
{
SCS s;
if(mrs_.find(mr_id) != mrs_.end()) {
RDMA_LOG(WARNING) << "mr " << mr_id << " has already been registered!";
delete m;
} else {
mrs_.insert(std::make_pair(mr_id,m));
}
};
return true;
}
int get_default_mr(MemoryAttr &attr) {
SCS s;
for(auto it = mrs_.begin();it != mrs_.end();++it) {
int idx = it->first; attr = it->second->rattr;
return idx;
}
return -1;
}
MemoryAttr get_local_mr(int mr_id) {
MemoryAttr attr = {};
{
SCS s;
if(mrs_.find(mr_id) != mrs_.end())
attr = mrs_[mr_id]->rattr;
}
return attr;
}
void clear_dev_info() {
cached_infos_.clear();
}
static std::vector<RNicInfo> query_devs_helper() {
int num_devices = 0; struct ibv_device **dev_list = nullptr;
std::vector<RNicInfo> res;
{ // query the device and its active ports using the underlying APIs
dev_list = ibv_get_device_list(&num_devices);
int temp_devices = num_devices;
if(dev_list == nullptr) {
RDMA_LOG(ERROR) << "cannot get ib devices.";
num_devices = 0;
goto QUERY_END;
}
for(uint dev_id = 0;dev_id < temp_devices;++dev_id) {
struct ibv_context *ib_ctx = ibv_open_device(dev_list[dev_id]);
if(ib_ctx == nullptr) {
RDMA_LOG(ERROR) << "open dev " << dev_id << " error: " << strerror(errno) << " ignored";
num_devices -= 1;
continue;
}
res.emplace_back(ibv_get_device_name(ib_ctx->device),dev_id,ib_ctx);
QUERY_DEV_END:
// close ib_ctx
RDMA_VERIFY(INFO,ibv_close_device(ib_ctx) == 0) << "failed to close device " << dev_id;
}
}
QUERY_END:
if(dev_list != nullptr)
ibv_free_device_list(dev_list);
return res;
}
std::vector<RNicInfo> query_devs() {
if(cached_infos_.size() != 0) {
return cached_infos_;
}
cached_infos_ = query_devs_helper();
return std::vector<RNicInfo>(cached_infos_.begin(),cached_infos_.end());
}
RdmaCtrl::DevIdx convert_port_idx(int idx) {
if(cached_infos_.size() == 0)
query_devs();
for(int dev_id = 0; dev_id < cached_infos_.size();++dev_id) {
int port_num = cached_infos_[dev_id].active_ports.size();
for(int port_id = 1; port_id <= port_num; port_id++) {
if(idx == 0) {
// find one
return DevIdx {.dev_id = dev_id,.port_id = port_id};
}
idx -= 1;
}
}
// failed to find the dev according to the idx
return DevIdx {.dev_id = -1,.port_id = -1};
}
RNicHandler *get_device() {
return rnic_instance();
}
void close_device() {
if(rnic_instance() != nullptr) delete rnic_instance();
rnic_instance() = nullptr;
}
void close_device(RNicHandler *rnic) {
if(rnic != nullptr)
delete rnic;
}
static void *connection_handler_wrapper(void *context)
{
return ((RdmaCtrlImpl *)context)->connection_handler();
}
/**
* Using TCP to connect in-coming QP & MR requests
*/
void *connection_handler(void) {
pthread_detach(pthread_self());
auto listenfd = PreConnector::get_listen_socket(local_ip_,tcp_base_port_);
int opt = 1;
RDMA_VERIFY(ERROR,setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR | SO_REUSEPORT,&opt,sizeof(int)) == 0)
<< "unable to configure socket status.";
RDMA_VERIFY(ERROR,listen(listenfd,24) == 0) << "TCP listen error: " << strerror(errno);
while(running_) {
asm volatile("" ::: "memory");
struct sockaddr_in cli_addr = {0};
socklen_t clilen = sizeof(cli_addr);
auto csfd = accept(listenfd,(struct sockaddr *) &cli_addr, &clilen);
if(csfd < 0) {
RDMA_LOG(ERROR) << "accept a wrong connection error: " << strerror(errno);
continue;
}
if(!PreConnector::wait_recv(csfd,6000)) {
close(csfd);
continue;
}
ConnArg arg;
auto n = recv(csfd,(char *)(&arg),sizeof(ConnArg), MSG_WAITALL);
if(n != sizeof(ConnArg)) {
// an invalid message
close(csfd);
continue;
}
ConnReply reply; reply.ack = ERR;
{ // in a global critical section
SCS s;
switch(arg.type) {
case ConnArg::MR:
if(mrs_.find(arg.payload.mr.mr_id) != mrs_.end()) {
memcpy((char *)(&(reply.payload.mr)),
(char *)(&(mrs_[arg.payload.mr.mr_id]->rattr)),sizeof(MemoryAttr));
reply.ack = SUCC;
};
break;
case ConnArg::QP: {
qp_callback_(arg.payload.qp); // call the user callback
QP *qp = NULL;
switch(arg.payload.qp.qp_type) {
case IBV_QPT_UD:
{
UDQP *ud_qp = get_qp<UDQP,get_ud_key>(
create_ud_idx(arg.payload.qp.from_node,arg.payload.qp.from_worker));
if(ud_qp != nullptr && ud_qp->ready()) {
qp = ud_qp;
}
}
break;
case IBV_QPT_RC:
{
RCQP *rc_qp = get_qp<RCQP,get_rc_key>(
create_rc_idx(arg.payload.qp.from_node,arg.payload.qp.from_worker));
qp = rc_qp;
}
break;
default:
RDMA_LOG(ERROR) << "unknown QP connection type: " << arg.payload.qp.qp_type;
}
if(qp != nullptr) {
reply.payload.qp = qp->get_attr();
reply.ack = SUCC;
}
reply.payload.qp.node_id = node_id_;
break;
}
default:
RDMA_LOG(WARNING) << "received unknown connect type " << arg.type;
}
} // end simple critical section protection
PreConnector::send_to(csfd,(char *)(&reply),sizeof(ConnReply));
PreConnector::wait_close(csfd); // wait for the client to close the connection
}
// end of the server
close(listenfd);
}
private:
friend class RdmaCtrl;
static RNicHandler* &rnic_instance() {
static thread_local RNicHandler * handler = NULL;
return handler;
}
std::vector<RNicInfo> cached_infos_;
// registered MRs at this control manager
std::map<int,Memory *> mrs_;
// created QPs on this control manager
std::map<uint64_t,QP *> qps_;
// local node information
const int node_id_;
const int tcp_base_port_;
const std::string local_ip_;
pthread_t handler_tid_;
bool running_ = true;
// connection callback function
connection_callback_t qp_callback_;
bool link_symmetric_rcqps(const std::vector<std::string> &cluster,int l_mrid,int mr_id,int wid,int idx) {
std::vector<bool> ready_list(cluster.size(),false);
std::vector<MemoryAttr> mrs;
MemoryAttr local_mr = get_local_mr(l_mrid);
for(auto s : cluster) {
// get the target mr
retry:
MemoryAttr mr = {};
auto rc = QP::get_remote_mr(s,tcp_base_port_,mr_id,&mr);
if(rc != SUCC) {
usleep(2000);
goto retry;
}
mrs.push_back(mr);
}
RDMA_ASSERT(mrs.size() == cluster.size());
while(true) {
int connected = 0, i = 0;
for(auto s : cluster) {
if(ready_list[i]) {
i++; connected++;
continue;
}
RCQP *qp = create_rc_qp(QPIdx {.node_id = i,.worker_id = wid,.index = idx },
get_device(),&local_mr);
RDMA_ASSERT(qp != nullptr);
if(qp->connect(s,tcp_base_port_,
QPIdx {.node_id = node_id_,.worker_id = wid, .index = idx}) == SUCC) {
ready_list[i] = true;
connected++;
qp->bind_remote_mr(mrs[i]);
}
i++;
}
if(connected == cluster.size())
break;
else
usleep(1000);
}
return true; // This example does not use error handling
}
void register_qp_callback(connection_callback_t callback) {
qp_callback_ = callback;
}
}; //
// link to the main class
inline __attribute__ ((always_inline))
RdmaCtrl::RdmaCtrl(int node_id, int tcp_base_port,connection_callback_t callback,std::string ip)
:impl_(new RdmaCtrlImpl(node_id,tcp_base_port,callback,ip)){
}
inline __attribute__ ((always_inline))
RdmaCtrl::~RdmaCtrl() {
impl_.reset();
}
inline __attribute__ ((always_inline))
std::vector<RNicInfo> RdmaCtrl::query_devs() {
return impl_->query_devs();
}
inline __attribute__ ((always_inline))
void RdmaCtrl::clear_dev_info() {
return impl_->clear_dev_info();
}
inline __attribute__ ((always_inline))
RNicHandler *RdmaCtrl::get_device() {
return impl_->get_device();
}
inline __attribute__ ((always_inline))
RNicHandler *RdmaCtrl::open_thread_local_device(DevIdx idx) {
return impl_->open_thread_local_device(idx);
}
inline __attribute__ ((always_inline))
RNicHandler *RdmaCtrl::open_device(DevIdx idx) {
return impl_->open_device(idx);
}
inline __attribute__ ((always_inline))
void RdmaCtrl::close_device() {
return impl_->close_device();
}
inline __attribute__ ((always_inline))
void RdmaCtrl::close_device(RNicHandler *rnic) {
return impl_->close_device(rnic);
}
inline __attribute__ ((always_inline))
RdmaCtrl::DevIdx RdmaCtrl::convert_port_idx(int idx) {
return impl_->convert_port_idx(idx);
}
inline __attribute__ ((always_inline))
bool RdmaCtrl::register_memory(int id,const char *buf,uint64_t size,RNicHandler *rnic,int flag) {
return impl_->register_memory(id,buf,size,rnic,flag);
}
inline __attribute__ ((always_inline))
MemoryAttr RdmaCtrl::get_local_mr(int mr_id) {
return impl_->get_local_mr(mr_id);
}
inline __attribute__ ((always_inline))
int RdmaCtrl::get_default_mr(MemoryAttr &attr) {
return impl_->get_default_mr(attr);
}
inline __attribute__ ((always_inline))
RCQP *RdmaCtrl::create_rc_qp(QPIdx idx, RNicHandler *dev,MemoryAttr *attr) {
return impl_->create_rc_qp(idx,dev,attr);
}
inline __attribute__ ((always_inline))
UDQP *RdmaCtrl::create_ud_qp(QPIdx idx, RNicHandler *dev,MemoryAttr *attr) {
return impl_->create_ud_qp(idx,dev,attr);
}
inline __attribute__ ((always_inline))
RCQP *RdmaCtrl::get_rc_qp(QPIdx idx) {
return impl_->get_rc_qp(idx);
}
inline __attribute__ ((always_inline))
UDQP *RdmaCtrl::get_ud_qp(QPIdx idx) {
return impl_->get_ud_qp(idx);
}
inline __attribute__ ((always_inline))
int RdmaCtrl::current_node_id() {
return impl_->node_id_;
}
inline __attribute__ ((always_inline))
int RdmaCtrl::listening_port() {
return impl_->tcp_base_port_;
}
inline __attribute__ ((always_inline))
bool RdmaCtrl::link_symmetric_rcqps(const std::vector<std::string> &cluster,
int l_mrid,int mr_id,int wid,int idx) {
return impl_->link_symmetric_rcqps(cluster,l_mrid,mr_id,wid,idx);
}
inline __attribute__ ((always_inline))
std::vector<RNicInfo> RdmaCtrl::query_devs_helper() {
return RdmaCtrlImpl::query_devs_helper();
}
inline __attribute__ ((always_inline))
void RdmaCtrl::register_qp_callback(connection_callback_t callback) {
impl_->register_qp_callback(callback);
}
};