-
Notifications
You must be signed in to change notification settings - Fork 92
/
ffrouter.cpp
3139 lines (2600 loc) · 129 KB
/
ffrouter.cpp
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "ffrouter.h"
#include "rdma_api.h"
#include "verbs_cmd.h"
#include <ifaddrs.h>
void mem_flush(const void *p, int allocation_size)
{
const size_t cache_line = 64;
const char *cp = (const char *)p;
size_t i = 0;
if (p == NULL || allocation_size <= 0)
return;
for (i = 0; i < allocation_size; i += cache_line) {
asm volatile("clflush (%0)\n\t"
:
: "r"(&cp[i])
: "memory");
}
asm volatile("sfence\n\t"
:
:
: "memory");
}
FreeFlowRouter::~FreeFlowRouter()
{
for(std::map<int, std::vector<ShmPiece*> >::iterator it = this->shm_pool.begin(); it != this->shm_pool.end(); it++)
{
for (int i = 0; i < it->second.size(); i++)
{
delete it->second[i];
}
}
for(std::map<std::string, ShmPiece* >::iterator it = this->shm_map.begin(); it != this->shm_map.end(); it++)
{
delete it->second;
}
}
ShmPiece* FreeFlowRouter::addShmPiece(int client_id, int mem_size)
{
pthread_mutex_lock(&this->shm_mutex);
if (this->shm_pool.find(client_id) == this->shm_pool.end())
{
std::vector<ShmPiece*> v;
this->shm_pool[client_id] = v;
}
int count = this->shm_pool[client_id].size();
std::stringstream ss;
ss << "client-" << client_id << "-memsize-" << mem_size << "-index-" << count;
ShmPiece *sp = new ShmPiece(ss.str().c_str(), mem_size);
this->shm_pool[client_id].push_back(sp);
if (!sp->open())
{
sp = NULL;
}
pthread_mutex_unlock(&this->shm_mutex);
return sp;
}
ShmPiece* FreeFlowRouter::addShmPiece(std::string shm_name, int mem_size)
{
pthread_mutex_lock(&this->shm_mutex);
if (this->shm_map.find(shm_name) != this->shm_map.end())
{
pthread_mutex_unlock(&this->shm_mutex);
return this->shm_map[shm_name];
}
ShmPiece *sp = new ShmPiece(shm_name.c_str(), mem_size);
if (!sp->open())
{
sp = NULL;
}
this->shm_map[shm_name] = sp;
pthread_mutex_unlock(&this->shm_mutex);
return sp;
}
ShmPiece* FreeFlowRouter::initCtrlShm(const char* tag)
{
std::stringstream ss;
ss << "ctrlshm-" << tag;
ShmPiece *sp = new ShmPiece(ss.str().c_str(), sizeof(struct CtrlShmPiece));
if (!sp->open())
{
sp = NULL;
}
if (!sp)
LOG_ERROR("Failed to create control shm for tag " << tag);
memset(sp->ptr, 0, sizeof(struct CtrlShmPiece));
struct CtrlShmPiece *csp = (struct CtrlShmPiece *)(sp->ptr);
csp->state = IDLE;
return sp;
}
FreeFlowRouter::FreeFlowRouter(const char* name) {
LOG_INFO("FreeFlowRouter Init");
this->name = name;
this->pathname = "/freeflow/";
this->pathname.append(this->name);
this->host_ip = 0;
if (getenv("HOST_IP_PREFIX")) {
const char* prefix = getenv("HOST_IP_PREFIX");
uint32_t prefix_ip = 0, prefix_mask = 0;
uint8_t a, b, c, d, bits;
if (sscanf(prefix, "%hhu.%hhu.%hhu.%hhu/%hhu", &a, &b, &c, &d, &bits) == 5) {
if (bits <= 32) {
prefix_ip = htonl(
(a << 24UL) |
(b << 16UL) |
(c << 8UL) |
(d));
prefix_mask = htonl((0xFFFFFFFFUL << (32 - bits)) & 0xFFFFFFFFUL);
}
}
if (prefix_ip != 0 || prefix_mask != 0) {
struct ifaddrs *ifaddr, *ifa;
getifaddrs(&ifaddr);
ifa = ifaddr;
while (ifa) {
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in *pAddr = (struct sockaddr_in *)ifa->ifa_addr;
if ((pAddr->sin_addr.s_addr & prefix_mask) == (prefix_ip & prefix_mask)) {
this->host_ip = pAddr->sin_addr.s_addr;
break;
}
}
ifa = ifa->ifa_next;
}
freeifaddrs(ifaddr);
}
}
if (getenv("HOST_IP")) {
this->host_ip = inet_addr(getenv("HOST_IP"));
}
if (!this->host_ip) {
LOG_ERROR("Missing HOST_IP or HOST_IP_PREFIX. Socket may not work.");
}
else {
struct in_addr addr_tmp;
addr_tmp.s_addr = this->host_ip;
LOG_INFO("Socket binding address:" << inet_ntoa(addr_tmp));
}
if (!getenv("RDMA_POLLING_INTERVAL_US")) {
this->rdma_polling_interval = 0;
}
else {
this->rdma_polling_interval = atoi(getenv("RDMA_POLLING_INTERVAL_US"));
}
if (!getenv("DISABLE_RDMA")) {
this->disable_rdma = 0;
}
else {
this->disable_rdma = atoi(getenv("DISABLE_RDMA"));
}
LOG_DEBUG("Pathname for Unix domain socket: " << this->pathname);
for (int i = 0; i < MAP_SIZE; i++)
{
this->pd_map[i] = NULL;
this->cq_map[i] = NULL;
this->qp_map[i] = NULL;
this->mr_map[i] = NULL;
this->ah_map[i] = NULL;
this->srq_map[i] = NULL;
this->shmr_map[i] = NULL;
this->qp_shm_map[i] = NULL;
this->cq_shm_map[i] = NULL;
this->srq_shm_map[i] = NULL;
this->channel_map[i] = NULL;
this->event_channel_map[i] = NULL;
this->cm_id_map[i] = NULL;
}
pthread_mutex_init(&this->qp_shm_vec_mtx, NULL);
pthread_mutex_init(&this->cq_shm_vec_mtx, NULL);
pthread_mutex_init(&this->srq_shm_vec_mtx, NULL);
pthread_mutex_init(&this->rkey_mr_shm_mtx, NULL);
pthread_mutex_init(&this->lkey_ptr_mtx, NULL);
pthread_mutex_init(&this->shm_mutex, NULL);
if (!this->disable_rdma) {
setup_ib(&this->rdma_data);
LOG_DEBUG("RDMA Dev: dev.name=" << this->rdma_data.ib_device->name << ", " << "dev.dev_name=" << this->rdma_data.ib_device->dev_name);
}
this->vip_map["10.47.0.4"] = "192.168.2.13";
this->vip_map["10.47.0.6"] = "192.168.2.13";
this->vip_map["10.47.0.7"] = "192.168.2.13";
this->vip_map["10.47.0.8"] = "192.168.2.13";
this->vip_map["10.44.0.3"] = "192.168.2.15";
this->vip_map["10.44.0.4"] = "192.168.2.15";
this->vip_map["10.44.0.6"] = "192.168.2.15";
}
void FreeFlowRouter::start()
{
LOG_INFO("FreeFlowRouter Starting... ");
if (!disable_rdma) {
start_udp_server();
pthread_t ctrl_th; //the fast data path thread
struct HandlerArgs ctrl_args;
ctrl_args.ffr = this;
pthread_create(&ctrl_th, NULL, (void* (*)(void*))CtrlChannelLoop, &ctrl_args);
sleep(1.0);
}
char c;
// FILE *fp;
register int i, len;
struct sockaddr_un saun;
if ((this->sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
LOG_ERROR("Cannot create Unix domain socket.");
exit(1);
}
saun.sun_family = AF_UNIX;
strcpy(saun.sun_path, this->pathname.c_str());
unlink(this->pathname.c_str());
len = sizeof(saun.sun_family) + strlen(saun.sun_path);
if (bind(this->sock, (const sockaddr*)&saun, len) < 0) {
LOG_ERROR("Cannot bind Unix domain socket.");
exit(1);
}
if (listen(this->sock, 128) < 0) {
LOG_ERROR("Cannot listen Unix domain socket.");
exit(1);
}
int client_sock;
int fromlen = sizeof(struct sockaddr_un);
struct sockaddr_un fsaun;
memset(&fsaun, 0, sizeof fsaun);
int count = 0;
LOG_DEBUG("Accepting new clients... ");
while (1)
{
if ((client_sock = accept(this->sock, (sockaddr*)&fsaun, (socklen_t*)&fromlen)) < 0) {
LOG_ERROR("Failed to accept." << errno);
exit(1);
}
LOG_TRACE("New client with sock " << client_sock << ".");
// Start a thread to handle the request.
pthread_t *pth = (pthread_t *) malloc(sizeof(pthread_t));
struct HandlerArgs *args = (struct HandlerArgs *) malloc(sizeof(struct HandlerArgs));
args->ffr = this;
args->client_sock = client_sock;
int ret = pthread_create(pth, NULL, (void* (*)(void*))HandleRequest, args);
LOG_TRACE("result of pthread_create --> " << ret);
count ++;
}
}
void CtrlChannelLoop(struct HandlerArgs *args)
{
LOG_INFO("Start the control channel loop.");
FreeFlowRouter *ffr = args->ffr;
cpu_set_t cpuset;
//the CPU we want to use
int cpu = 2;
CPU_ZERO(&cpuset); //clears the cpuset
CPU_SET( cpu , &cpuset); //set CPU 2 on cpuset
/*
* cpu affinity for the calling thread
* first parameter is the pid, 0 = calling thread
* second parameter is the size of your cpuset
* third param is the cpuset in which your thread will be
* placed. Each bit represents a CPU
*/
sched_setaffinity(0, sizeof(cpuset), &cpuset);
unsigned int count = 0;
ShmPiece *qp_sp = NULL;
ShmPiece *cq_sp = NULL;
ShmPiece *srq_sp = NULL;
struct CtrlShmPiece *qp_csp = NULL;
struct CtrlShmPiece *cq_csp = NULL;
struct CtrlShmPiece *srq_csp = NULL;
void *req_body, *rsp;
struct FfrRequestHeader *req_header;
struct FfrResponseHeader *rsp_header;
struct ibv_qp *qp = NULL;
TokenBucket *tb = NULL;
struct ibv_cq *cq = NULL;
struct ibv_srq *srq = NULL;
struct ibv_wc *wc_list = NULL;
while (1)
{
pthread_mutex_lock(&ffr->qp_shm_vec_mtx);
for (int i = 0; i < ffr->qp_shm_vec.size(); i ++)
{
qp_sp = ffr->qp_shm_map[ffr->qp_shm_vec[i]];
// QP operations.
if (qp_sp)
{
qp_csp = (struct CtrlShmPiece*)qp_sp->ptr;
//rmb();
if (qp_csp->state == REQ_DONE)
{
//clock_gettime(CLOCK_REALTIME, &st);
req_header = (struct FfrRequestHeader *)qp_csp->req;
req_body = qp_csp->req + sizeof(struct FfrRequestHeader);
rsp_header = (struct FfrResponseHeader *)qp_csp->rsp;
rsp = qp_csp->rsp + sizeof(struct FfrResponseHeader);
switch(req_header->func)
{
case IBV_POST_SEND:
{
// Now recover the qp and wr
struct ibv_post_send *post_send = (struct ibv_post_send*)req_body;
if (post_send->qp_handle >= MAP_SIZE)
{
LOG_ERROR("[Warning] QP handle (" << post_send->qp_handle << ") is no less than MAX_QUEUE_MAP_SIZE.");
}
else
{
qp = ffr->qp_map[post_send->qp_handle];
tb = ffr->tokenbucket[post_send->qp_handle];
}
struct ibv_send_wr *wr = (struct ibv_send_wr*)((char*)req_body + sizeof(struct ibv_post_send));
struct ibv_sge *sge = (struct ibv_sge*)((char*)req_body + sizeof(struct ibv_post_send) + post_send->wr_count * sizeof(struct ibv_send_wr));
uint32_t *ah = NULL;
if (qp->qp_type == IBV_QPT_UD) {
//LOG_INFO("POST_SEND_UD!!!");
ah = (uint32_t*)(sge + post_send->sge_count);
}
uint32_t wr_success = 0;
int count = 0;
for (int i = 0; i < post_send->wr_count; i++)
{
//LOG_DEBUG("wr[i].wr_id=" << wr[i].wr_id << " opcode=" << wr[i].opcode << " imm_data==" << wr[i].imm_data);
if (wr[i].opcode == IBV_WR_RDMA_WRITE || wr[i].opcode == IBV_WR_RDMA_WRITE_WITH_IMM || wr[i].opcode == IBV_WR_RDMA_READ) {
while(1)
{
pthread_mutex_lock(&ffr->rkey_mr_shm_mtx);
if (ffr->rkey_mr_shm.find(wr[i].wr.rdma.rkey) == ffr->rkey_mr_shm.end()) {
if (count > 4)
{
LOG_ERROR("One sided opertaion: can't find remote MR. rkey --> " << wr[i].wr.rdma.rkey << " addr --> " << wr[i].wr.rdma.remote_addr);
pthread_mutex_unlock(&ffr->rkey_mr_shm_mtx);
break;
}
}
else {
;//LOG_DEBUG("shm:" << (uint64_t)(ffr->rkey_mr_shm[wr[i].wr.rdma.rkey].shm_ptr) << " app:" << (uint64_t)(wr[i].wr.rdma.remote_addr) << " mr:" << (uint64_t)(ffr->rkey_mr_shm[wr[i].wr.rdma.rkey].mr_ptr));
wr[i].wr.rdma.remote_addr = (uint64_t)(ffr->rkey_mr_shm[wr[i].wr.rdma.rkey].shm_ptr) + (uint64_t)wr[i].wr.rdma.remote_addr - (uint64_t)ffr->rkey_mr_shm[wr[i].wr.rdma.rkey].mr_ptr;
pthread_mutex_unlock(&ffr->rkey_mr_shm_mtx);
break;
}
pthread_mutex_unlock(&ffr->rkey_mr_shm_mtx);
sleep(0.5);
count ++;
}
}
// fix the link list pointer
if (i >= post_send->wr_count - 1) {
wr[i].next = NULL;
}
else {
wr[i].next = &(wr[i+1]);
}
if (wr[i].num_sge > 0) {
// fix the sg list pointer
wr[i].sg_list = sge;
pthread_mutex_lock(&ffr->lkey_ptr_mtx);
for (int j = 0; j < wr[i].num_sge; j++) {
/*while (!(tb->consume(sge[j].length))) {
uint32_t stime = sge[j].length * 1000000 / MAX_QP_RATE_LIMIT;
if (stime) {
usleep(stime);
}
else {
usleep(1);
}
//wr[i-1].next = NULL;
//break;
}*/
//LOG_DEBUG("wr[i].wr_id=" << wr[i].wr_id << " qp_num=" << qp->qp_num << " sge.addr=" << sge[j].addr << " sge.length" << sge[j].length << " opcode=" << wr[i].opcode);
sge[j].addr = (uint64_t)((char*)(ffr->lkey_ptr[sge[j].lkey]) + sge[j].addr);
//LOG_DEBUG("data=" << ((char*)(sge[j].addr))[0] << ((char*)(sge[j].addr))[1] << ((char*)(sge[j].addr))[2]);
//LOG_DEBUG("imm_data==" << wr[i].imm_data);
}
pthread_mutex_unlock(&ffr->lkey_ptr_mtx);
sge += wr[i].num_sge;
}
else {
wr[i].sg_list = NULL;
}
// fix ah
if (qp->qp_type == IBV_QPT_UD) {
wr[i].wr.ud.ah = ffr->ah_map[*ah];
}
wr_success++;
}
struct ibv_send_wr *bad_wr = NULL;
//rsp = malloc(sizeof(struct IBV_POST_SEND_RSP));
rsp_header->rsp_size = sizeof(struct IBV_POST_SEND_RSP);
((struct IBV_POST_SEND_RSP*)rsp)->ret_errno = ibv_post_send(qp, wr, &bad_wr);
if (((struct IBV_POST_SEND_RSP*)rsp)->ret_errno != 0) {
LOG_ERROR("[Error] Post send (" << qp->handle << ") fails.");
}
//LOG_DEBUG("post_send success.");
if (bad_wr == NULL) {
// this IF is not needed right now, but left here for future use
if (post_send->wr_count == wr_success) {
((struct IBV_POST_SEND_RSP*)rsp)->bad_wr = 0;
}
else {
((struct IBV_POST_SEND_RSP*)rsp)->bad_wr = post_send->wr_count - wr_success;
((struct IBV_POST_SEND_RSP*)rsp)->ret_errno = ENOMEM;
}
}
else{
LOG_ERROR("bad_wr is not NULL.");
((struct IBV_POST_SEND_RSP*)rsp)->bad_wr = bad_wr - wr;
}
}
break;
case IBV_POST_RECV:
{
// LOG_DEBUG("IBV_POST_RECV");
// Now recover the qp and wr
struct ibv_post_recv *post_recv = (struct ibv_post_recv*)req_body;
if (post_recv->qp_handle >= MAP_SIZE)
{
LOG_ERROR("[Warning] QP handle (" << post_recv->qp_handle << ") is no less than MAX_QUEUE_MAP_SIZE.");
}
else
{
qp = ffr->qp_map[post_recv->qp_handle];
}
struct ibv_recv_wr *wr = (struct ibv_recv_wr*)((char*)req_body + sizeof(struct ibv_post_recv));
struct ibv_sge *sge = (struct ibv_sge*)((char*)req_body + sizeof(struct ibv_post_recv) + post_recv->wr_count * sizeof(struct ibv_recv_wr));
for (int i = 0; i < post_recv->wr_count; i++)
{
// fix the link list pointer
if (i >= post_recv->wr_count - 1) {
wr[i].next = NULL;
}
else {
wr[i].next = &(wr[i+1]);
}
if (wr[i].num_sge > 0) {
// fix the sg list pointer
wr[i].sg_list = sge;
pthread_mutex_lock(&ffr->lkey_ptr_mtx);
for (int j = 0; j < wr[i].num_sge; j++) {
sge[j].addr = (uint64_t)(ffr->lkey_ptr[sge[j].lkey]) + (uint64_t)(sge[j].addr);
}
pthread_mutex_unlock(&ffr->lkey_ptr_mtx);
sge += wr[i].num_sge;
}
else {
wr[i].sg_list = NULL;
}
//LOG_ERROR("wr[i].sg_list=" << wr[i].sg_list << " wr[i].num_sge=" << wr[i].num_sge);
}
struct ibv_recv_wr *bad_wr = NULL;
//rsp = malloc(sizeof(struct IBV_POST_RECV_RSP));
rsp_header->rsp_size = sizeof(struct IBV_POST_RECV_RSP);
((struct IBV_POST_RECV_RSP*)rsp)->ret_errno = ibv_post_recv(qp, wr, &bad_wr);
if (((struct IBV_POST_RECV_RSP*)rsp)->ret_errno != 0) {
LOG_ERROR("[Error] Post recv (" << qp->handle << ") fails. error=" << ((struct IBV_POST_RECV_RSP*)rsp)->ret_errno);
}
if (bad_wr == NULL)
{
((struct IBV_POST_RECV_RSP*)rsp)->bad_wr = 0;
}
else
{
((struct IBV_POST_RECV_RSP*)rsp)->bad_wr = bad_wr - wr;
}
}
break;
default:
break;
}
wmb();
qp_csp->state = RSP_DONE;
//wc_wmb();
//mem_flush((void*)&qp_csp->state, sizeof(enum CtrlChannelState));
/*clock_gettime(CLOCK_REALTIME, &et);
LOG_ERROR("REQ_DONE tv_sec=" << st.tv_sec << ", tv_nsec=" << st.tv_nsec);
LOG_SRQROR("RSP_DONE tv_sec=" << et.tv_sec << ", tv_nsec=" << et.tv_nsec);*/
}
}
}
pthread_mutex_unlock(&ffr->qp_shm_vec_mtx);
// CQ operations.
pthread_mutex_lock(&ffr->cq_shm_vec_mtx);
for (int i = 0; i < ffr->cq_shm_vec.size(); i ++)
{
cq_sp = ffr->cq_shm_map[ffr->cq_shm_vec[i]];
if (cq_sp)
{
cq_csp = (struct CtrlShmPiece*)cq_sp->ptr;
//rmb();
switch (cq_csp->state)
{
case REQ_DONE:
{
req_header = (struct FfrRequestHeader *)cq_csp->req;
req_body = cq_csp->req + sizeof(struct FfrRequestHeader);
rsp_header = (struct FfrResponseHeader *)cq_csp->rsp;
rsp = cq_csp->rsp + sizeof(struct FfrResponseHeader);
if (((struct IBV_POLL_CQ_REQ *)req_body)->cq_handle >= MAP_SIZE)
{
LOG_ERROR("CQ handle (" << ((struct IBV_POLL_CQ_REQ *)req_body)->cq_handle << ") is no less than MAX_QUEUE_MAP_SIZE.");
}
else
{
cq = ffr->cq_map[((struct IBV_POLL_CQ_REQ *)req_body)->cq_handle];
}
if (cq == NULL)
{
LOG_ERROR("cq pointer is NULL cq_handle -->" << ((struct IBV_POLL_CQ_REQ *)req_body)->cq_handle);
break;
}
wc_list = (struct ibv_wc*)((char *)rsp);
count = ibv_poll_cq(cq, ((struct IBV_POLL_CQ_REQ *)req_body)->ne, wc_list);
if (count <= 0)
{
rsp_header->rsp_size = 0;
}
else
{
rsp_header->rsp_size = count * sizeof(struct ibv_wc);
}
for (i = 0; i < count; i++)
{
if (wc_list[i].status == 0)
{
LOG_DEBUG("======== wc =========");
LOG_DEBUG("wr_id=" << wc_list[i].wr_id);
LOG_DEBUG("status=" << wc_list[i].status);
LOG_DEBUG("opcode=" << wc_list[i].opcode);
LOG_DEBUG("vendor_err=" << wc_list[i].vendor_err);
LOG_DEBUG("byte_len=" << wc_list[i].byte_len);
LOG_DEBUG("imm_data=" << wc_list[i].imm_data);
LOG_DEBUG("qp_num=" << wc_list[i].qp_num);
LOG_DEBUG("src_qp=" << wc_list[i].src_qp);
LOG_DEBUG("wc_flags=" << wc_list[i].wc_flags);
LOG_DEBUG("pkey_index=" << wc_list[i].pkey_index);
LOG_DEBUG("slid=" << wc_list[i].slid);
LOG_DEBUG("sl=" << wc_list[i].sl);
LOG_DEBUG("dlid_path_bits=" << wc_list[i].dlid_path_bits);
}
else
{
LOG_DEBUG("########## wc ############");
LOG_DEBUG("wr_id=" << wc_list[i].wr_id);
LOG_DEBUG("status=" << wc_list[i].status);
LOG_DEBUG("opcode=" << wc_list[i].opcode);
LOG_DEBUG("vendor_err=" << wc_list[i].vendor_err);
LOG_DEBUG("byte_len=" << wc_list[i].byte_len);
LOG_DEBUG("imm_data=" << wc_list[i].imm_data);
LOG_DEBUG("qp_num=" << wc_list[i].qp_num);
LOG_DEBUG("src_qp=" << wc_list[i].src_qp);
LOG_DEBUG("wc_flags=" << wc_list[i].wc_flags);
LOG_DEBUG("pkey_index=" << wc_list[i].pkey_index);
LOG_DEBUG("slid=" << wc_list[i].slid);
LOG_DEBUG("sl=" << wc_list[i].sl);
LOG_DEBUG("dlid_path_bits=" << wc_list[i].dlid_path_bits);
}
}
wmb();
cq_csp->state = RSP_DONE;
//wc_wmb();
//mem_flush((void*)&cq_csp->state, 4);
}
break;
default: break;
}
}
}
pthread_mutex_unlock(&ffr->cq_shm_vec_mtx);
// SRQ operations
pthread_mutex_lock(&ffr->srq_shm_vec_mtx);
for (int i = 0; i < ffr->srq_shm_vec.size(); i ++)
{
srq_sp = ffr->srq_shm_map[ffr->srq_shm_vec[i]];
if (srq_sp)
{
srq_csp = (struct CtrlShmPiece*)srq_sp->ptr;
if (srq_csp->state == REQ_DONE)
{
// Now recover the qp and wr
req_header = (struct FfrRequestHeader *)srq_csp->req;
req_body = srq_csp->req + sizeof(struct FfrRequestHeader);
rsp_header = (struct FfrResponseHeader *)srq_csp->rsp;
rsp = srq_csp->rsp + sizeof(struct FfrResponseHeader);
struct ibv_post_srq_recv *post_recv = (struct ibv_post_srq_recv*)req_body;
if (post_recv->srq_handle >= MAP_SIZE)
{
LOG_ERROR("[Warning] SRQ handle (" << post_recv->srq_handle << ") is no less than MAX_QUEUE_MAP_SIZE.");
}
else
{
srq = ffr->srq_map[post_recv->srq_handle];
}
struct ibv_recv_wr *wr = (struct ibv_recv_wr*)((char*)req_body + sizeof(struct ibv_post_srq_recv));
struct ibv_sge *sge = (struct ibv_sge*)((char*)req_body + sizeof(struct ibv_post_srq_recv) + post_recv->wr_count * sizeof(struct ibv_recv_wr));
for (int i = 0; i < post_recv->wr_count; i++)
{
// fix the link list pointer
if (i >= post_recv->wr_count - 1) {
wr[i].next = NULL;
}
else {
wr[i].next = &(wr[i+1]);
}
if (wr[i].num_sge > 0) {
// fix the sg list pointer
wr[i].sg_list = sge;
pthread_mutex_lock(&ffr->lkey_ptr_mtx);
for (int j = 0; j < wr[i].num_sge; j++) {
sge[j].addr = (uint64_t)(ffr->lkey_ptr[sge[j].lkey]) + (uint64_t)(sge[j].addr);
}
pthread_mutex_unlock(&ffr->lkey_ptr_mtx);
sge += wr[i].num_sge;
}
else {
wr[i].sg_list = NULL;
}
}
struct ibv_recv_wr *bad_wr = NULL;
//rsp = malloc(sizeof(struct IBV_POST_SRQ_RECV_RSP));
rsp_header->rsp_size = sizeof(struct IBV_POST_SRQ_RECV_RSP);
((struct IBV_POST_SRQ_RECV_RSP*)rsp)->ret_errno = ibv_post_srq_recv(srq, wr, &bad_wr);
if (((struct IBV_POST_SRQ_RECV_RSP*)rsp)->ret_errno != 0) {
LOG_ERROR("[Error] Srq post recv (" << srq->handle << ") fails.");
}
if (bad_wr == NULL)
{
((struct IBV_POST_SRQ_RECV_RSP*)rsp)->bad_wr = 0;
}
else
{
((struct IBV_POST_SRQ_RECV_RSP*)rsp)->bad_wr = bad_wr - wr;
}
wmb();
srq_csp->state = RSP_DONE;
}
}
}
pthread_mutex_unlock(&ffr->srq_shm_vec_mtx);
if (ffr->rdma_polling_interval > 0) {
usleep(ffr->rdma_polling_interval);
}
}
}
void HandleRequest(struct HandlerArgs *args)
{
LOG_TRACE("Start to handle the request from client sock " << args->client_sock << ".");
FreeFlowRouter *ffr = args->ffr;
int client_sock = args->client_sock;
// Speed up
char *req_body = NULL;
char *rsp = NULL;
if (ffr->disable_rdma) {
req_body = (char*)malloc(0xff);
rsp = (char*)malloc(0xff);
}
else {
req_body = (char*)malloc(0xfffff);
rsp = (char*)malloc(0xfffff);
}
while(1)
{
int n = 0, size = 0, count = 0, i = 0, ret = 0, host_fd = -1;
//void *req_body = NULL;
//void *rsp = NULL;
void *context = NULL;
struct ibv_cq *cq = NULL;
struct ibv_qp *qp = NULL;
struct ibv_pd *pd = NULL;
struct ibv_mr *mr = NULL;
struct ibv_ah *ah = NULL;
struct ibv_srq *srq = NULL;
struct ibv_comp_channel *channel = NULL;
struct rdma_event_channel *event_channel = NULL;
struct rdma_cm_id *cm_id = NULL;
ShmPiece *sp = NULL;
struct ibv_wc *wc_list = NULL;
TokenBucket *tb = NULL;
struct FfrRequestHeader header;
LOG_TRACE("Start to read from sock " << client_sock);
if ((n = read(client_sock, &header, sizeof(header))) < sizeof(header))
{
if (n < 0)
LOG_ERROR("Failed to read the request header. Read bytes: " << n << " Size of Header: " << sizeof(header));
goto kill;
}
else
{
LOG_TRACE("Get request cmd " << header.func);
}
switch(header.func)
{
case IBV_GET_CONTEXT:
{
LOG_DEBUG("GET_CONTEXT");
//rsp = malloc(sizeof(struct IBV_GET_CONTEXT_RSP));
size = sizeof(struct IBV_GET_CONTEXT_RSP);
((struct IBV_GET_CONTEXT_RSP *)rsp)->async_fd = ffr->rdma_data.ib_context->async_fd;
((struct IBV_GET_CONTEXT_RSP *)rsp)->num_comp_vectors = ffr->rdma_data.ib_context->num_comp_vectors;
}
break;
case IBV_QUERY_DEV:
{
LOG_DEBUG("QUERY_DEV client_id=" << client_sock);
//rsp = malloc(sizeof(struct IBV_QUERY_DEV_RSP));
size = sizeof(struct IBV_QUERY_DEV_RSP);
memcpy(&((struct IBV_QUERY_DEV_RSP *)rsp)->dev_attr, &ffr->rdma_data.ib_dev_attr, sizeof(struct ibv_device_attr));
/*((struct IBV_QUERY_DEV_RSP *)rsp)->dev_attr.max_srq = 0;
((struct IBV_QUERY_DEV_RSP *)rsp)->dev_attr.max_srq_wr = 0;
((struct IBV_QUERY_DEV_RSP *)rsp)->dev_attr.max_srq_sge = 0;*/
LOG_DEBUG("Finished QUERY_DEV client_id=" << client_sock);
}
break;
case IBV_EXP_QUERY_DEV:
{
LOG_DEBUG("EXP_QUERY_DEV client_id=" << client_sock << " cmd_fd=" << ffr->rdma_data.ib_context->cmd_fd);
if (read(client_sock, req_body, sizeof(struct IBV_EXP_QUERY_DEV_REQ)) < sizeof(struct IBV_EXP_QUERY_DEV_REQ))
{
LOG_ERROR("Failed to read the request body.");
goto kill;
}
//rsp = malloc(sizeof(struct IBV_QUERY_DEV_RSP));
size = sizeof(struct IBV_EXP_QUERY_DEV_RSP);
((struct IBV_EXP_QUERY_DEV_RSP *)rsp)->ret_errno = ibv_exp_cmd_query_device_resp(
ffr->rdma_data.ib_context->cmd_fd,
&((IBV_EXP_QUERY_DEV_REQ*)req_body)->cmd,
&((IBV_EXP_QUERY_DEV_RSP*)rsp)->resp);
size = sizeof(struct IBV_EXP_QUERY_DEV_RSP);
if (((struct IBV_EXP_QUERY_DEV_RSP *)rsp)->ret_errno != 0)
LOG_ERROR("Return error (" << ((struct IBV_EXP_QUERY_DEV_RSP *)rsp)->ret_errno << ") in EXP_QUERY_DEV");
}
break;
case IBV_QUERY_PORT:
{
LOG_DEBUG("QUERY_PORT client_id=" << client_sock);
//req_body = malloc(sizeof(struct IBV_QUERY_PORT_REQ));
if (read(client_sock, req_body, sizeof(struct IBV_QUERY_PORT_REQ)) < sizeof(struct IBV_QUERY_PORT_REQ))
{
LOG_ERROR("Failed to read request body.");
goto kill;
}
//rsp = malloc(sizeof(struct IBV_QUERY_PORT_RSP));
size = sizeof(struct IBV_QUERY_PORT_RSP);
if (ibv_query_port(ffr->rdma_data.ib_context,
((IBV_QUERY_PORT_REQ*)req_body)->port_num, &((struct IBV_QUERY_PORT_RSP *)rsp)->port_attr) < 0)
{
LOG_ERROR("Cannot query port" << ((IBV_QUERY_PORT_REQ*)req_body)->port_num);
}
}
break;
case IBV_ALLOC_PD:
{
LOG_DEBUG("ALLOC_PD");
//rsp = malloc(sizeof(struct IBV_ALLOC_PD_RSP));
size = sizeof(struct IBV_ALLOC_PD_RSP);
pd = ibv_alloc_pd(ffr->rdma_data.ib_context);
if (pd->handle >= MAP_SIZE)
{
LOG_INFO("PD handle is no less than MAX_QUEUE_MAP_SIZE. pd_handle=" << pd->handle);
}
else
{
ffr->pd_map[pd->handle] = pd;
}
((struct IBV_ALLOC_PD_RSP *)rsp)->pd_handle = pd->handle;
LOG_DEBUG("Return pd_handle " << pd->handle << "for client_id " << header.client_id);
}
break;
case IBV_DEALLOC_PD:
{
LOG_DEBUG("DEALLOC_PD");
//req_body = malloc(sizeof(struct IBV_DEALLOC_PD_REQ));
if (read(client_sock, req_body, sizeof(struct IBV_DEALLOC_PD_REQ)) < sizeof(struct IBV_DEALLOC_PD_REQ))
{
LOG_ERROR("Failed to read the request body.");
goto kill;
}
LOG_DEBUG("Dealloc PD: " << ((IBV_DEALLOC_PD_REQ*)req_body)->pd_handle);
pd = ffr->pd_map[((struct IBV_DEALLOC_PD_REQ *)req_body)->pd_handle];
if (pd == NULL)
{
LOG_ERROR("Failed to get pd with pd_handle " << ((struct IBV_DEALLOC_PD_REQ *)req_body)->pd_handle);
goto end;
}
ffr->pd_map[((struct IBV_DEALLOC_PD_REQ *)req_body)->pd_handle] = NULL;
ret = ibv_dealloc_pd(pd);
//rsp = malloc(sizeof(struct IBV_DEALLOC_PD_RSP));
((struct IBV_DEALLOC_PD_RSP *)rsp)->ret = ret;
size = sizeof(struct IBV_DEALLOC_PD_RSP);
}
break;
case IBV_CREATE_CQ:
{
LOG_INFO("CREATE_CQ, body_size=" << header.body_size);
//req_body = malloc(sizeof(struct IBV_CREATE_CQ_REQ));
if (read(client_sock, req_body, sizeof(struct IBV_CREATE_CQ_REQ)) < sizeof(struct IBV_CREATE_CQ_REQ))
{
LOG_ERROR("DESTROY_CQ: Failed to read the request body.");
goto kill;
}
if (((struct IBV_CREATE_CQ_REQ *)req_body)->channel_fd < 0)
{
channel = NULL;
}
else
{
channel = ffr->channel_map[((struct IBV_CREATE_CQ_REQ *)req_body)->channel_fd];
if (channel == NULL)
{
LOG_ERROR("Failed to get channel with fd " << ((struct IBV_CREATE_CQ_REQ *)req_body)->channel_fd);
goto end;
}
}
cq = ibv_create_cq(ffr->rdma_data.ib_context, ((struct IBV_CREATE_CQ_REQ *)req_body)->cqe, NULL, channel, ((struct IBV_CREATE_CQ_REQ *)req_body)->comp_vector);
if (cq->handle >= MAP_SIZE)
{
LOG_INFO("CQ handle (" << cq->handle << ") is no less than MAX_QUEUE_MAP_SIZE.");
}
else
{
ffr->cq_map[cq->handle] = cq;
}
//rsp = malloc(sizeof(struct IBV_CREATE_CQ_RSP));
((struct IBV_CREATE_CQ_RSP *)rsp)->cqe = cq->cqe;
((struct IBV_CREATE_CQ_RSP *)rsp)->handle = cq->handle;
size = sizeof(struct IBV_CREATE_CQ_RSP);
LOG_DEBUG("Create CQ: cqe=" << cq->cqe << " handle=" << cq->handle);
std::stringstream ss;
ss << "cq" << cq->handle;
ShmPiece* sp = ffr->initCtrlShm(ss.str().c_str());
ffr->cq_shm_map[cq->handle] = sp;
strcpy(((struct IBV_CREATE_CQ_RSP *)rsp)->shm_name, sp->name.c_str());
pthread_mutex_lock(&ffr->cq_shm_vec_mtx);
ffr->cq_shm_vec.push_back(cq->handle);
pthread_mutex_unlock(&ffr->cq_shm_vec_mtx);
}
break;
case IBV_DESTROY_CQ:
{
LOG_DEBUG("DESTROY_CQ, body_size=" << header.body_size);
//req_body = malloc(sizeof(struct IBV_DESTROY_CQ_REQ));
if (read(client_sock, req_body, sizeof(struct IBV_DESTROY_CQ_REQ)) < sizeof(struct IBV_DESTROY_CQ_REQ))
{
LOG_ERROR("DESTROY_CQ: Failed to read the request body.");
goto kill;
}
LOG_DEBUG("cq_handle in request: " << ((struct IBV_DESTROY_CQ_REQ *)req_body)->cq_handle);
cq = ffr->cq_map[((struct IBV_DESTROY_CQ_REQ *)req_body)->cq_handle];
if (cq == NULL)
{